OAuth2介绍

2020-07-26  本文已影响0人  随风摇摆水杉树

一、介绍

OAuth2是一个授权框架,使应用程序能够访问其它公司提供的资源,比如腾讯、阿里、华为等公司的开放平台。OAuth2为Web、桌面程序、移动程序提供了一整套的授权流程。

二、OAuth角色

OAuth一共定义了4个角色:

三、协议流程

协议流程图.png

1、client请求resource owner授予权限。
2、resource owner同意授权,并且将授权许可证颁发给client
3、client通过身份验证和授权许可证来向authorization server请求access token
4、authorization server在验证client身份和授权许可证有效后,向client颁发access token
5、client通过access token访问resource server上受保护的资源。
6、resource server在验证access token的有效性后,返回受保护的资源。
以上流程是一个基本的授权访问流程,不过根据授权方式不同流程会有差异。下面将介绍OAuth2的4种授权方式。

四、OAuth2授权模式

OAuth2一共定义了4种授权模式,另外还定义了一个机制用来定义额外的授权模式。这里主要介绍4种基本的授权模式:

Authorization Code
Authorization Code Grant.png

(A) client通过用户代理将用户引导至授权页面
(B)authorization server验证resource owner身份,并且确定resource owner是授予还是拒绝clientaccess请求
(C)如果resource owner允许client访问,authorization server将用户代理重定向到client请求时提供的redirection URI,同时附上authorization code
(D)client使用authorization code,向authorization servertoken endpoint请求access token。请求时可以附带(C)中的redirection URI(用于验证你提交的URI跟client注册的URI是否一致)
(E)authorization server验证client身份和authorization code以及redirection URI。如果验证有效,authorization server返回access tokenrefresh token(可选项)

Implicit
Implicit Grant.png

(A) client通过用户代理将用户引导至授权页面
(B)authorization server验证resource owner身份,并且确定resource owner是授予还是拒绝clientaccess请求
(C)如果resource owner允许client访问,authorization server将引导用户代理重定向到client请求时提供的redirection URI,同时附上URI fragmentURI fragment包括了access token
(D)用户代理通过向虚拟主机(www、FTP、Email等空间服务)请求client资源(客户端自己的资源)来进行重定向
(E)虚拟主机的client资源返回一个web页面,该页面可以访问完整的redirection URI以及包含了access tokenURI fragment
(F)用户代理运行web页面中的script脚本,用来提取URI fragment中的access token
(G)用户代理将access token发送给client

Resource Owner Password Credentials
Resource Owner Password Credentials Grant.png

(A)resource ownerclient提供账号和密码
(B)client通过resource owner的账号和密码来向authorization server请求获取access token
(C)authorization server在验证了client身份以及resource owner的账号和密码有效后,向client颁发access token

Client Credentials Grant
Client Credentials Grant.png

(A)client通过authorization server进行身份验证,并请求access token
(B)authorization server在验证了client身份有效后,向client颁发access token

五、参考资源

上一篇 下一篇

猜你喜欢

热点阅读