« 04.06 Pylons, CGI driven and logging | ココ | 04.08 ライセンス更新完了 »

2009年4月 7日

Pylons, CGI driven AuthKit Basic Auth   このエントリーを含むはてなブックマーク 

まったくと言っていいほど情報のない状態で始めたAuthKitのBasic認証、ようやくできた。loggingを自由に扱えるようにしたりいろいろ寄り道したけど、お陰でトレースがとても楽になって進めやすくなったんですよ。

で、AuthKitでBasic認証するのに、何が分からんかったかというと、仕組みそのもの(もちろんBasic認証の仕組みではなく)。Basic認証以外にも対応するのに共通する処理を基底クラスに書いてたりしてるのもあって、どこを参照すればいいんか追跡が大変。

@authorize decoratorで認証処理を通すのはまず良しとして、最初はそれでやってたんやけど、実際の認証はどこでするんだ? ということに。


認証ユーザーはREMOTE_USERで判断するんやろうから、REMOTE_USERが環境変数にくるんやろうな、と思って環境変数(wsgi.environ)を表示させてても一向に出てくる気配がない。なんでだ、と検索すると、

[apache][rails] リバースプロキシでの環境変数REMOTE_USER渡し(ref. komamitsu.log)

で、REMOTE_USERがmod_rewriteを通すと消えてしまうのかと思ったので、書かれていた対応策を書いたけど、全く解消せず(CGIで動かすにはmod_rewrite必須…ではないけど、index.cgiってのが入らないようにするには使わないと)。

そこでヘッダーを見ていて気がつく、「Authorization」がない。

あ、そうやな、WWW-Authenticateを送ったら、Authorizationが返ってくるもんな…。でもAuthorizationがないのもあるらしいけど。IEは付くはずなのに、表示してるヘッダーにない。端末からwgetで試すも、やはりAuthorizationがないがために認証に失敗する…。ひょっとして、これもmod_rewrite?

ApacheZopeSIG(ref. wiki.zope.jp)

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^/Zope(.*) /usr/local/apache/cgi-bin/Zope.cgi/$1 ?
[e=HTTP_CGI_AUTHORIZATION:%1,t=application/x-httpd-cgi,l]

はぁ、そうですか、やっぱりmod_rewriteでしたか。…Authorizationを、X-Authorizationかなんかに変換して、index.cgiで元のAuthenticationに戻してやればいいのか、とやってみるも、うまく行かず。

$ grep -R AUTHO /usr/lib/python2.4/site-packages/
/usr/lib/python2.4/site-packages/urlgrabber/grabber.py: SUGGESTED AUTHOR IMPLEMENTATION (THROTTLING)
/usr/lib/python2.4/site-packages/Pylons-0.9.7rc2-py2.4.egg/tests/test_webapps/filestotest/messages.ja.po:#FIRST AUTHOR , YEAR.
/usr/lib/python2.4/site-packages/Pylons-0.9.7rc2-py2.4.egg/tests/test_webapps/filestotest/messages.pot:#FIRST AUTHOR , YEAR.
/usr/lib/python2.4/site-packages/AuthKit-0.4.3-py2.4.egg/authkit/authenticate/basic.py: authorization = AUTHORIZATION(environ)
/usr/lib/python2.4/site-packages/AuthKit-0.4.3-py2.4.egg/authkit/authenticate/digest.py: 1. If the HTTP_AUTHORIZATION header was not provided or specifies
/usr/lib/python2.4/site-packages/AuthKit-0.4.3-py2.4.egg/authkit/authenticate/digest.py: authorization = AUTHORIZATION(environ)

AUTHORIZATION()が何なのかbasic.pyに書いてなくて探し回った挙句、grepしたらdigest.pyに書いてあった…。paste.httpheadersに書いてないんやけどなぁ…一体どこに書いてあるのか、AUTHORIZATIONなんて関数。で、AuthorizationヘッダーをHTTP_AUTHORIZATIONに変換すればよいことが分かったので、RewriteRuleにそれを書く。

DirectoryIndex index.cgi

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP:Authorization} (.+)
RewriteRule . - [E=HTTP_AUTHORIZATION:%1]
#RequestHeader set X-Authorization %{RU}e <-- 必要なかった

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ index.cgi/$1 [QSA,L]

これで、やっとBasic認証が通った!! …認証した後のWSGI環境変数(一部)。

<EnvironVariable AUTH_TYPE>: 'basic',
...
'HTTP_AUTHORIZATION': 'Basic dGVzdDp0ZXN0',
...
'REDIRECT_HTTP_AUTHORIZATION': 'Basic dGVzdDp0ZXN0',
...
<EnvironVariable REMOTE_USER>: 'test',
...
'authkit.authenticate': True,
'authkit.config': {'basic.authenticate.function': 'xxxx.lib.authkit.helper:validate',
'basic.realm': 'admin',
'cookie.params': 'expires: 10',
'cookie.secret': 'secret_string',
'setup.enable': True,
'setup.method': 'basic'},
'authkit.intercept': ['401'],
'authkit.users': None,
...

authkitの設定をdevelopment.iniでやって、下のコードで呼び出します。

from authkit.authorize.pylons_adaptors import authorize, authorize_request, authorized
from authkit.permissions import RemoteUser, ValidAuthKitUser, UserIn
from xxxxxx.lib.authkit.helper import httpExceptionCatcher

log = logging.getLogger(__name__)

class BController(BaseController):

  @httpExceptionCatcher()
  def index(self):
    # Return a rendered template
    # return render('/template.mako')
    # or, Return a response

    import pprint
    log.warning(pprint.pformat(request.environ))

    if not authorized(RemoteUser()):
      authorize_request(RemoteUser())

    return ''

By ただ at 23:28 カテゴリー ; PinMarch Samples , プログラミングとか

« 04.06 Pylons, CGI driven and logging | 04月の記事 | 04.08 ライセンス更新完了 »




トラックバック

このエントリーのトラックバックURL:
http://pinmarch.sakura.ne.jp/mt/mt-tb.cgi/1395

このリストは、次のエントリーを参照しています: Pylons, CGI driven AuthKit Basic Auth:

» ニューバランス 574 from ニューバランス 574
He arrived in a very white Bentley and partied together promoter Alex Gidewon...

トラックバック時刻: 2014年8月21日 10:40