- 2010-06-07 (Mon) 20:48
- article
今作っているサービスが後々、OAuth、OpenIDあたりにも対応することになるかもしれないので、そのあたり柔軟に対応できそうなwardenを試してみました。
必要なgemはwardenとrails_wardenの2つ。
Userモデルは以下のような感じで。
次にgithubのページを参考にconfig/initializers/warden.rbを追加します。今回はパスワードで認証をしたいのでpasswordのstrategyを自分で追加します。(と言ってもほとんどサンプルと同じ)
この後はUser.authenticateの中身をfind_by_login_and_passwordとかで実装してもいいのですが、それだとパスワードが平文になってしまうのでrestful_authenticationのコードを少し拝借することにします。
includeいているものがrestful_authenticationのコードです。適当にコピーしてlibにでも置いておくといいと思います。
これでpasswordとpassword_confirmationによるパスワードの確認、User#save時にパスワードをハッシュ化して保存してくれます。
後、一応ログイン処理するコントローラも載せておきます。
unauthenticatedが認証に失敗したときに遷移するアクションです。
正直、パスワード認証のことだけ考えるとちょっと面倒な気もします。パスワードの暗号化も自分で書かなければいけないし。ただ、上記のようにmoduleを別途用意しておけば作業の手間自体は結構軽減されので、そこまでは問題にならないかもしれません。
Strategyまわりについてはほとんど検証していませんが、色々と融通は効きそうな気もします。この辺は追々。
後はdeviseというwardenベースのライブラリもあるのでこれを試すのもありかなという気はします。ただ、deviseは色々とやり過ぎというか、多機能すぎてよくわからない感じもしますが・・・
- Newer: オブラブ2010夏イベントに参加・発表してきました
- Older: 本を頂きました
Comments:8
- Louanne Ruyter 11-10-19 (Wed) 9:23
-
Very nice post. I just stumbled upon your weblog and wished to say that I have truly enjoyed surfing around your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon!
- Maris Nachtrieb 11-10-25 (Tue) 15:46
-
I have to declare you help to make a number of great points and will put up a small amount of ideas to add on as soon as a day or two.
- Alona Swailes 11-10-30 (Sun) 9:24
-
I like this weblog very much so much wonderful information. “Fate chooses your relations, you choose your friends.” by Jacques Delille. Thank you.
- Jamie Lenherr 11-10-30 (Sun) 11:10
-
Hey there, You’ve done an incredible job here. I’ll definitely be back. And will personally suggest it to my friends. I’m sure they’ll benefit from this site. Thanks.
- Ferdinand Tiff 11-10-30 (Sun) 11:13
-
Hello there! I know this is somewhat off topic but I was wondering which blog platform are you using for this website? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking at alternatives for another platform. I would be great if you could point me in the direction of a good platform. Thank you!
- Walter Auker 11-10-31 (Mon) 6:05
-
Hi, I do not even know how I ended up here, but I thought this web site was cool. I do not know who you are but certainly you are great writing skills. Thank you.
- Harrison Levis 11-10-31 (Mon) 23:26
-
Great blog and really will help with becoming familiar with the subject matter much better.
- Cole Hillsgrove 11-11-02 (Wed) 10:29
-
Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a bit, but instead of that, this is wonderful blog. A fantastic read. I will definitely be back.
Trackbacks:0
- Trackback URL for this entry
- http://ukstudio.jp/2010/06/07/rails_warden/trackback/
- Listed below are links to weblogs that reference
- warden+railsでパスワード認証 from UKSTUDIO



