#NEWLINE# @ app . route ( '/oauth2callback/' ) #NEWLINE# def authenticate_user( ) : #NEWLINE# #INDENT# '\n    Authenticate user as logged in after Google OAuth2 sends a callback.\n\n    ' #NEWLINE# error = request . args . get ( 'error' ) #NEWLINE# if error : #NEWLINE# #INDENT# logger . warning ( 'Google sent us an error via OAuth2: %s' , error ) #NEWLINE# return redirect ( url_for ( 'login' ) ) #NEWLINE# #UNINDENT# code = request . args . get ( 'code' ) #NEWLINE# credentials = flow . step2_exchange ( code ) #NEWLINE# id_token = credentials . id_token #NEWLINE# email = id_token [ 'email' ] #NEWLINE# verified_email = id_token [ 'email_verified' ] #NEWLINE# if ( verified_email is True ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# user = FlaskUser ( User . objects . get ( email = email ) ) #NEWLINE# #UNINDENT# except User . DoesNotExist : #NEWLINE# #INDENT# user = None #NEWLINE# #UNINDENT# if ( not user ) : #NEWLINE# #INDENT# flash ( 'A Galah account does not exist for this email.' , 'error' ) #NEWLINE# logger . info ( 'User %s has attempted to log in via OAuth2 but an account does not exist for them.' , email ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# login_user ( user ) #NEWLINE# logger . info ( 'User %s has succesfully logged in via OAuth2.' , email ) #NEWLINE# return redirect ( url_for ( 'home' ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# flash ( "Sorry, we couldn't verify your email" , 'error' ) #NEWLINE# logger . info ( 'User %s failed to authenticate with OAuth2 because their email has not been verified with google.' , email ) #NEWLINE# #UNINDENT# return redirect ( url_for ( 'login' ) )