<s> """<STR_LIT>""" <EOL> import os <EOL> import random <EOL> import hmac <EOL> import __init__ as web <EOL> import openid . consumer . consumer <EOL> import openid . store . memstore <EOL> sessions = { } <EOL> store = openid . store . memstore . MemoryStore ( ) <EOL> def _secret ( ) : <EOL> try : <EOL> secret = file ( '<STR_LIT>' ) . read ( ) <EOL> except IOError : <EOL> secret = os . urandom ( <NUM_LIT:20> ) <EOL> file ( '<STR_LIT>' , '<STR_LIT:w>' ) . write ( secret ) <EOL> return secret <EOL> def _hmac ( identity_url ) : <EOL> return hmac . new ( _secret ( ) , identity_url ) . hexdigest ( ) <EOL> def _random_session ( ) : <EOL> n = random . random ( ) <EOL> while n in sessions : <EOL> n = random . random ( ) <EOL> n = str ( n ) <EOL> return n <EOL> def status ( ) : <EOL> oid_hash = web . cookies ( ) . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' , <NUM_LIT:1> ) <EOL> if len ( oid_hash ) > <NUM_LIT:1> : <EOL> oid_hash , identity_url = oid_hash <EOL> if oid_hash == _hmac ( identity_url ) : <EOL> return identity_url <EOL> return None <EOL> def form ( openid_loc ) : <EOL> oid = status ( ) <EOL> if oid : <EOL> return '''<STR_LIT>''' % ( openid_loc , oid , web . ctx . fullpath ) <EOL> else : <EOL> return '''<STR_LIT>''' % ( openid_loc , web . ctx . fullpath ) <EOL> def logout ( ) : <EOL> web . setcookie ( '<STR_LIT>' , '<STR_LIT>' , expires = - <NUM_LIT:1> ) <EOL> class host : <EOL> def POST ( self ) : <EOL> i = web . input ( return_to = '<STR_LIT:/>' ) <EOL> if i . get ( '<STR_LIT:action>' ) == '<STR_LIT>' : <EOL> logout ( ) <EOL> return web . redirect ( i . return_to ) <EOL> i = web . input ( '<STR_LIT>' , return_to = '<STR_LIT:/>' ) <EOL> n = _random_session ( ) <EOL> sessions [ n ] = { '<STR_LIT>' : i . return_to } <EOL> c = openid . consumer . consumer . Consumer ( sessions [ n ] , store ) <EOL> a = c . begin ( i . openid ) <EOL> f = a . redirectURL ( web . ctx . home , web . ctx . home + web . ctx . fullpath ) <EOL> web . setcookie ( '<STR_LIT>' , n ) <EOL> return web . redirect ( f ) <EOL> def GET ( self ) : <EOL> n = web . cookies ( '<STR_LIT>' ) . openid_session_id <EOL> web . setcookie ( '<STR_LIT>' , '<STR_LIT>' , expires = - <NUM_LIT:1> ) <EOL> return_to = sessions [ n ] [ '<STR_LIT>' ] <EOL> c = openid . consumer . consumer . Consumer ( sessions [ n ] , store ) <EOL> a = c . complete ( web . input ( ) , web . ctx . home + web . ctx . fullpath ) <EOL> if a . status . lower ( ) == '<STR_LIT:success>' : <EOL> web . setcookie ( '<STR_LIT>' , _hmac ( a . identity_url ) + '<STR_LIT:U+002C>' + a . identity_url ) <EOL> del sessions [ n ] <EOL> return web . redirect ( return_to ) </s>
