oauth2.0使用介绍 oauthlib使用介绍 authlib使用介绍 pyjwt使用介绍
- 2025-11-08 23:19:00
- admin
- 原创 172
一、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
oauth授权方式:
1、Authorization Code:授权码模式,用户授权才能访问资源,设备有浏览器,返回access_token,返回refresh_token;
2、Device Authorization:设备码模式,用户授权才能访问资源,设备没有浏览器,返回access_token,返回refresh_token;
3、Client Credentials:客户端凭证模式,无需用户授权访问资源,返回access_token,不返回refresh_token;
二、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
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
三、authlib使用介绍
1、authlib代码仓库:https://github.com/authlib/authlib
2、authlib帮助文档:https://docs.authlib.org/en/stable
3、authlib安装:pip install Authlib
四、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)