gem doorkeeper(4000✨) ,Go-rails视频

博客OAuth教程:https://i.cnblogs.com/EditPosts.aspx?postid=9531091

doorkeeper: (4000🌟)https://github.com/doorkeeper-gem/doorkeeper 

 

什么是OAuth? 

OAuth是一个通信协定。全名: The OAuth2.0 Authorization Framework

 

什么是Doorkeeper?

Doorkeeper用于Rails的,帮助我们构建Oauth2的gem. 

使用简介:http://linjunzhu.github.io/blog/2014/09/21/doorkeepershi-yong-jian-jie/


 

GORails API Authentication Series

5节课

 

第一节  APi Authentication with an OAuth Provider 

使用gem 'doorkeeper'给你的API增加验证,建立一个OAuth provider

Setting up an OAuth provider is a great way to add authentication to your API using the doorkeeper gem

 

rails new api_app -d postgresql -m https://raw.githubusercontent.com/excid3/jumpstart/master/template.rb

安装gem 'doorkeeper'

bundle

rails g doorkeeper:install  #这会生成config/initializers/doorkeeper.rb

rails g doorkeeper:migration

 

视频:

和devise一起使用的话,需要更改配置doorkeeper.rb:(在gem的Wiki中搜索devise)

目的是to generate auth_tokens for you client apps.

resource_owner_authenticator do
 +current_user || warden.authenticate!(:scope => :user)
end

 

Doorkeeper.configuration.access_token_expires_in可以指定token和grant的到期时间。

 

视频:

在create_doorkeeper_tables.rb中,加上两个外键。

add_foreign_key :oauth_access_grants, :users, column: :resource_owner_id
add_foreign_key :oauth_access_tokens, :users, column: :resource_owner_id

解释:add_foregin_key(from_table, to_table, options={})

给oauth_access_grants增加一个外键,这个外键对应users的默认id列。

from_table是涉及到外键列的表,to_table包括了涉及的主键(默认是id,也可以用参数自定义)

 

terminal上继续。输入rails db:migrate

 

在user.rb中加上git上的代码。和access_grants, access_tokens进行关联(代码见git):

class User < ApplicationRecord
#...略

  has_many :access_grants, class_name: "Doorkeeper::AccessGrant",
                           foreign_key: :resource_owner_id,
                           dependent: :delete_all # or :destroy if you need callbacks

  has_many :access_tokens, class_name: "Doorkeeper::AccessToken",
                           foreign_key: :resource_owner_id,
                           dependent: :delete_all # or :destroy if you need callbacks

end

 

 

在routes.rb中,安装脚本已经加上了use_doorkeeper,它会mount需要用到的路径(具体见git

GET       /oauth/authorize/native?code
GET       /oauth/authorize
POST      /oauth/authorize
DELETE    /oauth/authorize
POST      /oauth/token
POST      /oauth/revoke
POST      /oauth/introspect

GET       /oauth/authorized_applications
DELETE    /oauth/authorized_applications/:id
GET       /oauth/token/info


resources /oauth/applications     #得到8个路径

 

 

视频的讲解:

注册一个新的OAuth application.

https://github.com/settings/applications/new

视频10:00分的演示不明白

locallhost:3000/oauth/applications/打不开(作者说是安装不正确。)

Access to localhost was denied

You don't have authorization to view this page. 

HTTP ERROR 403

 

 

terminal上继续。

mkdir -p app/controllers/api/v1

touch app/controllers/api/v1/api_controller.rb, 增加代码:

⚠️ ::ApplicationController中的双冒号,不是很理解这种写法。

这是从Authorization Server的上返回Token。

 

在routes.rb上增加一个路径:

namespace :api do
 namespace :v1 do
  get '/me' => 'users#me'
 end
end

新增 touch app/controllers/api/v1/users_controller.rb

Authenticated resource owner:(git有基本相同的代码和扩展)

如果你想返回基于current resource owner的数据,access token owner, 你可能想定义一个方法,

这个方法返回resource owner instance;

⚠️ before_action :doorkeeper_authorize! 代表所有行为都需要 access token (也可以自定义,见git)

respond_to :json 所有的方法的响应的格式都是json

respond_with() Rails4.2后就被遗弃了。(git 'doorkeeper'中用到一次)

在这个代码中,返回access token owner的credentials(me.json)

浏览器输入api/v1/me.json,show行为也需要进行doorKeepr_authorize! ,得到doorkeeper_token。然后根据外键搜索到对应的user对象,并返回JSON格式。

 

 

视频14:51 显示了返回的json. 我实操返回401代码。请求没有被applied, 因为它缺少有效的验证文凭给目标资源。(留言中,其他用户遇到一样的问题。)

 

另一个可选的使用的方法:

module Api::V1
  class ApiController < ::ApplicationController
    private

    def  current_resource_owener
      User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
    end

    helper_method :current_resource_owner   //不使用me方法。注释掉
  end
end

如果想客制化me.json, 可以自定义获得的信息:

  1. 注释掉方法me中的代码
  2. 在api_controller.rb中的类ApiController增加:helper_method :current_resource_owner
  3. mkdir -p app/views/api/v1/users  (加-p 可以一下生成多个文件夹)
  4. 新建touch app/views/api/v1/user/me.json.jbuilder, 并增加代码:👇

json.id current_resource_owner.id
json.name current_resource_owner.name
json.avatar_url gravatar_image_url(current_resource_owner.email, size: 40)

⚠️ helper_method(*methods)用于声明一个controller方法为helper。之后这个方法就可以在controller中的view中直接用了。

 

posted @ 2018-07-02 21:48  Mr-chen  阅读(295)  评论(0编辑  收藏  举报