<s> from __future__ import unicode_literals <EOL> import frappe <EOL> import frappe . utils <EOL> import json <EOL> from frappe import _ <EOL> class SignupDisabledError ( frappe . PermissionError ) : pass <EOL> def get_oauth2_providers ( ) : <EOL> return { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:code>" <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:user>" <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT:code>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> } <EOL> def get_oauth_keys ( provider ) : <EOL> """<STR_LIT>""" <EOL> keys = frappe . conf . get ( "<STR_LIT>" . format ( provider = provider ) ) <EOL> if not keys : <EOL> social = frappe . get_doc ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> keys = { } <EOL> for fieldname in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> value = social . get ( "<STR_LIT>" . format ( provider = provider , fieldname = fieldname ) ) <EOL> if not value : <EOL> keys = { } <EOL> break <EOL> keys [ fieldname ] = value <EOL> return keys <EOL> else : <EOL> return { <EOL> "<STR_LIT>" : keys [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" : keys [ "<STR_LIT>" ] <EOL> } <EOL> def get_oauth2_authorize_url ( provider ) : <EOL> flow = get_oauth2_flow ( provider ) <EOL> state = { "<STR_LIT>" : frappe . utils . get_url ( ) , "<STR_LIT>" : frappe . generate_hash ( ) } <EOL> frappe . cache ( ) . set_value ( "<STR_LIT>" . format ( provider , state [ "<STR_LIT>" ] ) , True , expires_in_sec = <NUM_LIT> ) <EOL> data = { <EOL> "<STR_LIT>" : get_redirect_uri ( provider ) , <EOL> "<STR_LIT:state>" : json . dumps ( state ) <EOL> } <EOL> oauth2_providers = get_oauth2_providers ( ) <EOL> data . update ( oauth2_providers [ provider ] . get ( "<STR_LIT>" , { } ) ) <EOL> return flow . get_authorize_url ( ** data ) <EOL> def get_oauth2_flow ( provider ) : <EOL> from rauth import OAuth2Service <EOL> params = get_oauth_keys ( provider ) <EOL> oauth2_providers = get_oauth2_providers ( ) <EOL> params . update ( oauth2_providers [ provider ] [ "<STR_LIT>" ] ) <EOL> return OAuth2Service ( ** params ) <EOL> def get_redirect_uri ( provider ) : <EOL> keys = frappe . conf . get ( "<STR_LIT>" . format ( provider = provider ) ) <EOL> if keys and keys . get ( "<STR_LIT>" ) : <EOL> return keys [ "<STR_LIT>" ] <EOL> else : <EOL> oauth2_providers = get_oauth2_providers ( ) <EOL> redirect_uri = oauth2_providers [ provider ] [ "<STR_LIT>" ] <EOL> return frappe . utils . get_url ( redirect_uri ) <EOL> def login_via_oauth2 ( provider , code , state , decoder = None ) : <EOL> info = get_info_via_oauth ( provider , code , decoder ) <EOL> login_oauth_user ( info , provider = provider , state = state ) <EOL> def get_info_via_oauth ( provider , code , decoder = None ) : <EOL> flow = get_oauth2_flow ( provider ) <EOL> oauth2_providers = get_oauth2_providers ( ) <EOL> args = { <EOL> "<STR_LIT:data>" : { <EOL> "<STR_LIT:code>" : code , <EOL> "<STR_LIT>" : get_redirect_uri ( provider ) , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> } <EOL> if decoder : <EOL> args [ "<STR_LIT>" ] = decoder <EOL> session = flow . get_auth_session ( ** args ) <EOL> api_endpoint = oauth2_providers [ provider ] . get ( "<STR_LIT>" ) <EOL> api_endpoint_args = oauth2_providers [ provider ] . get ( "<STR_LIT>" ) <EOL> info = session . get ( api_endpoint , params = api_endpoint_args ) . json ( ) <EOL> if ( ( "<STR_LIT>" in info and not info . get ( "<STR_LIT>" ) ) <EOL> or ( "<STR_LIT>" in info and not info . get ( "<STR_LIT>" ) ) ) : <EOL> frappe . throw ( _ ( "<STR_LIT>" ) . format ( provider . title ( ) ) ) <EOL> return info <EOL> def login_oauth_user ( data = None , provider = None , state = None , email_id = None , key = None , generate_login_token = False ) : <EOL> if isinstance ( data , basestring ) : <EOL> data = json . loads ( data ) <EOL> if isinstance ( state , basestring ) : <EOL> state = json . loads ( state ) <EOL> if not ( state and state [ "<STR_LIT>" ] ) : <EOL> frappe . respond_as_web_page ( _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) , http_status_code = <NUM_LIT> ) <EOL> return <EOL> token = frappe . cache ( ) . get_value ( "<STR_LIT>" . format ( provider , state [ "<STR_LIT>" ] ) , expires = True ) <EOL> if not token : <EOL> frappe . respond_as_web_page ( _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) , http_status_code = <NUM_LIT> ) <EOL> return <EOL> user = data [ "<STR_LIT:email>" ] <EOL> if not user : <EOL> frappe . respond_as_web_page ( _ ( "<STR_LIT>" ) , _ ( "<STR_LIT>" ) ) <EOL> return <EOL> try : <EOL> update_oauth_user ( user , data , provider ) <EOL> except SignupDisabledError : <EOL> return frappe . respond_as_web_page ( "<STR_LIT>" , "<STR_LIT>" , <EOL> success = False , http_status_code = <NUM_LIT> ) <EOL> frappe . local . login_manager . user = user <EOL> frappe . local . login_manager . post_login ( ) <EOL> frappe . db . commit ( ) <EOL> if frappe . utils . cint ( generate_login_token ) : <EOL> login_token = frappe . generate_hash ( length = <NUM_LIT:32> ) <EOL> frappe . cache ( ) . set_value ( "<STR_LIT>" . format ( login_token ) , frappe . local . session . sid , expires_in_sec = <NUM_LIT> ) <EOL> frappe . response [ "<STR_LIT>" ] = login_token <EOL> else : <EOL> redirect_post_login ( desk_user = frappe . local . response . get ( '<STR_LIT:message>' ) == '<STR_LIT>' ) <EOL> def update_oauth_user ( user , data , provider ) : <EOL> if isinstance ( data . get ( "<STR_LIT:location>" ) , dict ) : <EOL> data [ "<STR_LIT:location>" ] = data . get ( "<STR_LIT:location>" ) . get ( "<STR_LIT:name>" ) <EOL> save = False <EOL> if not frappe . db . exists ( "<STR_LIT>" , user ) : <EOL> if frappe . utils . cint ( frappe . db . get_single_value ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> raise SignupDisabledError <EOL> save = True <EOL> user = frappe . new_doc ( "<STR_LIT>" ) <EOL> user . update ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : get_first_name ( data ) , <EOL> "<STR_LIT>" : get_last_name ( data ) , <EOL> "<STR_LIT:email>" : data [ "<STR_LIT:email>" ] , <EOL> "<STR_LIT>" : ( data . get ( "<STR_LIT>" ) or "<STR_LIT>" ) . title ( ) , <EOL> "<STR_LIT>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : frappe . generate_hash ( data [ "<STR_LIT:email>" ] ) , <EOL> "<STR_LIT:location>" : data . get ( "<STR_LIT:location>" ) , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : data . get ( "<STR_LIT>" ) or data . get ( "<STR_LIT>" ) <EOL> } ) <EOL> else : <EOL> user = frappe . get_doc ( "<STR_LIT>" , user ) <EOL> if provider == "<STR_LIT>" and not user . get ( "<STR_LIT>" ) : <EOL> save = True <EOL> user . update ( { <EOL> "<STR_LIT>" : data . get ( "<STR_LIT:username>" ) , <EOL> "<STR_LIT>" : data [ "<STR_LIT:id>" ] , <EOL> "<STR_LIT>" : "<STR_LIT>" . format ( id = data [ "<STR_LIT:id>" ] ) <EOL> } ) <EOL> elif provider == "<STR_LIT>" and not user . get ( "<STR_LIT>" ) : <EOL> save = True <EOL> user . google_userid = data [ "<STR_LIT:id>" ] <EOL> elif provider == "<STR_LIT>" and not user . get ( "<STR_LIT>" ) : <EOL> save = True <EOL> user . github_userid = data [ "<STR_LIT:id>" ] <EOL> user . github_username = data [ "<STR_LIT>" ] <EOL> if save : <EOL> user . flags . ignore_permissions = True <EOL> user . flags . no_welcome_mail = True <EOL> user . save ( ) <EOL> def get_first_name ( data ) : <EOL> return data . get ( "<STR_LIT>" ) or data . get ( "<STR_LIT>" ) or data . get ( "<STR_LIT:name>" ) <EOL> def get_last_name ( data ) : <EOL> return data . get ( "<STR_LIT>" ) or data . get ( "<STR_LIT>" ) <EOL> def redirect_post_login ( desk_user ) : <EOL> frappe . local . response [ "<STR_LIT:type>" ] = "<STR_LIT>" <EOL> frappe . local . response [ "<STR_LIT:location>" ] = "<STR_LIT>" if desk_user else "<STR_LIT:/>" </s>
