关于oauth2.0:OAuth-20

11次阅读

共计 2433 个字符,预计需要花费 7 分钟才能阅读完成。

简介

第三方认证解决方案。用来获取令牌和应用令牌的协定,自身不解决用户信息。用来受权第三方利用,获取用户数据,而不须要将用户名、明码提供给第三方。

场景

第三方客户端拜访用户的资源,须要向用户申请受权。用户批准后,资源给第三方客户端令牌(access token)。令牌的权限范畴、有效期可控。

角色 Roles

Resource Owner: User
资源 resource 的拥有者,受权给第三方利用来拜访。这种受权有限度。
Resource Server/Authorization Server: API
资源服务器保留用户资源。受权服务器验证用户。
Client: Application
第三方利用。

协定示意


整体流程

  1. The application requests authorization to access service resources from the user
  2. If the user authorized the request, the application receives an authorization grant
  3. The application requests an access token from the authorization server (API) by presenting authentication of its own identity, and the authorization grant
  4. If the application identity is authenticated and the authorization grant is valid, the authorization server (API) issues an access token to the application. Authorization is complete.
  5. The application requests the resource from the resource server (API) and presents the access token for authentication
  6. If the access token is valid, the resource server (API) serves the resource to the application

第三方治理

在资源零碎(Authorization Server)申请注册,会生成客户端凭证:客户端标识符(client identifier/client ID)、客户端秘钥(client secret)。

受权形式

一、受权码模式(authorization code)

含意:第三方客户端先申请一个受权码,而后再用该码获取令牌。
特点:性能最欠缺、流程最紧密。通过客户端的后盾服务器与服务提供商的认证服务器进行互动。受权码前端传送,令牌的存储和通信都在后端。
流程示意

步骤
1.B 提供一个受权申请链接(A 的受权接口拼接参数),用户点击后会跳转到 A。(toA:response_type=code, client_id, redirect_uri, scope=read)
2. 跳转到 A 后,要求用户登录 A,并且询问提醒用户受权数据给 B 应用。若用户批准,跳转会指定地址且传回一个受权码。(jump to redirect_uri?code=AUTHORIZATION_CODE)
3.B 拿到受权码后,在后端向 A 申请令牌。(toA: client_id、client_secret、grant_type=AUTHORIZATION_CODE、code、redirect_uri)
4. 收到申请后颁发令牌。向 redirect_uri 发送数据,其中蕴含令牌。(jump to redirect_uri)。返回数据(access_token, token_type, expires, refresh_token, scope)。

二、隐式模式(implicit)

含意:容许间接向前端颁发令牌,没有受权码步骤。
特点:纯前端利用,无后端。
流程示意

步骤
1.B 提供一个链接,用户点击后跳转到 A。(response_type=token, client_id, redirect_uri, scop)
2. 跳转到 A 后,要求用户登录,并且询问提醒用户受权数据给 B 应用。用户批准,将令牌传回。(redirect_uri, token=ACCESS_TOKEN)
令牌地位是 url 锚点(fragment, #)不会发送到服务器。
3. 利用获取到 token 后,申请时带上。

三、明码模式(resource owner password credentials)

含意:高度信赖,容许把用户名和明码通知第三方利用,用来申请令牌。
特点:用户高度信赖的援用。
步骤
1.A 要求用户提供 B 的用户名和明码。拿到后向 B 申请令牌。(grant_type=password, username, password, client_id)
2.B 验证身份通过后,间接给出令牌。放在 json 数据中返回。

四、客户端模式(client credentials)

含意:获取第三方服务本人的数据(非用户数据)。
特点:后端 / 命令行申请。
步骤
1.B 在命令行向 A 发出请求。(grant_type=client_credentials, clent_id, client_secret)
2.A 验证通过后,返回令牌。

令牌的应用

B 拿到令牌后,就能够向 A 申请数据。在每个收回的 api 申请,都须要带有令牌。
申请 HEADER 头信息减少 Authorization 字段。

自动更新令牌

A 颁发令牌时,下发两个令牌,一个用户获取数据,一个用于获取新的令牌。
刷新令牌(grant_type=refresh_token, client_id, cliient_secret, refresh_token=REFERSH_TOKEN)

source:
https://www.ruanyifeng.com/bl…
http://www.ruanyifeng.com/blo…
https://www.digitalocean.com/…

正文完
 0