ss_token 传输时的安全性。
第四部分: 使用 Bearer access_token 的调用 api
1. 使用 refresh_token 换取 access_token: curl -u testclient:testpass www.yii.com/oauth2/index.php?r=oauth2/token -d "grant_type=refresh_token&refresh_token=1ce1a52dff3b5ab836ae25714c714cb86bf31b6f"
返回: {"access_token":"50540a7ead3a27cdb458b6cdc38df25f64da18f1", "expires_in":3600, "token_type":"bearer", "scope":null} 这里没有新的 refresh_token,需要进行配置以重新获取 refresh_token,可修改 OAuth2/GrantType/RefreshToken.php 中的 RefreshToken class __construct 方法中的 'always_issue_new_refresh_token' => true 来开启颁发新的 refresh_token。 Tips: IETF rfc2649 中对于 refresh_token section 的部分说明, 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
需要提供客户端的 client_id 和 client_secret, grant_type 值必须是 refresh_token。 access_token 有效期内不能使用 refresh_token 换取新的 access_token。
2. 使用 access_token: a. client app 使用 access_token 获取 resource 信息。 oauth2-server 验证 access_token: curl www.yii.com/oauth2/index.php?r=oauth2/verifytoken -d 'access_token=aea4a1059d3194a3dd5e4117bedd6e07ccc3f402'
返回: {"result":"success", "message":"your access token is valid." } 这个部分只是为了验证 access token 的有效性,client app 并不应该直接调用该方法,而是在请求资源时有server自行调用,根据判断结果进行不同处理。 可以在 Oauth2 extension 的 Server.php 中来修改 access_token 的有效期。
3. scope scope 需要服务端确定具体的可行操作。 scope 用来确定 client 所能进行的操作权限。项目中操作权限由 srbac 进行控制, Oauth2 中暂不做处理。
4. state state 为 client app 在第一步骤中获取 authorization code 时向 OAuth2 Server 传递并由 OAuth2 Server 返回的随机哈希参数。state 参数主要用来防止跨站点请求伪造(Cross Site Request Forgery, CSRF),相关讨论可参见本文最后的参考【7】和【8】。
References:
|