def checkIfUserWasAlreadyLogged(self): tokenSessionAfterGetRequest = str(uuid.uuid4()) if loginBusiness.LoginBusiness().checkIfUserWasAlreadyLogged( tokenSessionAfterGetRequest): print('TEST 3 SUCCESS: checkIfUserWasAlreadyLogged') else: print('TEST 3 FAILED: checkIfUserWasAlreadyLogged')
def shouldThrowExceptionWhenTheCodeIsInvalid(self): fakeToken = str(uuid.uuid4) if not loginBusiness.LoginBusiness().excangeCodeForCredentialObject( fakeToken, 'gplus_client_secret.json'): print('TEST 10 SUCCESS: shouldThrowExceptionWhenTheCodeIsInvalid') else: print('TEST 10 FAILED: shouldThrowExceptionWhenTheCodeIsInvalid')
def shouldThrowExceptionIfClientSecretsFileDoesNotExists(self): if not loginBusiness.LoginBusiness().readGoogleSecretsData( 'googleSecretsFileNameWithExtension.json'): print('TEST 8 SUCCESS: \ shouldThrowExceptionIfClientSecretsFileDoesNotExists') else: print('TEST 8 FAILED: \ shouldThrowExceptionIfClientSecretsFileDoesNotExists')
def shouldThrowExceptionsWhenLongTermTokenIsInvalidForRetrievePhoto(self): token = str(uuid.uuid4) if not loginBusiness.LoginBusiness().getUserProfilePhoto(token): print('TEST 7 SUCCESS: \ shouldThrowExceptionsWhenLongTermTokenIsInvalidForRetrievePhoto') else: print('TEST 7 FAILED: \ shouldThrowExceptionsWhenLongTermTokenIsInvalidForRetrievePhoto')
def shouldThrowExceptionWhenLongTermTokenIsInvalid(self): token = str(uuid.uuid4) if not loginBusiness.LoginBusiness().getFacebookUserInfos(token): print('TEST 6 SUCCESS: \ shouldThrowExceptionWhenLongTermTokenIsInvalid') else: print('TEST 6 FAILED: \ shouldThrowExceptionWhenLongTermTokenIsInvalid')
def shouldThrowExceptionWhenHasNoClientSecretsFile(self): if not loginBusiness.LoginBusiness( ).checkIfFacebookClientSecretsExists( 'fbSecretsFileNameWithExtension.json'): print('TEST 4 SUCCESS: \ shouldThrowExceptionWhenHasNoClientSecretsFile') else: print('TEST 4 FAILED: \ shouldThrowExceptionWhenHasNoClientSecretsFile')
def shouldThrowExceptionWhenGoogleAccessTokenIsInvalid(self): fakeAccessToken = str(uuid.uuid4) if not loginBusiness.LoginBusiness().validateGoogleToken( fakeAccessToken): print('TEST 11 SUCCESS: \ shouldThrowExceptionWhenGoogleAccessTokenIsInvalid') else: print('TEST 11 FAILED: \ shouldThrowExceptionWhenGoogleAccessTokenIsInvalid')
def shouldThrowExceptionWhenRequestDataIsIncorrect(self): app_id = 'id' app_secret = 'secret' access_token = 'token' if not loginBusiness.LoginBusiness().getLongTermAccessToken( app_id, app_secret, access_token): print('TEST 5 SUCCESS: \ shouldThrowExceptionWhenRequestDataIsIncorrect') else: print('TEST 5 FAILED: \ shouldThrowExceptionWhenRequestDataIsIncorrect')
def tokenValidationShouldBeFalse(self): tokenSessionAfterPostRequest = '' for request in self.requests: if request == 'POST': tokenSessionAfterPostRequest = str(uuid.uuid4()) tokenSessionAfterGetRequest = str(uuid.uuid4()) if not loginBusiness.LoginBusiness().validateUserSession( tokenSessionAfterGetRequest, tokenSessionAfterPostRequest): print('TEST 2 SUCCESS: tokenValidationShouldBeFalse') else: print('TEST 2 FAILED: tokenValidationShouldBeFalse')
def shouldThrowExceptionWhenTheClientIdIsNotPresent(self): absoluteTempFilePath = os.path.join(gettempdir(), 'googleSecretsFile.json') fakeData = {"web": {"clientid": "someid"}} tmp = open(absoluteTempFilePath, 'w+') tmp.write(json.dumps(fakeData)) tmp.close() if not loginBusiness.LoginBusiness().readGoogleSecretsData( absoluteTempFilePath): print('TEST 9 SUCCESS: \ shouldThrowExceptionWhenTheClientIdIsNotPresent') else: print('TEST 9 FAILED: \ shouldThrowExceptionWhenTheClientIdIsNotPresent')
def login(): if 'username' in login_session: return redirect('/') if request.method == "POST": loginbusiness = loginBusiness.LoginBusiness() print('LOGINFO: VALIDATING TOKEN SESSION...') if not loginbusiness.validateUserSession(login_session['user_token'], request.args.get('state')): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: CHECKING IF THE USER WAS ALREADY LOGGED...') if loginBusiness.LoginBusiness().checkIfUserWasAlreadyLogged( login_session.get('access_token')): response = make_response( json.dumps('The user was already logged...'), 200) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: CHECKING THE PLATFORM...') if request.args.get('platform') == 'facebook': print('LOGINFO: DONE! PLATFORM=%s' % request.args.get('platform')) facebookAccessToken = request.data.decode('utf-8') print('LOGINFO: CHECKING CLIENT SECRETS...') if not loginbusiness.checkIfFacebookClientSecretsExists( 'fb_secrets.json'): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: GETTING LONGTERM ACCESSTOKEN...') if not loginbusiness.getLongTermAccessToken( loginbusiness.getClientId(), loginbusiness.getClientSecret(), facebookAccessToken): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: BUILDING A SESSION FOR THE USER...') facebookLongTermAccessToken = \ loginbusiness.getLongTermAccessTokenFromFacebook() print('LOGINFO: DONE...') login_session['access_token'] = facebookLongTermAccessToken print('LOGINFO: GETTING USERINFOS...') loginbusiness.getFacebookUserInfos(login_session['access_token']) print('LOGINFO: DONE...') login_session['provider'] = loginbusiness.getProvider() login_session['user_id_facebook'] = loginbusiness.getUserId() login_session['username'] = loginbusiness.getUserName() login_session['email'] = loginbusiness.getUserEmail() print('LOGINFO: GETTING USER PHOTO...') if not loginbusiness.getUserProfilePhoto( login_session['access_token']): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response login_session['picture'] = loginbusiness.getProfilePhoto() login_session['local_user_id'] = \ loginbusiness.getLocalUserId(login_session) print('LOGINFO: DONE...') if request.args.get('platform') == 'google': print('LOGINFO: DONE! PLATFORM=%s' % request.args.get('platform')) if not loginbusiness.readGoogleSecretsData( 'gplus_client_secret.json'): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: BUILDING A CREDENTIAL OBJECT...') if not loginbusiness.excangeCodeForCredentialObject( request.data, 'gplus_client_secret.json'): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: VALIDATING TOKEN...') if not loginbusiness.validateGoogleToken( loginbusiness.getCredentialsData().access_token): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: VALIDATING USER...') if not loginbusiness.isTheDataIsFromTheSameUserThatGaranteedAccess( loginbusiness.getCredentialsData().id_token['sub'], loginbusiness.getDataFromTokenValidation()['user_id']): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: VALIDATING APP...') if not loginbusiness.theResponseDataShouldBeDirectedToAnotherApp( loginbusiness.getDataFromTokenValidation()['issued_to'], loginbusiness.getClientId()): response = make_response( json.dumps('Occured an error, please, try log in again!'), 500) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: CHECKING IF USER WAS ALREADY LOGGED...') if loginBusiness.LoginBusiness().checkIfUserWasAlreadyLogged( login_session.get('gplus_id')): response = make_response( json.dumps('The user was already logged...'), 200) response.headers['Content-Type'] = 'application/json' return response login_session['access_token'] = \ loginbusiness.getCredentialsData().access_token login_session['gplus_id'] = \ loginbusiness.getCredentialsData().id_token['sub'] print('LOGINFO: GETTING USER INFOS...') if not loginbusiness.getUserInformationFromGoogleApi( loginbusiness.getCredentialsData().access_token): response = make_response( json.dumps('Occured an error, please, try log in again!'), 200) response.headers['Content-Type'] = 'application/json' return response print('LOGINFO: BUILDING USER SESSION...') login_session['provider'] = loginbusiness.getProvider() login_session['username'] = loginbusiness.getUserName() login_session['email'] = loginbusiness.getUserEmail() login_session['picture'] = loginbusiness.getProfilePhoto() login_session['local_user_id'] = loginbusiness.getLocalUserId( login_session) print('LOGINFO: DONE!') flash('Welcome!!!') return 'Logged with success!' if request.method == "GET": state = str(uuid.uuid4()) login_session['user_token'] = state return render_template('login.html', STATE=state)