エキPy lt repoze.whoの紹介
-
Upload
atsushi-odagiri -
Category
Documents
-
view
628 -
download
0
Transcript of エキPy lt repoze.whoの紹介
repoze.whoエキスパートPython読書会 LT
aodag
repoze.whoとは?
Web認証フレームワーク
WSGIミドルウェアでもAPI呼び出しでも使える
ユーザー認証の各フェーズを細かく設定できるpyramidでも使えるよ
認証のフェーズ
IdentifyChallengeAuthenticate
Identify
リクエストから認証情報を取り出すレスポンスに認証情報を追加するレスポンスから認証情報を削除する
Challenger
認証を開始するための情報をレスポンスに追加する
Authenticate
ユーザー入力などを認証する
Basic認証の場合(1)
Identifierがリクエスト の Authorizationヘッダを読み取る
なかった場合
Challengerが WWW-Authorizationヘッダを追加して、ブラウザにBasic認証を促す
Basic認証の場合(2)
Identifierがリクエスト の Authorizationヘッダを読み取る
読み取った内容を Authenticate する。
成功だったら、アプリケーションが呼び出される。レスポンスを返すときに、 Identifierがレスポンスに認証結果を追加する
repoze.whoを使う
1. WSGIミドルウェアとして使う
2. APIとして使う
3. 両方を混ぜる
repoze.whoの設定
1. iniファイルで指定する
2. Pythonコードで指定する
設定例
認証情報はAuthTktで保存する認証自体はSQLAlchemyのモデルで行うログイン画面を使う
who.ini 各プラグインの設定
[plugin:auth_tkt]use = repoze.who.plugins.auth_tkt:make_pluginsecret = SeCrEt
[plugin:sqla]use = repoze.who.plugins.sa:make_sa_authenticator
[plugin:redirect]use = repoze.who.plugins.redirector:make_pluginlogin_url = /login
who.ini 認証を構成する
[identifiers]plugins = auth_tkt
[authenticators]plugins = sqla
[challengers]plugins = redirect
PasteDeploy + WSGIミドルウェア
[filter:who]use = egg:repoze.who#configconfig_file = %(here)s/who.ini
ログイン画面でAPIを使う
api = repoze.who.api.get_api(environ)identity = { 'login': request.POST['username'], 'password': request.POST['password'],}
authenticated, headers = api.login(identity)
Pyramidで使うには?
pyramid_whoを使うと、repoze.whoの認証結果をpyramidのautenticated_useridから取得できるようになる
Pluginを作るには?
IIdentifier, IAuthenticator, IChallenger を実装しましょう。
多分、 IAuthenticator を実装することが多い
IAuthenticator を実装する
@implementer(IAuthenticator)class MyAuthenticator(object): def __init__(self, members): self.members = members
def authenticate(self, environ, identity): member = identity['member'] return (member if member in members else None)
設定ファイルで使えるようにする
def make_plugin(members=None): assert members is not None, u"設定しろ"
members = members.split() return MyAuthenticator(members)
使う!
[plugins:my]use = aodag.who.plugins:make_pluginmembers = aodag drillbits shimizukawa takanory...[authenticators]plugins = my
いろんなプラグイン
標準● AuthTkt● Basic認証● HTPasswd● リダイレクト● SQL
追加アドオン● LDAP● SQLAlchemy● OpenID
etc...
まとめ
repoze.whoは認証フレームワーク
認証処理の各フェーズを組み合わせて使える
すでに十分なプラグインがある
必要な場合はプラグインを簡単に追加できる
まとめ
とっつきにくいけどいいやつだよ!
LINKS
● repoze.who – WSGI Authentication Middleware