当前位置:   article > 正文

Github认证调研_github authentication code

github authentication code

Github认证调研

我使用了阿里云和百度云的镜像服务中的代码库绑定,绑定了github,所以在github的设置中的application中可以看到如下显示:
在这里插入图片描述

百度云使用了OAuth App

在这里插入图片描述

阿里云使用的权限

在这里插入图片描述

阮一峰将的auth2.0。将的很好,接了我很多的疑惑,尤其是更新令牌那块,阮一峰的文章

第三方登录原理

所谓第三方登录,实质就是 OAuth 授权。用户想要登录 A 网站,A 网站让用户提供第三方网站的数据,证明自己的身份。获取第三方网站的身份数据,就需要 OAuth 授权。举例来说,A 网站允许 GitHub 登录,背后就是下面的流程:

  1. A 网站让用户跳转到 GitHub。
  2. GitHub 要求用户登录,然后询问"A 网站要求获得 xx 权限,你是否同意?"
  3. 用户同意,GitHub 就会重定向回 A 网站,同时发回一个授权码。
  4. A 网站使用授权码,向 GitHub 请求令牌。
  5. GitHub 返回令牌.
  6. A 网站使用令牌,向 GitHub 请求用户数据。

Github认证流程详解

简单看下github认证的时序图,后面会一一讲解

应用 github 请求github对第三方应用授权 接受授权请求,并调用r edirect_url 调用redirect_githuburl并传递授权码 接受授权码 用授权码和client_id获取access_token 返回access_token 接受access_token 用access_token获取正常的用户数据 应用 github

登记

一个应用要求 OAuth 授权,必须先到对方网站登记,让对方知道是谁在请求。
所以,你要先去 GitHub 登记一下。登记网址
应用的名称随便填,主页 URL 填写http://localhost:8080,跳转网址填写 http://localhost:8080/oauth/redirect。提交表单以后,GitHub 应该会返回客户端 ID(client ID)和客户端密钥(client secret),这就是应用的身份识别码:
在这里插入图片描述

请求github用户授权

用户授权就是请求:

https://github.com/login/oauth/authorize?client_id=65210525ee9827b8d328&redirect_uri=http://localhost:8080/oauth/redirect&state=zhouyu
  • 1

解释下state可以用作当前用户的标识,因为有很多用户在申请授权,根据回调的url中的state可以进行区分。此外上面的授权是普通授权,需要在参数中加入scope,在此不做演示。client_id告诉 GitHub 谁在请求,redirect_uri是稍后登陆完后跳转回来的网址。用户点击到了 GitHub,GitHub 会要求用户登录,确保是本人在操作。

返回授权码

登录后,GitHub 询问用户,该应用正在请求数据,你是否同意授权。
用户同意授权, GitHub 就会跳转到redirect_uri指定的跳转网址,并且带上授权码,跳转回来的 URL 就是下面的样子。

http://localhost:8080/oauth/redirect?
  code=859310e7cecc9196f4af
  • 1
  • 2

后端收到这个请求以后,就拿到了授权码(code参数)。

用授权码获取access_token

针对/oauth/redirect的请求,编写一个路由,完成 OAuth 认证,其实就是解析github返回的授权码

后端使用这个授权码,向 GitHub 请求令牌。
在这里插入图片描述

返回 access_token

作为回应,GitHub 会返回一段 JSON 数据,里面包含了令牌access_token

在这里插入图片描述

用access_token正常请求

有了令牌以后,就可以向 API 请求数据了。比如请求user的数据,把token放在Header里,格式

Authorization: token OAUTH-TOKEN
在这里插入图片描述

返回结果:

{
    "login": "fishingfly",
    "id": 29742631,
    "node_id": "MDQ6VXNlcjI5NzQyNjMx",
    "avatar_url": "https://avatars2.githubusercontent.com/u/29742631?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/fishingfly",
    "html_url": "https://github.com/fishingfly",
    "followers_url": "https://api.github.com/users/fishingfly/followers",
    "following_url": "https://api.github.com/users/fishingfly/following{/other_user}",
    "gists_url": "https://api.github.com/users/fishingfly/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/fishingfly/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/fishingfly/subscriptions",
    "organizations_url": "https://api.github.com/users/fishingfly/orgs",
    "repos_url": "https://api.github.com/users/fishingfly/repos",
    "events_url": "https://api.github.com/users/fishingfly/events{/privacy}",
    "received_events_url": "https://api.github.com/users/fishingfly/received_events",
    "type": "User",
    "site_admin": false,
    "name": null,
    "company": null,
    "blog": "",
    "location": null,
    "email": null,
    "hireable": null,
    "bio": null,
    "public_repos": 22,
    "public_gists": 0,
    "followers": 1,
    "following": 0,
    "created_at": "2017-06-28T00:43:10Z",
    "updated_at": "2020-02-15T03:26:52Z"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

要是需要展示仓库信息的话,需要在认证时加入scope。例如

https://github.com/login/oauth/authorize?client_id=65210525ee9827b8d328&redirect_uri=http://localhost:8080/oauth/redirect&state=zhouyu&scope=repo
  • 1

scope除了写repo之外,还能写别的权限:参考这个连接

https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/

在这里插入图片描述

拿到code后,再次去调https://github.com/login/oauth/access_token 接口,返回的值是这样的:

access_token=cfee603fe634bb638bb21bb0dabeaa4d09232790&scope=repo&token_type=bearer
  • 1

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QcC22IlB-1581767866930)
在这里插入图片描述

上面这张图片是获取了用户的所有仓库。

https://github.com/google/go-github 这个是github的V3api的go代码库,方便开发,里面有认证相关的api。

https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/ 官方认证流程在这

今年可能废弃的请求(token不要放url中就没问题)

https://developer.github.com/changes/2020-02-10-deprecating-auth-through-query-param/ 废弃验证中Token放在请求url中的认证请求。这部分认证请求会在2020年5月废除。之前的使用者将受到401状态码返回值。
auth认证参考文章
github的api概览

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/842594
推荐阅读
相关标签
  

闽ICP备14008679号