您的后端使用 Passwordless.dev 私有 API 来发起密钥注册、验证登录、为最终用户获取密钥等。
对此 API 发出的所有请求都要求标头中包含您的 API 私有机密以进行身份验证。通过 JavaScript 客户端中的方法向公共 API 发出的请求将需要您的 API 公钥。
/register/token
请求
向 /register/token 端点发出的 POST 请求会为用户创建一个注册令牌,您的前端将使用该令牌来协商 WebAuth 凭据的创建。
请求正文至少必须包含userId 和 username,例如:
POST https://v4.passwordless.dev/register/token HTTP/1.1ApiSecret:myapplication:secret:11f8dd7733744f2596f2a28544b5fbc4Content-Type:application/json{"userId":"107fb578-9559-4540-a0e2-f82ad78852f7","username":"pjfry@passwordless.dev","displayname":"Philip J Fry",}
向 /signin/verify 端点发出的 POST 请求会解压身份验证令牌,该令牌必须通过在前端调用 .signinWith*() 方法来生成(了解更多)并包含在请求正文中,例如:
POST https://v4.passwordless.dev/signin/verify HTTP/1.1ApiSecret:myapplication:secret:11f8dd7733744f2596f2a28544b5fbc4Content-Type:application/json{"token":"d5vzCkL_GvpS4VYtoT3..."}
{"success":true,"userId":"123","timestamp":"3023-08-01T14:43:03Z","rpid":"localhost","origin":"http://localhost:3000","device":"Firefox, Windows 10","country":"SE","nickname":"My Work Phone","expiresAt":"3023-08-01T14:43:03Z","tokenId":"TODO","type":"passkey_signin"// or passkey_register }
向 /signin/generate-token 端点发出的 POST 请求可为用户手动生成一个身份验证令牌,从而避开常规的登录流程(即 .signinWith*() 方法)。生成的令牌可以通过 /signin/verify 端点进行验证,并像普通身份验证令牌一样使用。
POST https://v4.passwordless.dev/signin/generate-token HTTP/1.1ApiSecret:myapplication:secret:11f8dd7733744f2596f2a28544b5fbc4Content-Type:application/json{"userId":"123"}
constapiUrl='https://v4.passwordless.dev';// 生成身份验证令牌,绕过通常的登录过程。constpayload= { userId:'107fb578-9559-4540-a0e2-f82ad78852f7'};// 使用您的 API 私有机密将用户 ID POST 到 Passwordless.dev API。constresponse=awaitfetch(apiUrl +'/signin/generate-token', { method:'POST', body:JSON.stringify(payload), headers: {'ApiSecret':'myapplication:secret:11f8dd7733744f2596f2a28544b5fbc4','Content-Type':'application/json' }});
相应
成功后,/signin/generate-token 端点将返回一个响应对象,例如:
{"token":"d5vzCkL_GvpS4VYtoT3..."}
/alias
请求
向 /alias 端点发出的 POST 请求会根据他们的 userId 向用户添加别名(了解更多),以便允许使用其他用户名、电子邮件地址等登录。
请求正文必须包含用户的 userId 和完整的别名数组,因为发出 POST 请求时预先存在的别名将被覆盖,例如:
POST https://v4.passwordless.dev/alias HTTP/1.1ApiSecret:myapplication:secret:11f8dd7733744f2596f2a28544b5fbc4Content-Type:application/json{"userId":"107fb578-9559-4540-a0e2-f82ad78852f7","aliases": ["pjfry@passwordless.dev","benderrules@passwordless.dev" ],"hashing":true}
任何 API 响应中都不会返回别名,并且可以对其进行哈希处理以保护用户隐私(见上文)。成功后,/alias 端点将返回 HTTP 200 OK 状态代码。
/credentials/list
请求
向 /credentials/list 端点发出的 GET 请求会列出与用户关联的所有已注册凭据(由 userId 指定)。请求必须包含相关的 userId,例如:
GET https://v4.passwordless.dev/credentials/list?userId=107fb578-9559-4540-a0e2-f82ad78852f7 HTTP/1.1ApiSecret:myapplication:secret:11f8dd7733744f2596f2a28544b5fbc4
[ {"descriptor": {"type":"public-key","id":"2mgrJ6LPItfxbnVc2UgFPHowNGKaYBm3Pf4so1bsXSk" }, "publicKey": "pQECAyYgASFYIPi4M0A+ZFeyOHEC9iMe6dVhFnmOZdgac3MRmfqVpZ0AIlggWZ+l6+5rOGckXAsJ8i+mvPm4YuRQYDTHiJhIauagX4Q=",
"userHandle":"YzhhMzJlNWItNDZkMy00ODA4LWFlMTAtMTZkM2UyNmZmNmY5","signatureCounter":0,"createdAt":"2023-04-21T13:33:50.0764103","aaGuid":"adce0002-35bc-c60a-648b-0b25f1f05503","lastUsedAt":"2023-04-21T13:33:50.0764103","rpid":"myapp.example.com","origin":"https://myapp.example.com","country":"US","device":"Chrome, Mac OS X 10","nickname":"Fred's Macbook Pro 2","userId":"c8a32e5b-46d3-4808-ae10-16d3e26ff6f9" } //, ...]
向 /credentials/delete 端点发出的 POST 请求会删除与用户关联的特定凭证(由 credentialId 指定)。该请求必须包含相关的 credentialId,例如:
POST https://v4.passwordless.dev/credentials/delete HTTP/1.1ApiSecret:myapplication:secret:11f8dd7733744f2596f2a28544b5fbc4Content-Type:application/json{"credentialId":"qgB2ZetBhi0rIcaQK8_HrLQzXXfwKia46_PNjUC2L_w"}
POST https://v4.passwwordless.dev/magic-links/send HTTP/1.1ApiSecret:myapplication:secret:11f8dd7733744f2596f2a28544b5fbc4Content-Type:application/json{"emailAddress":"user-email@example.com","urlTemplate":"https://www.myapp.com?token=$TOKEN""userId": "c8a32e5b-46d3-4808-ae10-16d3e26ff6f9""timeToLive": 3600}