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, googleIssuer.metadata);

3、配置客户端:

const client = new googleIssuer.Client({client_id: 'xxxxxx',                              // 客户端idclient_secret: 'xxxxxxx',                     // 客户端密码redirect_uris: ['http://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 = generators.codeVerifier();
// 如果是基于cookie的解决方案,则将code_verifier存储在框架的会话机制中
// 它应该是httpOnly(不能被javascript读取)并且加密。
const code_challenge = generators.codeChallenge(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 = await client.refresh(refresh_token);
console.log('refreshed and validated tokens %j', tokenSet);
console.log('refreshed ID Token claims %j', tokenSet.claims());

总结

openid-client处理起来简单方便,适合koa/express为服务器的前端接口项目。使用过程中,遇到问题请留意或私信。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1731034159a3601094.html

相关推荐

  • openid

    10月前
    90
  • 微信公众号h5网页获取授权openId

    文章目录前言一、准备一个页面用于授权获取code2.在首页使用code进行授权登录前言 在微信公众号h5网页里面获取授权,我是用的是uniapp框架,主要讲一下前端的操作,

    1月前
    210
  • 【微信开发】免费域名—网页授权获取用户openid

    最近在接触微信支付开发,要进行微信支付就需要用户的唯一标识——openid,因为第一次接触踩了很多坑,于是就把他记录下来,也便于以后查阅&#xf

    1月前
    280

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信