###Open Authorizationjson
是——标准受权协议后端
解决——之前使用的受权方法容易形成信息泄露,不安全,须要不向第三方泄漏数据,容许第三方访问服务器中的数据浏览器
作什么——给第三方客户端有时效的token,第三方拿token令牌访问资源安全
经常使用于——第三方登陆bash
传统受权方式:服务器
复制资源拥有者的凭据直接使用app
向用户索要凭据dom
开发者密钥,万能钥匙学习
特殊密码令牌ui
1.Client 客户端 (使用受保护资源的API)
2.Resource Owner资源拥有者
3.Authorization Server 受权服务器
4.Resource Server 资源服务器
5.User Agent 用户代理,浏览器
我去银行ATM机取钱为例子
我就是resource owner
银行的金库就是resource server
ATM机子是client
我输入密码以后验证我帐号密码对不对的服务器是authorization server
ATM机内嵌的系统是user agent
+--------+ +---------------+
| |--(A)- Authorization Request ->| Resource |
| | | Owner |
| |<-(B)-- Authorization Grant ---| |
| | +---------------+
| |
| | +---------------+
| |--(C)-- Authorization Grant -->| Authorization |
| Client | | Server |
| |<-(D)----- Access Token -------| |
| | +---------------+
| |
| | +---------------+
| |--(E)----- Access Token ------>| Resource |
| | | Server |
| |<-(F)--- Protected Resource ---| |
+--------+ +---------------+
复制代码
Authorization Code Flow受权码模式
服务器与客户端配合
1.用户访问客户端,选择第三方登陆
2.客户端重定向到受权服务器,并携带client_id
GET {Authorization Endpoint}
?response_type=code // - Required
&client_id={Client ID} // - Required
&redirect_uri={Redirect URI} // - Conditionally required
&scope={Scopes} // - Optional
&state={Arbitrary String} // - Recommended
&code_challenge={Challenge} // - Optional
&code_challenge_method={Method} // - Optional
HTTP/1.1
HOST: {Authorization Server}
复制代码
示例:
https://your_domain/login?
response_type=code& // response_type为code时表示是受权码请求
client_id=your_app_client_id& //客户端id
redirect_uri=your_callback_url //经过客户端注册的重定向 URI
复制代码
3.用户确认受权(Authentication),客户端被重定向到给定的URI,并携带code
HTTP/1.1 302 Found
Location: {Redirect URI}
?code={Authorization Code} // - Always included
&state={Arbitrary String} // - Included if the authorization
// request included 'state'.
复制代码
示例:
https://www.example.com/?code=95157e76-7f22-40b7-81f6-77b27be91f19
复制代码
4.重定向过程当中,客户端拿到code,client_id,client_serect请求令牌,若是成功,去资源服务器获取资源
token request:
POST {Token Endpoint} HTTP/1.1
Host: {Authorization Server}
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code // - Required
&code={Authorization Code} // - Required
&redirect_uri={Redirect URI} // - Required if the authorization
// request included 'redirect_uri'.
&code_verifier={Verifier} // - Required if the authorization
// request included
// 'code_challenge'.
复制代码
5.客户端拿到token后,就能够请求资源了
token response:
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
Cache-Control: no-store
Pragma: no-cache
{
"access_token": "{Access Token}", // - Always included
"token_type": "{Token Type}", // - Always included
"expires_in": {Lifetime In Seconds}, // - Optional
"refresh_token": "{Refresh Token}", // - Optional
"scope": "{Scopes}" // - Mandatory if the granted
// scopes differ from the
// requested ones.
}
复制代码
Implicit Flow 隐式模式
用于移动应用程序或者Web应用程序
直接在浏览器中请求token,没有受权码
要求用户受权应用程序,而后受权服务器将访问令牌传回给用户代理,用户代理将其传递给客户端
1.用户访问客户端,选择第三方登陆
2.客户端重定向到受权服务器,发起请求
GET {Authorization Endpoint}
?response_type=token // - Required
&client_id={Client ID} // - Required
&redirect_uri={Redirect URI} // - Conditionally required
&scope={Scopes} // - Optional
&state={Arbitrary String} // - Recommended
HTTP/1.1
HOST: {Authorization Server}
复制代码
https://your_domain/login?response_type=token&client_id=your_app_client_id&redirect_uri=your_callback_url
复制代码
3.受权服务器响应给用户代理,其中包含access_token
HTTP/1.1 302 Found
Location: {Redirect URI}
#access_token={Access Token} // - Always included
&token_type={Token Type} // - Always included
&expires_in={Lifetime In Seconds} // - Optional
&state={Arbitrary String} // - Included if the request
// included 'state'.
&scope={Scopes} // - Mandatory if the granted
// scopes differ from the
// requested ones.
复制代码
4.用户代理提取转发令牌token给客户端
5.客户端拿到token请求资源信息
Resource Owner Password credentials Flow 密码模式
用户把帐号密码直接提供给客户端
客户端不能存储密码,对客户端高度信任的状况下,其余受权模式没法使用的状况下
https://oauth.example.com/token?grant_type=password&
username=USERNAME&
password=PASSWORD&
client_id=CLIENT_ID
复制代码
{
"access_token" : "",
"token_type" : "",
"expires_in" : "",
"refresh_token" : "",
}
复制代码
Client Credentials 客户端模式。 适用于后端API的操做
这种模式只须要提供 client_id
和 client_secret
便可获取受权。通常用于后端 API 的相关操做。
https://oauth.example.com/token?
grant_type=client_credentials&
client_id=CLIENT_ID&
client_secret=CLIENT_SECRET
复制代码
Refresh Token Flow
访问时token过时,则须要更新令牌获取新的令牌
客户端发出更新令牌的HTTP请求,包含如下参数:
OAuth2: www.oauth.com/oauth2-serv… www.ory.sh/oauth2-for-… medium.com/@darutk/dia… medium.com/@darutk/ful…