<s> import urllib <EOL> import dbutils <EOL> import auth <EOL> import textutils <EOL> class OAuthProvider ( auth . Provider ) : <EOL> def start ( self , db , req , target_url = None ) : <EOL> cursor = db . cursor ( ) <EOL> state = auth . getToken ( ) <EOL> authorize_url = self . getAuthorizeURL ( state ) <EOL> if authorize_url is None : <EOL> return False <EOL> if target_url is None : <EOL> target_url = req . getParameter ( "<STR_LIT:target>" , None ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( state , target_url ) ) <EOL> req . setStatus ( <NUM_LIT> ) <EOL> req . addResponseHeader ( "<STR_LIT>" , authorize_url ) <EOL> req . start ( ) <EOL> db . commit ( ) <EOL> return True <EOL> def finish ( self , db , req ) : <EOL> if req . method != "<STR_LIT:GET>" : <EOL> raise auth . InvalidRequest <EOL> code = req . getParameter ( "<STR_LIT:code>" , default = None ) <EOL> state = req . getParameter ( "<STR_LIT:state>" , default = None ) <EOL> if code is None or state is None : <EOL> raise auth . InvalidRequest ( "<STR_LIT>" ) <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( state , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> raise auth . InvalidRequest ( "<STR_LIT>" % state ) <EOL> ( target_url , ) = row <EOL> access_token = self . getAccessToken ( code ) <EOL> if access_token is None : <EOL> raise auth . Failure ( "<STR_LIT>" ) <EOL> user_data = self . getUserData ( access_token ) <EOL> if user_data is None : <EOL> raise auth . Failure ( "<STR_LIT>" ) <EOL> account = textutils . encode ( user_data [ "<STR_LIT>" ] ) <EOL> username = textutils . encode ( user_data [ "<STR_LIT:username>" ] ) <EOL> email = user_data [ "<STR_LIT:email>" ] <EOL> email = textutils . encode ( email ) if email else None <EOL> fullname = textutils . encode ( user_data . get ( "<STR_LIT>" , username ) ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( self . name , account ) ) <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( self . name , account , email ) ) <EOL> row = ( cursor . fetchone ( ) [ <NUM_LIT:0> ] , None ) <EOL> external_user_id , user_id = row <EOL> user = None <EOL> if user_id is None : <EOL> if auth . isValidUserName ( username ) and self . configuration . get ( "<STR_LIT>" ) : <EOL> try : <EOL> dbutils . User . fromName ( db , username ) <EOL> except dbutils . NoSuchUser : <EOL> user = dbutils . User . create ( db , username , fullname , email , None ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( user . id , external_user_id ) ) <EOL> user . sendUserCreatedMail ( "<STR_LIT>" % self . name , <EOL> { "<STR_LIT>" : self . name , <EOL> "<STR_LIT>" : account } ) <EOL> else : <EOL> user = dbutils . User . fromId ( db , user_id ) <EOL> if user is not None : <EOL> auth . startSession ( db , req , user ) <EOL> else : <EOL> token = auth . getToken ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( token , external_user_id ) ) <EOL> data = { "<STR_LIT>" : self . name , <EOL> "<STR_LIT>" : account , <EOL> "<STR_LIT>" : token } <EOL> if target_url : <EOL> data [ "<STR_LIT:target>" ] = target_url <EOL> if username : <EOL> data [ "<STR_LIT:username>" ] = username <EOL> if email : <EOL> data [ "<STR_LIT:email>" ] = email <EOL> if fullname : <EOL> data [ "<STR_LIT>" ] = fullname <EOL> target_url = "<STR_LIT>" % urllib . urlencode ( data ) <EOL> req . setStatus ( <NUM_LIT> ) <EOL> req . addResponseHeader ( "<STR_LIT>" , target_url or "<STR_LIT:/>" ) <EOL> req . start ( ) <EOL> db . commit ( ) <EOL> return True <EOL> def validateToken ( self , db , account , token ) : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( self . name , account ) ) <EOL> row = cursor . fetchone ( ) <EOL> return row and token == row [ <NUM_LIT:0> ] </s>
