npm install openid-client
// 发布者
import { Issuer } from 'openid-client';// 发布者地址
const googleIssuer = await Issuer.discover('');
console.log('Discovered issuer %s %O', googleIssuer.issuer, adata);
const client = new googleIssuer.Client({client_id: 'xxxxxx', // 客户端idclient_secret: 'xxxxxxx', // 客户端密码redirect_uris: ['localhost:3000/cb'], // 回调地址response_types: ['code'], // 响应类型code、token、id_token、id_token token、code id_token、code token、code id_token token// id_token_signed_response_alg (default "RS256") // token_endpoint_auth_method (default "client_secret_basic")
});
import { generators } from 'openid-client';
const code_verifier = deVerifier();
// 如果是基于cookie的解决方案,则将code_verifier存储在框架的会话机制中
// 它应该是httpOnly(不能被javascript读取)并且加密。
const code_challenge = deChallenge(code_verifier);client.authorizationUrl({scope: 'openid email profile',resource: '', // 可不写code_challenge,code_challenge_method: 'S256',
});
const params = client.callbackParams(req);
const tokenSet = await client.callback('', params, { code_verifier });
console.log('received and validated tokens %j', tokenSet);
console.log('validated ID Token claims %j', tokenSet.claims());
const userinfo = await client.userinfo(access_token);
console.log('userinfo %j', userinfo);
const tokenSet = fresh(refresh_token);
console.log('refreshed and validated tokens %j', tokenSet);
console.log('refreshed ID Token claims %j', tokenSet.claims());
openid-client处理起来简单方便,适合koa/express为服务器的前端接口项目。使用过程中,遇到问题请留意或私信。
本文发布于:2024-02-04 09:09:13,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170703779954246.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |