openid

阅读: 评论:0

openid

openid

1、安装:

npm install openid-client

2、增加发布者:

// 发布者
import { Issuer } from 'openid-client';// 发布者地址
const googleIssuer = await Issuer.discover('');
console.log('Discovered issuer %s %O', googleIssuer.issuer, adata);

3、配置客户端:

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")   
}); 

4、认证地址:

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',
});

5、回调获取token

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());

6、获取userinfo

const userinfo = await client.userinfo(access_token);
console.log('userinfo %j', userinfo);

7、刷新token

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小时内删除。

标签:openid
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23