#NEWLINE# def default_provider( app ) : #NEWLINE# #INDENT# oauth = OAuth2Provider ( app ) #NEWLINE# @ oauth . clientgetter #NEWLINE# def get_client( client_id ) : #NEWLINE# #INDENT# return Client . query . filter_by ( client_id = client_id ) . first ( ) #NEWLINE# #UNINDENT# @ oauth . grantgetter #NEWLINE# def get_grant( client_id , code ) : #NEWLINE# #INDENT# return Grant . query . filter_by ( client_id = client_id , code = code ) . first ( ) #NEWLINE# #UNINDENT# @ oauth . tokengetter #NEWLINE# def get_token( access_token = None , refresh_token = None ) : #NEWLINE# #INDENT# if access_token : #NEWLINE# #INDENT# return Token . query . filter_by ( access_token = access_token ) . first ( ) #NEWLINE# #UNINDENT# if refresh_token : #NEWLINE# #INDENT# return Token . query . filter_by ( refresh_token = refresh_token ) . first ( ) #NEWLINE# #UNINDENT# return None #NEWLINE# #UNINDENT# @ oauth . grantsetter #NEWLINE# def set_grant( client_id , code , request , * args , **kwargs ) : #NEWLINE# #INDENT# expires = ( datetime . utcnow ( ) + timedelta ( seconds = 100 ) ) #NEWLINE# grant = Grant ( client_id = client_id , code = code [ 'code' ] , redirect_uri = request . redirect_uri , scope = ' ' . join ( request . scopes ) , user_id = g . user . id , expires = expires ) #NEWLINE# db . session . add ( grant ) #NEWLINE# db . session . commit ( ) #NEWLINE# #UNINDENT# @ oauth . tokensetter #NEWLINE# def set_token( token , request , * args , **kwargs ) : #NEWLINE# #INDENT# tok = Token ( ** token ) #NEWLINE# if ( request . response_type == 'token' ) : #NEWLINE# #INDENT# tok . user_id = g . user . id #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# tok . user_id = request . user . id #NEWLINE# #UNINDENT# tok . client_id = request . client . client_id #NEWLINE# db . session . add ( tok ) #NEWLINE# db . session . commit ( ) #NEWLINE# #UNINDENT# @ oauth . usergetter #NEWLINE# def get_user( username , password , * args , **kwargs ) : #NEWLINE# #INDENT# user = User . query . filter_by ( username = username ) . first ( ) #NEWLINE# if ( user and user . check_password ( password ) ) : #NEWLINE# #INDENT# return user #NEWLINE# #UNINDENT# return None #NEWLINE# #UNINDENT# return get_grant