Railsの認証について
7/11(土)にRailsGirls Shiojiri Moreにて、『Webを支える技術』の9.8~9.12HTTP認証について学び、Railsの認証についても気になったのでまとめてみました。
【目次】
Railsにおける認証
Railsでは、Basic認証・Digest認証・Token認証が利用されています。
詳しくはこちらに載っております。
Basic認証
http_basic_authenticate_with
メソッドを追加することで利用可能となります。
全てのアクションでフィルタが実行されるので、アクションを除外する場合にはexcept
で指定します。
class PostsController < ApplicationController http_basic_authenticate_with name: "dhh", password: "secret", except: :index def index render plain: "Everyone can see me!" end def edit render plain: "I'm only accessible if you know the password" end end
Digest認証
authenticate_or_request_with_http_digest
メソッドを追加することで利用可能となります。
ブロックの引数にはユーザー名をとり、認証に成功すればここからPWが返り、失敗すればfalseやnilが返ります。
require 'digest/md5' class PostsController < ApplicationController REALM = "SuperSecret" USERS = {"dhh" => "secret", #plain text password "dap" => Digest::MD5.hexdigest(["dap",REALM,"secret"].join(":"))} #ha1 digest password before_action :authenticate, except: [:index] def index render plain: "Everyone can see me!" end def edit render plain: "I'm only accessible if you know the password" end private def authenticate authenticate_or_request_with_http_digest(REALM) do |username| USERS[username] end end end
Token認証
その名の通りTokenを利用する認証方式です。
クライアントがサーバーに接続した際に、サーバーに送られたユーザー情報(セッションデータ)を暗号化したTokenが返されます。この時サーバーにはその情報が保存されず、Token(認証情報)として持ち、クライアントはリクエストする度にリクエストヘッダにToken含めて送り返し、サーバーは返されたTokenを復号して認証情報を取得する仕組みになっています。
これまで実装したDevise(ユーザー認証で利用)や、OmniAuth(twitterやgithub認証で利用)の機能もまた、Token認証になります。
仕組みとしては以下のようになります。
class PostsController < ApplicationController TOKEN = "secret" before_action :authenticate, except: [ :index ] def index render plain: "Everyone can see me!" end def edit render plain: "I'm only accessible if you know the password" end private def authenticate authenticate_or_request_with_http_token do |token, options| # Compare the tokens in a time-constant manner, to mitigate # timing attacks. ActiveSupport::SecurityUtils.secure_compare(token, TOKEN) end end end
参考
感想・まとめ
『Webを支える技術』のHTTP認証の学びから、Railsの認証についても調べるきっかけにできたことがよかったです。
HTTP認証のまとめはこちらより。