LoginSignup
123
118

More than 5 years have passed since last update.

Rails4に対応したgem doorkeeperついて調べてみた。

Last updated at Posted at 2013-12-02

Ruby on Rails Advent Calendar 2日目遅れました(^o^)

--

タイトルの通りですが、以前ブログに書いた事がある自社サービスの機能を簡単にAPIで提供出来てしまう!gem doorkeeperが凄い。 という記事を書いて1年以上経ちました。

doorkeeperも大分進化しており、Rails4対応もしている様なので再度検証してみることにしました。

下記作業ログになります。

検証環境

  • ruby 2.0.0p353
  • Rails 4.0.1

Gemfileにdoorkeer追加

Gemfileにdoorkeeperを追加します。

gem 'doorkeeper'

そしてbundle install

セットアップ

READMEに書いてある作業を行います。

rails generate doorkeeper:install

この時routesにuse_doorkeeperが追加される

rake routes の結果が下記。

                              GET    /oauth/authorize/:code(.:format)             doorkeeper/authorizations#show
          oauth_authorization GET    /oauth/authorize(.:format)                   doorkeeper/authorizations#new
                              POST   /oauth/authorize(.:format)                   doorkeeper/authorizations#create
                              PATCH  /oauth/authorize(.:format)                   doorkeeper/authorizations#update
                              PUT    /oauth/authorize(.:format)                   doorkeeper/authorizations#update
                              DELETE /oauth/authorize(.:format)                   doorkeeper/authorizations#destroy
                  oauth_token POST   /oauth/token(.:format)                       doorkeeper/tokens#create
           oauth_applications GET    /oauth/applications(.:format)                doorkeeper/applications#index
                              POST   /oauth/applications(.:format)                doorkeeper/applications#create
        new_oauth_application GET    /oauth/applications/new(.:format)            doorkeeper/applications#new
       edit_oauth_application GET    /oauth/applications/:id/edit(.:format)       doorkeeper/applications#edit
            oauth_application GET    /oauth/applications/:id(.:format)            doorkeeper/applications#show
                              PATCH  /oauth/applications/:id(.:format)            doorkeeper/applications#update
                              PUT    /oauth/applications/:id(.:format)            doorkeeper/applications#update
                              DELETE /oauth/applications/:id(.:format)            doorkeeper/applications#destroy
oauth_authorized_applications GET    /oauth/authorized_applications(.:format)     doorkeeper/authorized_applications#index
 oauth_authorized_application DELETE /oauth/authorized_applications/:id(.:format) doorkeeper/authorized_applications#destroy
             oauth_token_info GET    /oauth/token/info(.:format)                  doorkeeper/token_info#show

config/initializers/doorkeeper.rb が追加されているので、resource_owner_authenticator ブロックを下記に変更。

  resource_owner_authenticator do
    User.find_by_id(session[:user_id]) || redirect_to(new_user_session_url)
  end

またmigrateファイルも生成されているので、rake db:migrate を行います。

rails g scaffold User name:string
rails generate doorkeeper:migration
rake db:migrate

これでセットアップ完了。

ブラウザからアクセスしてみる

http://localhost:3000/oauth/applications にアクセス。

アプリケーション一覧が表示される

Doorkeeper_2.png

アプリケーション作成してみる

Doorkeeper_1.png

アプリケーションの認証

Doorkeeper.png

codeの取得も普通に出来ました!

感想

相変わらずprovider側の実装は簡単に出来そうですが、APIの実装とclient側のサンプルについての情報はまだまだ多くないなぁと思いました。

参考

gem doorkeeperを用いたAPI側の実装する情報が載っていて便利。
http://blog.n-z.jp/blog/2013-10-08-doorkeeper-api-client.html

123
118
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
123
118