OAuth2.0授权
这里图片展示下 OAuth 授权流程:
流程基本上简单来说就是:
- 客户端请求唤起地址, 需要跳转到第三方的授权页面
- 第三方授权页面等待玩家账号授权, 同意的时候根据
redirect_uri跳转回调服务器地址 - 回调到我们自己服务器的时候返回授权的
code和我们自己带的token用户标识 - 根据返回
code需要再去验证获取到accss_token会话凭据, 这里就是这次请求主要会话凭据 - 拿到
access_token相当于本次访问临时密码, 可以请求获取玩家信息:user( 主要获取唯一标识作为账号记录 ) - 现在获取
user相关信息, 提取当中在第三方做唯一标识的字段去数据库判断username, 不存在就帮助创建账号
这里就是常规 OAuth 验证流程, 但是在这个过程当中的处理方式有所不同:
- 接口复用, 请求采用
/login路径, 判断参数有没有带code字段识别, 如果有就直接在原接口进行玩家创建处理(会有接口代码堆叠情况, 单个接口堆叠大量逻辑). - 状态判断, 请求依旧采用
/login路径, 但是返回的是请求redis生成会话token, 客户端需要不断通过token检查/check接口状态( 分离化处理, 有的 PC 端需要另外打开网页来访问 ).
之前正式项目都是采用接口复用, 这种方式相对来比较简单, 如果本身是 Web 网页跳转的话;
但是有种例外情况, 那就是 PC 客户端打开网页授权, 这时候客户端是没办法知道网页变动的, 所以需要不断轮询监听指定 token 来确认状态.
这里两种情况按照自己需求变动, 如果是纯网页授权可以直接接口复用跳转到指定登录页, 如果是打算全平台登录授权则采用状态判断.
实现方式
这里以 bitbucket 登录做例子, 这种代码托管平台也支持 OAuth 做第三方登录.
上面文档说明了怎么创建生成应用.
- Select your avatar (Your profile) from the navigation bar at the top of the screen.
- Under
Recent workspaces, select the workspace that will be accessed using the consumer; or find and open the workspace underAll workspaces. - Select the
Settingscog on the top navigation bar. - Select
Workspace settingsfrom theSettingsdropdown menu. - On the sidebar, under
Apps and features, selectOAuth consumers. - Click the
Add consumerbutton.- The system requests the following information:
Name: The display name for your consumer. This must be unique within your account. This is required.Description: An optional description of what your consumer does.Callback URL: Required for OAuth 2.0 consumers.- When making requests you can include a call back URL in the request:
- If you do include the URL in a request it must be appended to the same URL configured in the consumer. So if your consumer callback URL is example.com/add-on the URL in your request must be something similar to example.com/add-on/function.
- If you don’t include the URL in the request we redirect to the callback URL in the consumer.
- URL: An optional URL where the curious can go to learn more about your cool application.
- Click Save. The system generates a key and a secret for you.
- Toggle the consumer name to see the generated
KeyandSecretvalue for your consumer.
以上就是如何生成应用, 主要最后获得应用的 Key 和 Secret, 这就是必须的.
之后就是关键的两个请求链接:
- 跳转第三方验证:
https://bitbucket.org/site/oauth2/authorize - 请求获取会话凭据:
https://bitbucket.org/site/oauth2/access_token - 获取玩家信息:
https://api.bitbucket.org/2.0/user
首先跳转第三方的授权页面:
# 这里的 client_id 就是应用获取的key
https://bitbucket.org/site/oauth2/authorize?client_id={client_id}&response_type=code
# 这里 GET 方式涵盖以下参数
# client_id(必须): 应用注册时候的 key
# response_type(必须): 第三方授权返回形式, 默认返回 `CALLBACK_URL?code=xxx` 方式
# redirect_uri(非必须): 如果没有该参数会默认读取创建应用的时候的 CALLBACK 参数
这里会跳转到第三方授权页面等待用户授权完成, 之后再次跳转指定 Callback URL 地址上, 最后附带有 code=xxx 的登录标识.
拿到之后 code 之后就是获取会话凭据得情况:
# 这里需要利用 Basic 权限认证, 相当于 header 追加 Auth Basic: username:password 方式
# 之后利用 POST 方式推送 grant_type=authorization_code 和 code={code}
curl -X POST -u "client_id:secret" https://bitbucket.org/site/oauth2/access_token -d grant_type=authorization_code -d code={code}
# 这里首先 POST 涵盖以下参数
# grant_type(必须): 权限认证类型(authorization_code)
# code(必须): authorize 接口回调的授权码
# redirect_uri(非必须): 这里如果 authorize 接口带上该参数跳转则必须追加, 否则会导致验签错误(如果没有则不需要)
这里顺利获取 access_token 的情况, 之后就是直接提取玩家信息, 提取玩家信息当中关键 uuid 类似这种唯一不变标识.
# 这里需要 Bearer 权限认证, 用获取 access_token 获取出信息
curl -H "Authorization: Bearer ACCESS_TOKEN" https://api.bitbucket.org/2.0/user
# 这里不需要做什么操作, 会返回 JSON 格式的玩家数据, { uuid: xxx, .... }
这里玩家信息获取 uuid 就是玩家唯一标识, 可以作为账号 username + type 保存数据库.