OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版。目前Auth2.0已经得到了广泛应用,比如微信登录、微博、QQ等。
(5)只要有一个第三方应用程序被破解,就会导致用户密码泄漏,以及所有被密码保护的数据泄漏。
OAuth就是为了解决上面这些问题而诞生的。
关于OAuth2.0协议的授权流程可以参考下面的流程图
其中Client指第三方应用,Resource Owner指用户,Authorization Server是我们的授权服务器,Resource Server是API服务器。
解释一下上述流程:
(A)用户打开客户端以后,客户端要求用户给予授权。
(B)用户同意给予客户端授权。
(C)客户端使用上一步获得的授权,向认证服务器申请令牌。
(D)认证服务器对客户端进行认证以后,确认无误,同意发放令牌。
(E)客户端使用令牌,向资源服务器申请获取资源。
(F)资源服务器确认令牌无误,同意向客户端开放资源。
客户端必须得到用户的授权(authorization grant),才能获得令牌(access token)。OAuth 2.0定义了四种授权方式。
授权码模式(authorization code)
简化模式(implicit)
密码模式(resource owner password credentials)
客户端模式(client credentials)
授权码模式(authorization code)是功能最完整、流程最严密的授权模式。
它的步骤如下:
(A)用户访问客户端,后者将前者导向认证服务器。
(B)用户选择是否给予客户端授权。
(C)假设用户给予授权,认证服务器将用户导向客户端事先指定的"重定向URI"(redirection URI),同时附上一个授权码。
(D)客户端收到授权码,附上早先的"重定向URI",向认证服务器申请令牌。这一步是在客户端的后台的服务器上完成的,对用户不可见。
(E)认证服务器核对了授权码和重定向URI,确认无误后,向客户端发送访问令牌(access token)和更新令牌(refresh token)。
下面是上面这些步骤所需要的参数。
A步骤中,客户端申请认证的URI,包含以下参数:
response_type:表示授权类型,必选项,此处的值固定为"code"
client_id:表示客户端的ID,必选项
redirect_uri:表示重定向URI,可选项
scope:表示申请的权限范围,可选项
state:表示客户端的当前状态,可以指定任意值,认证服务器会原封不动地返回这个值。
下面是一个例子。
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
C步骤中,服务器回应客户端的URI,包含以下参数:
HTTP/1.1 302 Found
Location: https://client.example.com/cb?code=SplxlOBeZQQYbYS6WxSbIA
&state=xyz
D步骤中,客户端向认证服务器申请令牌的HTTP请求,包含以下参数:
下面是一个例子。
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
下面是一个例子。
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"
}
Refresh Token 是 Access Grants 的一种,在获取 Access Token 时,认证服务器将返回相应的 Refresh Token,如果 Access Token 过期,就可以用 Refresh Token 去刷新。
一般针对移动应用时,在返回access_token时会返回refresh_token。refresh_token 也是有有效期的,当refresh_token失效的后,需要用户重新授权。
refresh_token失效后,可重新获取。
首选需要用户授权,获取code后获取refresh_token,包含参数:
client_id:表示客户端的ID,必填项
grant_type:固定值 ,填refresh_token , 必填项
refresh_token:填写通过access_token获取到的refresh_token参数,必填项。
以微信登录为例,整个认证授权流程如下:
1、打开授权页面
GET https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4afd02a0d14d2e89&redirect_uri=wx4afd02a0d14d2e89%3A%2F%2Foauth&response_type=code&scope=snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact&state=none&uin=MTA1MjE4MTQwMA%3D%3D&lang=zh_CN&key=08dc53d6f0880cc5b4052d374032c26103e315cb2a2fc114b4b591df260aa205ca88a52dc3fef5cfd6e61bdea39e16c3&pass_ticket=2wuB7BcROVt5mIoS4BI06jZ%2B970bXK6Iv4N9JWg6S9mlznLKwlEjhW3sKCyKZEeZ HTTP/1.1
参数说明:
appid:wx4afd02a0d14d2e89
redirect_uri:wx4afd02a0d14d2e89://oauth
response_type:code
scope:snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact
state:none
uin:MTA1MjE4MTQwMA==
lang:zh_CN
key:08dc53d6f0880cc5b4052d374032c26103e315cb2a2fc114b4b591df260aa205ca88a52dc3fef5cfd6e61bdea39e16c3
pass_ticket:2wuB7BcROVt5mIoS4BI06jZ+970bXK6Iv4N9JWg6S9mlznLKwlEjhW3sKCyKZEeZ
2、点击登录 获取code
一个code只能成功获取一次AccessToken
POST https://open.weixin.qq.com/connect/oauth2/authorize_reply HTTP/1.1
Host: open.weixin.qq.com
Connection: keep-alive
Q-UA2: QV=3&PL=ADR&PR=WX&PP=com.tencent.mm&PPVN=6.3.30&TBSVC=36842&CO=BK&COVC=036872&PB=GE&VE=GA&DE=PHONE&CHID=0&LCID=9422&MO= MI3W &RL=1080*1920&OS=6.0.1&API=23
Q-GUID: 24ee875dc809037e90eaa4ea13b788cb
Q-Auth: 31045b957cf33acf31e40be2f3e71c5217597676a9729f1b
Content-Length: 288
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/wxpc,*/*;q=0.8
Origin: https://open.weixin.qq.com
User-Agent: Mozilla/5.0 (Linux; Android 6.0.1; MI 3W Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.8 TBS/036872 Safari/537.36 MicroMessenger/6.3.30.920 NetType/WIFI Language/zh_CN
Content-Type: application/x-www-form-urlencoded
Referer: https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4afd02a0d14d2e89&redirect_uri=wx4afd02a0d14d2e89%3A%2F%2Foauth&response_type=code&scope=snsapi_message,snsapi_userinfo,snsapi_friend,snsapi_contact&state=none&uin=MTA1MjE4MTQwMA%3D%3D&lang=zh_CN&key=08dc53d6f0880cc5b4052d374032c26103e315cb2a2fc114b4b591df260aa205ca88a52dc3fef5cfd6e61bdea39e16c3&pass_ticket=2wuB7BcROVt5mIoS4BI06jZ%2B970bXK6Iv4N9JWg6S9mlznLKwlEjhW3sKCyKZEeZ
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,en-US;q=0.8
snsapi_userinfo=on&allow=%E7%A1%AE%E8%AE%A4%E7%99%BB%E5%BD%95&uuid=0315whxAUTCzUabF&uin=MTA1MjE4MTQwMA%3D%3D&key=08dc53d6f0880cc5b4052d374032c26103e315cb2a2fc114b4b591df260aa205ca88a52dc3fef5cfd6e61bdea39e16c3&pass_ticket=2wuB7BcROVt5mIoS4BI06jZ%2B970bXK6Iv4N9JWg6S9mlznLKwlEjhW3sKCyKZEeZ
snsapi_userinfo on
allow 确认登录
uuid 0315whxAUTCzUabF
uin MTA1MjE4MTQwMA==
key 08dc53d6f0880cc5b4052d374032c26103e315cb2a2fc114b4b591df260aa205ca88a52dc3fef5cfd6e61bdea39e16c3
pass_ticket 2wuB7BcROVt5mIoS4BI06jZ+970bXK6Iv4N9JWg6S9mlznLKwlEjhW3sKCyKZEeZ
响应
HTTP/1.1 301 Move Permanently
Connection: keep-alive
Location: wx4afd02a0d14d2e89://oauth?code=011vxUYC0OV5Xe2iFdZC0bOVYC0vxUYN&state=none
Content-Length: 0
重定义到redirect_uri?code=
3、获取access_token
GET https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx4afd02a0d14d2e89&secret=610afefcafe40a95b41314ed48868da2&code=011vxUYC0OV5Xe2iFdZC0bOVYC0vxUYN&grant_type=authorization_code HTTP/1.1
appid wx4afd02a0d14d2e89
secret 610afefcafe40a95b41314ed48868da2
code 011vxUYC0OV5Xe2iFdZC0bOVYC0vxUYN
grant_type authorization_code
User-Agent: Dalvik/2.1.0 (Linux; U; Android 6.0.1; MI 3W MIUI/V8.1.3.0.MXDCNDI)
Host: api.weixin.qq.com
Connection: Keep-Alive
Accept-Encoding: gzip
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/plain
Date: Wed, 23 Nov 2016 01:49:21 GMT
Content-Length: 377
{
"access_token":"lzfj7zopmtDA17aY8X_eueJz-aVCNPOdFV4veLmnpvddOQqSdpUtIIlLCPpIho6eXDe6jfNCMDtbkDzwtJeZyQ0CXzMS80SRIRIulh6x8qk",
"expires_in":7200,
"refresh_token":"K-kLkcG7xUJRrdp06os-YQlmzokEjIKMGaYm61EjMygPXsXqw30FJL6jpiEJTOnjAcXD9NzKjx9ke4ftqQAnmiIr2v7dp-QI3XXuLhlmooo",
"openid":"oPYxRwa6z4QWPAJn82a9fFZaKTKA",
"scope":"snsapi_userinfo",
"unionid":"odRDvvoL8q_FSMZ4gN3H54PHp2_E"
}
GET https://api.weixin.qq.com/sns/userinfo?access_token=lzfj7zopmtDA17aY8X_eueJz-aVCNPOdFV4veLmnpvddOQqSdpUtIIlLCPpIho6eXDe6jfNCMDtbkDzwtJeZyQ0CXzMS80SRIRIulh6x8qk&openid=oPYxRwa6z4QWPAJn82a9fFZaKTKA HTTP/1.1
access_token lzfj7zopmtDA17aY8X_eueJz-aVCNPOdFV4veLmnpvddOQqSdpUtIIlLCPpIho6eXDe6jfNCMDtbkDzwtJeZyQ0CXzMS80SRIRIulh6x8qk
openid oPYxRwa6z4QWPAJn82a9fFZaKTKA
User-Agent: Dalvik/2.1.0 (Linux; U; Android 6.0.1; MI 3W MIUI/V8.1.3.0.MXDCNDI)
Host: api.weixin.qq.com
Connection: Keep-Alive
Accept-Encoding: gzip
HTTP/1.1 200 OK
Connection: keep-alive
Content-Type: text/plain
Date: Wed, 23 Nov 2016 01:49:22 GMT
Content-Length: 350
{
"openid":"oPYxRwa6z4QWPAJn82a9fFZaKTKA",
"nickname":"小伙子",
"sex":1,"language":"zh_CN",
"city":"Chaoyang",
"province":"Beijing",
"country":"CN",
"headimgurl":"",
"privilege":[],
"unionid":"odRDvvoL8q_FSMZ4gN3H54PHp2_E"
}
Oauth2.0
新浪微博授权
微信授权
理解Auth2.0