当前位置:网站首页>[security] read rfc6749 and understand the authorization code mode under oauth2.0
[security] read rfc6749 and understand the authorization code mode under oauth2.0
2022-07-28 13:45:00 【Ch.yang】
1. Preface
OAuth 2.0 Norms come from RFC6749. I saw 《Spring Micro service practice 》 Yes OAuth 2.0 After the introduction of, I still feel that there are some translation problems . Now combine RFC6749 Let's sort it out again .
1.1. A scene :o-stock Get wechat avatars .
- All the conclusive things come from this document .
- This paper mainly takes obtaining wechat avatar as a model , Yes
Oauth2.0Sort out and understand the knowledge points . - Focus on authorization code authorization mode .
- borrow 《Spring Micro service practice 》 The project name
o-stock( CorrespondingOauth2.0Mediumclient)
1.2. This article will not be translated in Chinese , If you need to see
2. OAuth 2.0 Authorization code mode Natural for Web Back end technology stack
RFC6749 Definition OAuth 2.0 At present, it is only established in HTTP Agreement on . And mentioned it many times user-agen It can be played by the browser , The authorization code mode is very suitable for those with independent servers Web The server .
2.1. OAuth 2.0 What problem does the authorization code mode solve ?
from o-stock Look at
- Keep new users , New customers may not want to
o-stockregister , But there may be an impulse to use wechat to log in .
from WeChat From the perspective of
- More developers choose wechat , Then the potential user stickiness increases , But wechat needs to be maintained
authorization serverandresource serverRole , Quite a few io The cost of .
From the perspective of industry norms
- The server does not need the user's wechat user name 、 password , User information is more secure .
- It is a proven and reliable process , Developers don't have to build wheels again .
2.2. Key points of authorization code mode
- The WeChat
authorization serverIt is impossible to give allo-stockProvide the function of three-party login , Then asko-stockOn wechatauthorization serverRegistered on - RFC6749 Regulations , Authorization code mode ,
o-stockUnable to save the user's wechat password , Insteadauthorization servertowardso-stockIssued bycode. o-stockNeed to be on wechatauthorization serverHold login credentials ( Must letauthorization serverKnow yourself ), close code And login client login credentials , requestauthorization serverYou can getaccess_tokenaccess_tokenIt is the final product of replacing wechat password , This token can be used ino-stockpreservation ,o-stockIt can be used to get the user's Avatar .
3. RFC6749 Unspecified antecedents
o-stockNeed to be on wechatauthorization serverTo register (ipad Write on , Due to the membrane , It's sloppy )
4. Take the browser as the starting point to understand the authorization activities of wechat
Pull the picture in the document , Now the user-agen As a browser
+----------+
| Resource |
| Owner |
| |
+----------+
^
|
(B)
+----|-----+ Client Identifier +---------------+
| -+----(A)-- & Redirection URI ---->| |
| User- | | Authorization |
| Agent -+----(B)-- User authenticates --->| Server |
| | | |
| -+----(C)-- Authorization Code ---<| |
+-|----|---+ +---------------+
| | ^ v
(A) (C) | |
| | | |
^ v | |
+---------+ | |
| |>---(D)-- Authorization Code ---------' |
| Client | & Redirection URI |
| | |
| |<---(E)----- Access Token -------------------'
+---------+ (w/ Optional Refresh Token)
Note: The lines illustrating steps (A), (B), and (C) are broken into
two parts as they pass through the user-agent.
Figure 3: Authorization Code Flow
4.1. User entrusts browser , Initiate authentication to wechat GET request

Note: RFC6749 There is no specific implementation of the standard black line , However, specific requirements for red line submission parameters are specified
- The red lines can be compared with the examples given on the official website
The authorization request :(/authorize It's on wechat url)
GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
Host: server.example.com
4.2. Wechat confirms identity , Entrust the browser to issue code ( Follow 4.3. Merge into a picture )
- The browser uses redirection to tell o-stock The user's authorization request was accepted by wechat , use code It means the evidence admitted this time .
Authorization response :( Wechat perceives that the user agrees to authorize , Take advantage of the browser's ability to redirect to the server code)
HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSbIA&state=xyz

4.3. Wechat received o-stock apply access_token Request , Issue after authentication and request entry
Authorization token request :
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb
Worth mentioning Authorization: Basic
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
That is, wechat given in the first picture above token, This needs to be o-stock Store and maintain by yourself . Many materials on the Internet say that you should carry client_id and client_secret, But the code on the official website obviously does not . as a result of czZCaGRSa3F0MzpnWDFmQmF0M2JW Wechat has been able to recognize that the request is o-stock Sponsored , And the specification also describes the authorization code mode ,client If it has been certified, it may not be provided client_id , Say nothing of client_secret
Authorization token response :
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token":"2YotnFZFEjr1zCsicMWpAA",
"token_type":"example",
"expires_in":3600,
"refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA",
"example_parameter":"example_value"
}

4.4. access_token Renewal
Token renewal :
POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
5. Postscript
thus ,RFC6749 The authorization process of has been explained . Use access_token Go to wechat resource server You can get the avatar . The whole authorization process may not include resource server ,resource server Follow up with o-stock Dealing with .o-stock Can be stored access_token . Based on Oauth2.0 It also expands a OIDC For enhancement authentication technological process (RFC6749 Didn't explain the certification process ), This specification can realize single sign on , here o-stock Storage OIDC Under the norm id_token It can be regarded as a better implementation , Later, I figured out my thoughts and wrote a blog record of single sign on .
边栏推荐
- 【ECMAScript6】Promise
- P1797重型运输 题解
- Volcanic stone investment Zhang Suyang: hard technology, the relatively certain answer in the next 10 years
- Jenkins -- continuous integration server
- SQL每日一练(牛客新题库)——第4天:高级操作符
- DDoS protection with iptables
- 剖析 kubernetes 集群内部 DNS 解析原理
- 力扣 2354. 优质数对的数目
- 少儿编程 电子学会图形化编程等级考试Scratch二级真题解析(判断题)2022年6月
- 我秃了!唯一索引、普通索引我该选谁?
猜你喜欢

用非递归的方法实现二叉树中的层遍历,先序遍历,中序遍历和后序遍历

DDoS protection with iptables

拒绝服务 DDoS 攻击

gicv3 spi register

面经整理,助力秋招,祝你称为offer收割机

Leetcdoe-342. Power of 4

Three men "running away" from high positions in the mobile phone factory
![[error] after logging in to another machine using SSH, you find that the hostname is still yourself | unable to access yarn8088](/img/81/641a5b3445534fc3b8c87ee6deaa64.png)
[error] after logging in to another machine using SSH, you find that the hostname is still yourself | unable to access yarn8088

jar包

Blue Bridge Training (additional interview questions) day 7
随机推荐
验证码暴力破解测试[通俗易懂]
【架构】评分较高的三本微服务书籍的阅读笔记
今日睡眠质量记录75分
UVA11175有向图D和E From D to E and Back题解
力扣 剑指 Offer 51. 数组中的逆序对
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
[ecmascript6] function and its related use
Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022
微念“失去”李子柒的这一年
基于神经网络的帧内预测和变换核选择
Half wave rectification light LED
Auto.js enables Taobao to quickly submit orders
Guide for using IP phone system and VoIP system
Use non recursive method to realize layer traversal, preorder traversal, middle order traversal and post order traversal in binary tree
C语言:优化后的归并排序
SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版
111. SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误
DDoS protection with iptables
图的遍历(BFS&&DFS基础)
Dry goods -- encapsulated anti shake and throttling method in the project