oauth2.0使用介绍 oauthlib使用介绍 authlib使用介绍 pyjwt使用介绍

2025-11-15 23:19:00
admin
原创 317
摘要:oauth2.0使用介绍 oauthlib使用介绍 authlib使用介绍 pyjwt使用介绍

一、oauth2.0使用介绍

1、oauth规范:https://datatracker.ietf.org/doc/html/rfc6749

2、pkce规范:https://datatracker.ietf.org/doc/html/rfc7636

3、设备授权:https://datatracker.ietf.org/doc/html/rfc8628

4、token吊销:https://datatracker.ietf.org/doc/html/rfc7009

5、token内省:https://datatracker.ietf.org/doc/html/rfc7662

6、metadata:https://datatracker.ietf.org/doc/html/rfc8414

7、http认证:https://datatracker.ietf.org/doc/html/rfc2617

8、bearer认证:https://datatracker.ietf.org/doc/html/rfc6750

9、代码相关资源:https://oauth.net/code


oauth授权方式:

1、Authorization Code:授权码模式,用户授权才能访问资源,设备有浏览器,返回access_token,返回refresh_token;

2、Device Authorization:设备码模式,用户授权才能访问资源,设备没有浏览器,返回access_token,返回refresh_token;

3、Client Credentials:客户端凭证模式,无需用户授权访问资源,返回access_token,不返回refresh_token


oidc使用介绍:

1、oidc规范:https://openid.net/specs/openid-connect-core-1_0.html

2、oidc服务发现:https://openid.net/specs/openid-connect-discovery-1_0.html

3、多个响应类型:https://openid.net/specs/oauth-v2-multiple-response-types-1_0.html

4、认证策略扩展:https://openid.net/specs/openid-provider-authentication-policy-extension-1_0.html

5、认证方法引用:https://datatracker.ietf.org/doc/html/rfc8176

6、语言标签列表:https://datatracker.ietf.org/doc/html/rfc5646

7、oidc认证用户成功,返回一个签名的id_token,包含认证用户的信息;

8、id_token必填字段:iss签发者、sub用户标识、aud客户端ID、exp过期时间、iat签发时间;

9、id_token可选字段:auth_time认证时间、nonce随机值、acr认证等级、amr认证方法、azp认证客户端;


二、oauthlib使用介绍

1、oauthlib代码仓库:https://github.com/oauthlib/oauthlib

2、oauthlib代码仓库:https://github.com/requests/requests-oauthlib

3、oauthlib帮助文档:https://oauthlib.readthedocs.io/en/latest

4、oauthlib帮助文档:https://requests-oauthlib.readthedocs.io/en/latest

5、oauthlib安装:pip install oauthlib

6、oauthlib安装:pip install requests requests-oauthlib

7、OAuth2Session.fetch_token会根据expires_in计算expires_at

8、OAuth2Session.refresh_token会根据expires_in计算expires_at

09、服务端验证器:服务端需要实现RequestValidator,用于验证客户端的请求;

10、服务端验证器示例:https://github.com/oauthlib/oauthlib/blob/master/examples/skeleton_oauth2_web_application_server.py

11、协议外自定义验证:RequestValidator之外增加额外校验,pre_auth, post_auth,pre_token, post_token

12、协议外自定义验证:https://oauthlib.readthedocs.io/en/latest/oauth2/grants/custom_validators.html

13、自定义授权:https://oauthlib.readthedocs.io/en/latest/oauth2/grants/custom_grant.html

14、自定义token:WebApplicationServer的token_generator用于自定义token生成器;


oauthlib授权码模式:

1、常用参数:client_id、client_secret、redirect_uri、scope、state

2、资源服务重定向到授权服务:OAuth2Session.authorization_url

3、授权服务返回授权码:Server.create_authorization_response

4、资源服务请求token:OAuth2Session.fetch_token

5、授权服务返回token:Server.create_token_response

6、资源服务携带token请求:OAuth2Session.get

7、资源服务携带token请求:OAuth2Session.post


oauthlib授权码模式+pkce:

1、常用参数:client_id、code_verifier、code_challenge、redirect_uri、scope、state

2、资源服务重定向到授权服务:OAuth2Session.authorization_url

3、授权服务返回授权码:Server.create_authorization_response

4、资源服务请求token:OAuth2Session.fetch_token

5、授权服务返回token:Server.create_token_response

6、资源服务携带token请求:OAuth2Session.get

7、资源服务携带token请求:OAuth2Session.post


oauthlib设备码模式:

1、常用参数:client_id、client_secret、device_code、user_code、verification_uri、scope

2、requests-oauthlib不支持设备码模式,客户端需要使用requests + oauthlib的DeviceClient自行实现;


oauthlib客户端凭证模式:

1、常用参数:client_id、client_secret、scope

2、资源服务请求token:OAuth2Session.fetch_token

3、授权服务返回token:Server.create_token_response

4、资源服务携带token请求:OAuth2Session.get

5、资源服务携带token请求:OAuth2Session.post


oauthlib支持oidc:

1、引入服务端:from oauthlib.openid import Server
2、引入验证器:from oauthlib.openid import RequestValidator

3、生成id_token:RequestValidator.get_id_token,需要生成所有字段,不建议使用这个方法;

4、生成id_token:RequestValidator.finalize_id_token,已经生成部分字段,只需补充额外字段;

5、刷新id_token:RefreshTokenGrant调用RequestValidator.refresh_id_token;

6、获取用户信息:UserInfoEndpoint调用RequestValidator.get_userinfo_claims;

7、获取资源校验(bearer token):ResourceEndpoint调用RequestValidator.validate_bearer_token;

8、获取资源校验(jwt token):ResourceEndpoint调用RequestValidator.validate_jwt_bearer_token;


三、authlib使用介绍

1、authlib代码仓库:https://github.com/authlib/authlib

2、authlib帮助文档:https://docs.authlib.org/en/stable

3、joserfc帮助文档:https://jose.authlib.org/en/guide/jwt

4、安装依赖:pip install Authlib,pip install joserfc


客户端使用介绍:

1、客户端使用介绍:https://docs.authlib.org/en/stable/oauth2/client/http/index.html

2、客户端认证方法:https://docs.authlib.org/en/stable/oauth2/client/http/index.html#client-authentication

3、客户端认证方法:client_secret_basic、client_secret_post,如果提供client_secret,默认是client_secret_basic


四、pyjwt使用介绍

1、jwt在线规范:https://datatracker.ietf.org/doc/html/rfc7519

2、jwt公共字段:https://www.iana.org/assignments/jwt/jwt.xhtml

3、pyjwt代码仓库:https://github.com/jpadilla/pyjwt

4、pyjwt帮助文档:https://pyjwt.readthedocs.io/en/stable

5、jwt格式:base64(header).base64(payload).base64(sign)

6、jwt字段:注册字段、公共字段、私有字段;

7、jwt字段:注册字段是在规范里面的公共字段;

8、安装依赖:pip install pyjwt


使用详解:

1、无校验获取头部:jwt.get_unverified_header(encoded)

2、无校验解码:jwt.decode(encoded, options={"verify_signature": False})

3、校验解码(返回payload):jwt.decode(encoded, key, algorithms="HS256")

4、校验解码(返回全部):jwt.decode_complete(encoded, key, algorithms="HS256")


计算哈希:

algo = jwt.get_algorithm_by_name("HS256")
hash = algo.compute_hash_digest("data".encode())
print(hash)

发表评论
评论通过审核之后才会显示。