« Rails勉強会@東京 第6回 | メイン | ドラゴンのスヌーカーレッスン Part4 »
2006年05月25日 (木)
簡単認証プラグイン試作 [テクニカル]
「ヽ( ・∀・)ノくまくまー(2006-05-21)」より本格的な認証機能は不要なので、のーにゅーまでもう時間がない!みたいな必死なときに30秒くらいでとりあえずな認証をかけられるものがあると便利だとゆいたいです。とりあえず .htaccess でBasic認証をちょこっと置いてます、みたいな。
class AdminController < ApplicationController htpasswd :user=>"maiha", :pass=>"berryz" # 認証(何行でもかけて上から随時実行) htpasswd :file=>"/var/www/passwd/.htpasswd" # 外部ファイル指定も可能(htpasswd 互換) end
Basic認証でよければ、ってことで認証まわりの練習がてらプラグインをでっちあげてみた。
だいたい上の使用例の感じで動くはず。動かなかったらごめんなさい。
追記:くまくまーの中の人に添削してテストも書いてもらった!感謝! てことで使うときはこちらをどうぞ→「ヽ( ・∀・)ノくまくまー(2006-05-25)」
コード。汚いので添削希望。
module Htpasswd
def self.included(base)
base.extend(ClassMethods)
end
class Error < StandardError
define_method(:message) { 'Authorization is required' }
end
module ClassMethods
def htpasswd(options={})
if options[:file]
type = options[:type] || :crypted
IO.foreach(options[:file]) do |line|
user, pass = line.chomp.split(':', 2)
write_inheritable_array(:htpasswd, [{:user => user, :pass => pass, :type => type }])
end
else
write_inheritable_array(:htpasswd, [{:user => options[:user], :pass => options[:pass], :type => :plain }])
end
skip_before_filter :htpasswd_authorize rescue nil
before_filter :htpasswd_authorize
end
end
protected
def htpasswd_authorize
raise Htpasswd::Error unless auth_header = request.env['HTTP_AUTHORIZATION']
auth_type, auth_data = auth_header.split(' ', 2)
user, pass = Base64.decode64(auth_data).split(':', 2)
raise Htpasswd::Error unless pass.length > 0
auth_users = self.class.read_inheritable_attribute(:htpasswd)
auth_success = false
auth_users.each do |h|
case h[:type]
when :plain
if h[:user] == user and h[:pass] == pass
auth_success = true
break
end
when :crypted
if h[:user] == user and h[:pass] == pass.crypt(h[:pass])
auth_success = true
break
end
end
end
raise Htpasswd::Error unless auth_success
return true
rescue Htpasswd::Error => error
realm = "Authorization"
response.headers['WWW-Authenticate'] = response.headers['WWW-Authenticate'].to_a
response.headers['WWW-Authenticate'].push("Basic realm=\"#{realm}\"")
#headers['Status'] = '401 Unauthorized'
render :nothing => true, :status => 401
return false
end
end
トラックバック
このエントリーのトラックバックURL:
http://www.4bit.net/x/mt/mt-tb.cgi/120
コメント
ありがとう大地、ありがとう太陽。素晴らしいプラグインをありがとう!
投稿者 舞波 : 2006年05月26日 06:51
投稿者 samanta : 2010年04月11日 12:17
投稿者 grenlickwssv : 2010年07月08日 21:12
Good crew it's cool :)
投稿者 Fcgljoah : 2010年07月15日 19:24
I'm happy very good site
投稿者 Kogftpkt : 2010年07月15日 19:24
I love this site
投稿者 Vjzqtqjy : 2010年07月15日 19:25
good material thanks devon as a girl's name %( devon horse show parade %-((( mason moore tattoos :-PPP kitley house hotel devon :-[[ nina hartley lesbian movies 6478 tera patrick masterbates xxx video :DD lacey duvalle ass parade >:-[ view tera patrick anal %[[ devon jones sizes %)) lacey duvalle foot fucking ass shaking =((( naked jenna jameson >:PPP cody lane blue room 97315 the grass company crediton devon >:((( rachel starr patient lucky ljnu slutload delta white 8((( amy reid oiled and fucking ejwjq american daydreams shy love 2763 mike devon dance %[[ taylor vixen lela star 8-[[ jenna haze rough sex bgen
投稿者 Iohiiraa : 2010年07月29日 14:34