当前位置:网站首页>[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 .
边栏推荐
- C language: random generated number + merge sort
- Paddleclas classification practice record
- I miss the year of "losing" Li Ziqi
- 图的遍历(BFS&&DFS基础)
- JWT login authentication + token automatic renewal scheme, well written!
- Leetcdoe-342. Power of 4
- powerdesigner创建数据库模型(概念模型举例)
- Is azvudine, a domestic oral new coronal drug, safe? Expert authority interpretation
- I'm bald! Who should I choose for unique index or general index?
- SAP ui5 fileuploader control realizes local file upload, and trial version of cross domain access error encountered when receiving server-side response
猜你喜欢

【安全】 阅读 RFC6749 及理解 Oauth2.0 下的授权码模式

Chapter 6 promotion

Night God simulator packet capturing wechat applet

Continuous (integration -- & gt; delivery -- & gt; deployment)

我秃了!唯一索引、普通索引我该选谁?

少儿编程 电子学会图形化编程等级考试Scratch二级真题解析(判断题)2022年6月
![[dark horse morning post] byte valuation has shrunk to $270billion;](/img/58/8d5c78d919ed60bc833ec4daa22e23.jpg)
[dark horse morning post] byte valuation has shrunk to $270billion; "Second uncle" video author responded to plagiarism; Renzeping said that the abolition of the pre-sale system of commercial housing

Rust from introduction to mastery 01 introduction

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

今日睡眠质量记录75分
随机推荐
POJ3275 Ranking the Cows题解
DDoS protection with iptables
Chapter 6 promotion
Leetcode notes 566. Reshaping the matrix
Debezium系列之:2.0.0.Beta1的重大变化和新特性
微信小程序中自定义模板
Realize the mutual value transfer between main window and sub window in WPF
Jar package
倒计时 2 天!2022 中国算力大会:移动云邀您共见算力网络,创新发展
Map tiles: detailed explanation of vector tiles and grid tiles
Leetcdoe-342. Power of 4
.NET的求复杂类型集合的差集、交集、并集
验证码暴力破解测试[通俗易懂]
I'm bald! Who should I choose for unique index or general index?
My friend sent me some interview questions
POJ3259虫洞题解
二舅能治好年轻人的精神内耗吗?
持续(集成--&gt;交付--&gt;部署)
.NET桌面开发的一些思考
POJ1860货币兑换题解