def post(self): post_data = request.get_json(force=True) isOk, message = update_email(get_data(post_data, 'account_id'), get_data(post_data, 'email')) if isOk: return build_json_result(None, 200, message) else: return build_json_result(None, 404, 'Twoja sesja wygasła', 'Your token is invalid')
def post(self): post_data = request.get_json(force=True) isExist, user = check_email_role(get_data(post_data, 'email'), get_data(post_data, 'role')) if isExist: save_pass_token(token=None, email=get_data(post_data, 'email')) html_content = build_pass_link(get_data(post_data, 'email'), None, user.role) LOG.info(html_content) send_email_by_sendinblue(get_data(post_data, 'email'), 'Zresetuj hasło', html_content) return build_json_result(None, 200, 'Wysłano wiadomość email', 'email sent') else: return build_json_result(None, 404, 'Nie znaleziono adresu email', 'email not exist')
def login_user(data): try: # fetch the user data user = User.query.filter_by(email=data.get('email')).first() if user and user.check_password(data.get('password')): if user.account_type == 'facebook': return build_json_result( None, 403, 'Nie rozpoznano konta Facebook', 'You must login by facebook account') if user.account_type == 'google': return build_json_result( None, 403, 'Nie rozpoznano konta Google', 'You must login by google account') isOk, message = save_refresh_token( user.id, get_data(data, 'device_id'), get_data(data, 'device_name'), get_data(data, 'device_model')) update_logged_time(user) if not user.is_active: return build_json_result( None, 403, 'Aby się zalogować, najpierw aktywuj konto', 'You need active your account to login') auth_token = User.encode_auth_token(user.id, user.role, user.account_type) if auth_token: response_object = { 'user_id': user.id, 'role': user.role, 'refresh_token': message, 'token': auth_token.decode() } # return response_object, 200 return build_json_result(response_object, 200, 'Zalogowano użytkownika', 'Successfully logged in.') else: return build_json_result(None, 401, 'Błąd serwera. Spróbuj jeszcze raz.', 'email or password does not match.') except Exception as e: print(e) trace_log() return build_json_result( None, 500, 'Błąd serwera. Spróbuj jeszcze raz.', 'a server communication error has occurred please try again later' )
def post(self): post_data = request.get_json(force=True) ok, err = change_pass_validate(post_data) if not ok: return build_json_error(err, 400, 'Błąd walidacji', 'Validation failed') isExist, email = check_pass(get_data(post_data, 'account_id'), get_data(post_data, 'old_password')) if isExist: isOk, message = update_password(email, get_data(post_data, 'password')) if isOk: return build_json_result(None, 200, message) else: return build_json_result(None, 500, message) else: return build_json_result(None, 404, 'Stare haslo jest niepoprawne', 'Your old password is not correct')
def save_new_user(data): user = User.query.filter_by(email=data['email']).first() if not user or not user.is_active: if user and not user.is_active: Device.query.filter_by(user_id=user.id).delete() User.query.filter_by(email=data['email']).delete() facebook_id = None google_id = None full_name = None avatar = None if 'facebook_id' in data: facebook_id = data['facebook_id'] if 'google_id' in data: google_id = data['google_id'] if 'full_name' in data: full_name = data['full_name'] if 'avatar' in data: avatar = data['avatar'] if 'avatar' in data: avatar = data['avatar'] new_user = User(email=data['email'], username=get_data(data, 'username'), password=data['password'], registered_on=datetime.datetime.utcnow(), last_logged=datetime.datetime.utcnow(), role=data['role'], account_type=data['account_type'], facebook_id=facebook_id, google_id=google_id, full_name=full_name, avatar=avatar) result = save_changes(new_user) if not result: return generate_token(new_user) else: return build_json_result(None, 500, result) else: response_object = { 'user_id': user.id, # 'status': 'fail', # 'message': 'User already exists. Please Log in.', } return build_json_result( response_object, 409, 'Ten email jest już w naszej bazie. Spróbuj się zalogować.', 'User already exists. Please Log in.')
def post(self, token): post_data = request.get_json(force=True) ok, err = change_pass_validate(post_data) if not ok: return build_json_error(err, 400, 'Błąd walidacji', 'Validation failed') isExist, email = check_pass_token(token) if isExist: isOk, message = update_password(email, get_data(post_data, 'password')) if isOk: return build_json_result(None, 200, message) else: return build_json_result(None, 500, message) else: return build_json_result(None, 404, 'Twoja sesja wygasła', 'Your token is invalid')
def login_google_user(post_data): try: if not 'token' in post_data: return build_json_result( None, 404, 'You need give me your facebook token') else: gg_token = post_data['token'] headers = {'Authorization': 'OAuth ' + gg_token} response = requests.get( "https://www.googleapis.com/oauth2/v1/userinfo", None, headers=headers) data = response.json() LOG.info(data) user_data = {} if data and not 'error' in data: gg_id = data['id'] gg_name = data['name'] gg_avatar = data['picture'] if 'email' in data: gg_email = data['email'] else: gg_email = gg_id + '@google.com' user = User.query.filter_by(google_id=gg_id).first() LOG.info(user) # login success if user: auth_token = User.encode_auth_token( user.id, user.role, user.account_type) isOk, message = save_refresh_token( user.id, get_data(post_data, 'device_id'), get_data(post_data, 'device_name'), get_data(post_data, 'device_model')) if auth_token: response_object = { "user_id": user.id, # 'status': 'success', # 'message': 'Successfully logged in.', 'token': auth_token.decode(), 'resfresh_token': message } profile = get_user_profile(user.id) response_object['profile'] = profile return build_json_result( response_object, 200, 'Successfully logged in.') else: user_data['email'] = gg_email user_data['username'] = gg_email user_data['full_name'] = gg_name user_data['google_id'] = gg_id user_data['role'] = 'user' user_data['account_type'] = 'google' user_data['password'] = '******' user_data['avatar'] = gg_avatar result = save_new_social_user(data=user_data) LOG.info(result) if result[0]['meta']['code'] != 200 and result[0][ 'meta']['code'] != 201: return result # return build_json_result(None, 409, 'Your email is used') isOk, message = save_refresh_token( get_data(result, 'user_id'), get_data(post_data, 'device_id'), get_data(post_data, 'device_name'), get_data(post_data, 'device_model')) LOG.info(result) result[0]['resfresh_token'] = message return result else: return build_json_result(None, 401, 'Your token is not valid') except Exception as e: # LOG.info(e) return build_json_result( None, 500, 'Błąd serwera. Spróbuj jeszcze raz.', 'a server communication error has occurred please try again later' )
def login_facebook_user(post_data): try: if not 'token' in post_data: return build_json_result( None, 404, 'You need give me your facebook token') else: fb_token = post_data['token'] response = requests.get("https://graph.facebook.com/v3.3/me", params={ 'fields': 'id, name, email', 'access_token': fb_token }, headers=None) data = response.json() LOG.info(data) user_data = {} if data and not 'error' in data: fb_id = data['id'] fb_name = data['name'] if 'email' in data: fb_email = data['email'] else: fb_email = fb_id + '@facebook.com' user = User.query.filter_by(facebook_id=fb_id).first() LOG.info(user) # login success if user: auth_token = User.encode_auth_token( user.id, user.role, user.account_type) isOk, message = save_refresh_token( user.id, get_data(post_data, 'device_id'), get_data(post_data, 'device_name'), get_data(post_data, 'device_model')) if auth_token: # profile = get_user_profile(user.id, user.account_type) response_object = { "user_id": user.id, # 'profile':profile, # 'status': 'success', # 'message': 'Successfully logged in.', 'token': auth_token.decode(), 'refresh_token': message } # LOG.info('aaaaaaaa') profile = get_user_profile(user.id) response_object['profile'] = profile return build_json_result( response_object, 200, 'Zalogowano użytkownika', 'Successfully logged in.') else: user_data['email'] = fb_email user_data['username'] = fb_email user_data['full_name'] = fb_name user_data['facebook_id'] = fb_id user_data['role'] = 'user' user_data['account_type'] = 'facebook' user_data['password'] = '******' user_data['avatar'] = 'http://graph.facebook.com/' + \ fb_id+'/picture?type=square' result = save_new_social_user(data=user_data) isOk, message = save_refresh_token( get_data(result, 'user_id'), get_data(post_data, 'device_id'), get_data(post_data, 'device_name'), get_data(post_data, 'device_model')) result[0]['resfresh_token'] = message # profile = get_user_profile( # get_data(result, 'user_id'), user_data['account_type']) # result[0]['profile'] = profile return result else: return build_json_result(None, 401, 'Your token is not valid') except Exception as e: trace_log() # LOG.info(e) return build_json_result( None, 500, 'Błąd serwera. Spróbuj jeszcze raz.', 'a server communication error has occurred please try again later' )
def post(self): auth_header = request.headers.get('Authorization') post_data = request.get_json(force=True) owner_from = request.headers.get('X-Sub') return Auth.logout_user(auth_header, owner_from, get_data(post_data, 'device_id'))
def post(self): post_data = request.get_json(force=True) LOG.info(post_data) return Auth.renew_token(get_data(post_data, 'device_id'), get_data(post_data, 'refresh_token'))