#NEWLINE# @ staticmethod #NEWLINE# def GetToken( svc , params , rduri , type , cid , csec ) : #NEWLINE# #INDENT# clients = clientdb . ClientDB ( ) #NEWLINE# try : #NEWLINE# #INDENT# client = clients . find_client ( cid ) #NEWLINE# if ( client is None ) : #NEWLINE# #INDENT# raise AuthClientError ( ) #NEWLINE# #UNINDENT# if ( not client . check_secret ( csec ) ) : #NEWLINE# #INDENT# raise AuthClientError ( ) #NEWLINE# #UNINDENT# if ( not rduri ) : #NEWLINE# #INDENT# rduri = client . get_default_redirect_uri ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( not client . check_redirect_uri ( rduri ) ) : #NEWLINE# #INDENT# raise AuthClientError ( ) #NEWLINE# #UNINDENT# #UNINDENT# if ( not type ) : #NEWLINE# #INDENT# raise AuthError ( rduri , 'invalid_request' ) #NEWLINE# #UNINDENT# if ( not client . check_grant_type ( type ) ) : #NEWLINE# #INDENT# raise AuthError ( rduri , 'unauthorized_client' ) #NEWLINE# #UNINDENT# if ( type == 'authorization_code' ) : #NEWLINE# #INDENT# if ( not params . has_key ( 'code' ) ) : #NEWLINE# #INDENT# raise AuthError ( rduri , 'invalid_request' ) #NEWLINE# #UNINDENT# code = params [ 'code' ] #NEWLINE# ( sessid , uid , scopes ) = Auth . SessionInfoFromCode ( code , cid ) #NEWLINE# if ( sessid == None ) : #NEWLINE# #INDENT# raise AuthError ( rduri , 'invalid_grant' ) #NEWLINE# #UNINDENT# Auth . RemoveCode ( code ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( type == 'refresh_token' ) : #NEWLINE# #INDENT# if ( not params . has_key ( 'refresh_token' ) ) : #NEWLINE# #INDENT# raise AuthError ( rduri , 'invalid_grant' ) #NEWLINE# #UNINDENT# old_refresh_token = svc . get_str ( params , 'refresh_token' ) #NEWLINE# refreshments = RefreshTokens ( ) #NEWLINE# try : #NEWLINE# #INDENT# old_token = refreshments . find ( old_refresh_token ) #NEWLINE# if ( old_token is None ) : #NEWLINE# #INDENT# raise AuthError ( rduri , 'invalid_grant' ) #NEWLINE# #UNINDENT# if ( old_token [ 'client_id' ] != cid ) : #NEWLINE# #INDENT# raise AuthError ( rduri , 'invalid_grant' ) #NEWLINE# #UNINDENT# uid = old_token [ 'uid' ] #NEWLINE# scopes = old_token [ 'scopes' ] #NEWLINE# user = UserManager . LoadUserByUid ( uid ) #NEWLINE# session = Session ( user , svc . client_address [ 0 ] , scopes = scopes . split ( ',' ) ) #NEWLINE# session . RecordLogin ( True ) #NEWLINE# sessid = session . GetID ( ) #NEWLINE# refreshments . remove ( old_refresh_token ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# refreshments . close ( ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise AuthError ( rduri , 'unsupported_grant_type' ) #NEWLINE# #UNINDENT# #UNINDENT# resp = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# resp [ 'access_token' ] = sessid #NEWLINE# resp [ 'token_type' ] = 'session' #NEWLINE# resp [ 'expires_in' ] = Config . SESSION_TIMEOUT_SECONDS #NEWLINE# resp [ 'scope' ] = ' ' . join ( scopes ) #NEWLINE# if client . check_grant_type ( 'refresh_token' ) : #NEWLINE# #INDENT# refreshments = RefreshTokens ( ) #NEWLINE# try : #NEWLINE# #INDENT# refresh_token = refreshments . new ( uid , cid , svc . client_address [ 0 ] , scopes ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# refreshments . close ( ) #NEWLINE# #UNINDENT# resp [ 'refresh_token' ] = refresh_token #NEWLINE# #UNINDENT# svc . writedata ( json . dumps ( old_refresh_token ) ) #NEWLINE# #UNINDENT# finally : #NEWLINE# #INDENT# clients . close ( )