Val Town Blog 10月02日
Val Town 公钥认证机制详解
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

Val Town 提供了一种基于公钥加密的认证机制,允许用户向第三方验证其身份。用户生成公私钥对,公开发布公钥,并在每次请求时签名数据包。API 端点验证签名和时间戳,确保请求的合法性和时效性。该机制解决了跨用户认证的难题,无需依赖中心化区块链,通过 Web Crypto API 实现安全传输。此外,文章还探讨了将支付功能集成到 Val Town 的可能性,但鉴于 vals 的非线程安全性,建议使用外部 SQL 数据库管理支付。

🔑 Val Town 使用公私钥对进行认证:用户生成密钥对,私钥保密,公钥公开发布。通过签名请求数据包,确保请求的来源和完整性。

⏱️ 时间戳验证:API 端点验证签名时检查时间戳,防止重放攻击,确保请求的时效性。

🚀 Web Crypto API 应用:利用 Web Crypto API 对请求进行签名和验证,实现安全的数据传输和用户身份确认。

💳 支付功能集成探讨:文章提出将支付系统集成到 Val Town 的设想,但考虑到 vals 的非线程安全性,建议使用外部 SQL 数据库管理支付流程。

🔒 第三方认证:该机制允许用户向第三方证明其身份,无需依赖中心化区块链,增强了系统的灵活性和安全性。

Public Key Auth: Val Town users can be your users

on

Every public function in Val Town has an API endpoint. Some vals wrap premium APIs (openai, rime) that you can use for free. The idea is that sometimes you want to just try an API or use it a tiny amount (<$0.10) and we’re happy to cover that.

But what about abuse? It’s easy enough to add a rate-limit to these functions to ensure that the total usage is below some maximum, but it would be nice if we could make this rate-limit per-user. If “borrow my API key” becomes a pattern, you could imagine a marketplace of premium functions on Val Town that somehow charge for usage… but we’re getting ahead of ourselves.

First, we need a way for Val Town users to authenticate themselves to third-parties. This is distinct from authenticating to Val Town, the platform. That’s a much easier problem - we provide api auth tokens to authenticate with us. The challenge is authenticating with another Val Town user. You need to prove that you are who you say you are, that you made this very request, and that you’re making it now. The generic way to solve this sort of problem is public key cryptography:

    Create a public/private key pairPublish the public key under your Val Town usernameWhenever you want to make an authenticated request, collect the request package:
      the endpointthe data you’re sendingthe time you’re making the requestyour Val Town username
    Sign the package & send along the signature with your requestThe API you’re calling can then do the reverse of this:
      fetch your public key from your Val Town accountverify the signature against the provided dataconfirm the timestamp is recent enough.

It took an afternoon to get it working, and the main issue was struggling to understand the Web Crypto API and get them properly encoded into a format that can persist on Val Town. I learned a lot about encoding ArrayBuffers into base64 and back again. But the whole point of Val Town is that now that I’ve written these vals, you don’t have to worry about any of that - you just call @stevekrouse.generateKeys and viola you get keys in beautiful JSON. Let me take you through how to set yourself up to both authenticate with this system and accept authenticated requests.

Click Run to generate keys and see them in JSON. If you’re logged into Val Town, this will save those keys to your account.

Above we generated a public and private key pair. We want your private key to remain private always but we need to publish your public key.

    Go to your public keyPress the 🔒 icon toggle and publish that val

To make an authenticated API request you need to pass:

    The Val you want to callThe args you want to passYour handleYour keys

@stevekrouse.runValAPIAuth packages all this up with a timestamp, signs it, and makes the request. Here’s how you’d use it:

To make an API that verifies the request, you can use @stevekrouse.verifyAPIAuth:

This scheme uses the Val Town Run API, but it can easily be adapted to the Val Town Express API. These keys and this scheme works only for signing/authorizing. We could also build an end-to-end encrypted version of this scheme by encrypting the request with the public key of the recipient. One could also imagine naming these schemes and passing your scheme’s protocol name to its recipient so that it knows how to properly authenticate you.

It’d be cool to add payment primitives on top of these authentication ones. One issue is that vals are not thread-safe and thus aren’t a good data store for payments. However this would be a perfect use for a regular sql database in conjunction with Val Town (Neon, PlanetScale, Supabase, etc). An Val Town user could have a whole payment system in their own namespace. Anyone could register a payment with them and then they would follow up with the payer and recipients later (by email?) to actually collect and distribute the payment.

We might one day adopt authentication and payment into Val Town as a first-class primitives, but it’s fun to keep them in userspace for as long as possible to encourage experimentation and innovation.

You may have noticed that Val Town has many of the the best parts of web3 — a global runtime of code and data — but for good and ill, there’s no blockchain. We keep everyone’s data in a handy Postgres instance, which means it’s super fast and cheap, but you’ve gotta trust us at Val Town. We also lack any sort of authentication or payment primitives… but maybe not for long!

Edit this page

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Val Town 公钥认证 Web Crypto API 第三方认证 支付功能
相关文章