def oauth_login(provider): """Provides facebook authorization. Retrieves user info from facebook, check if there is user with retrieved from facebook user id, :param provider: Oauth provider (Facebook by default) - if yes: skips to next step - if not: checks if there is user with retrieved email - if yes: adds oauth credentials to this user - if not: creates new user After all function logging in user into app and return it's params """ access_token_url = 'https://graph.facebook.com/oauth/access_token' graph_api_url = 'https://graph.facebook.com/v2.5/me?fields=email,'\ 'first_name,last_name,id,picture.type(large)' params = { 'client_id': request.json['clientId'], 'redirect_uri': request.json['redirectUri'], 'client_secret': app.config['OAUTH_CREDENTIALS']['facebook']['secret'], 'code': request.json['code'] } resource = requests.get(access_token_url, params=params) access_token = dict(parse_qsl(resource.text)) resource = requests.get(graph_api_url, params=access_token) profile = json.loads(resource.text) nickname = '{}{}'.format(profile['last_name'], int(time.time())) logger.info(profile['picture']['data']['url']) user, is_registered = ecomap_user.facebook_register( profile['first_name'], profile['last_name'], nickname, profile['email'], provider, profile['id']) if not db.get_user_avatar(user.uid)[0]: db.insert_user_avatar(user.uid, profile['picture']['data']['url']) login_user(user, remember=True) response = jsonify(iat="???", token=user.get_auth_token(), email=user.email, name=user.first_name, surname=user.last_name, registered=is_registered) response.set_cookie('id', bytes(user.uid), max_age=COOKIE_MAX_AGE) response.set_cookie('role', bytes(user.role), max_age=COOKIE_MAX_AGE) return response
def oauth_login(provider): """Provides facebook authorization. Retrieves user info from facebook, check if there is user with retrieved from facebook user id, if yes: skips to next step if not: checks if there is user with retrieved email if yes: adds oauth credentials to this user if not: creates new user After all function loggins user and return it's params """ access_token_url = 'https://graph.facebook.com/oauth/access_token' graph_api_url = 'https://graph.facebook.com/v2.5/me?fields=email,'\ 'first_name,last_name,id,picture.type(large)' params = { 'client_id': request.json['clientId'], 'redirect_uri': request.json['redirectUri'], 'client_secret': app.config['OAUTH_CREDENTIALS']['facebook']['secret'], 'code': request.json['code'] } resource = requests.get(access_token_url, params=params) access_token = dict(parse_qsl(resource.text)) resource = requests.get(graph_api_url, params=access_token) profile = json.loads(resource.text) logger.info(profile['picture']['data']['url']) user = ecomap_user.facebook_register(profile['first_name'], profile['last_name'], profile['email'], provider, profile['id']) db.insert_user_avatar(user.uid, profile['picture']['data']['url']) login_user(user, remember=True) response = jsonify(id=user.uid, name=user.first_name, surname=user.last_name, role=user.role, iat="???", token=user.get_auth_token(), email=user.email) return response
def add_profile_photo(): """Controller provides add and edit function for user's profile photo. :return: json object with image path if success or 400 error message """ response = jsonify(), 400 extension = '.png' f_name = 'profile_id%s' % current_user.uid + extension static_url = '/uploads/user_profile/userid_%d/' % current_user.uid f_path = os.environ['STATICROOT'] + static_url if request.method == 'POST': img_file = request.files['file'] if img_file and validator.validate_image_file(img_file): if not os.path.exists(f_path): os.makedirs(os.path.dirname(f_path + f_name)) img_file.save(os.path.join(f_path, f_name)) img_path = static_url + f_name db.insert_user_avatar(current_user.uid, img_path) response = json.dumps({'added_file': img_path}) else: response = jsonify(error='error with import file'), 400 return response
def add_profile_photo(): """Controller provides add and edit function for user's profile photo. :content-type: multipart/form-data :fparam name: name of image file ('photo.jpg') :fparam file: image file in base64. Content-Type: image/png :rtype: JSON :return: json object with image path if success or 400 error message - If request data is invalid: ``{'error': 'error with import file'}`` - If all ok: ``{added_file: "/uploads/user_profile/userid_6/profile_id6.png"}`` :statuscode 400: request is invalid :statuscode 200: photo was successfully added """ response = jsonify(), 400 extension = '.png' f_name = 'profile_id%s' % current_user.uid + extension static_url = '/uploads/user_profile/userid_%d/' % current_user.uid f_path = os.environ['STATICROOT'] + static_url if request.method == 'POST': img_file = request.files['file'] if img_file and validator.validate_image_file(img_file): if not os.path.exists(f_path): os.makedirs(os.path.dirname('%s%s' % (f_path, f_name))) img_file.save(os.path.join(f_path, f_name)) img_path = '%s%s' % (static_url, f_name) db.insert_user_avatar(current_user.uid, img_path) response = json.dumps({'added_file': img_path}) else: response = jsonify(error='error with import file'), 400 return response
def oauth_login(provider): """Provides facebook authorization. Retrieves user info from facebook, check if there is user with retrieved from facebook user id, :param provider: Oauth provider (Facebook by default) - if yes: skips to next step - if not: checks if there is user with retrieved email - if yes: adds oauth credentials to this user - if not: creates new user After all function logging in user into app and return it's params """ access_token_url = 'https://graph.facebook.com/oauth/access_token' graph_api_url = 'https://graph.facebook.com/v2.5/me?fields=email,'\ 'first_name,last_name,id,picture.type(large)' params = { 'client_id': request.json['clientId'], 'redirect_uri': request.json['redirectUri'], 'client_secret': app.config['OAUTH_CREDENTIALS']['facebook']['secret'], 'code': request.json['code'] } resource = requests.get(access_token_url, params=params) access_token = dict(parse_qsl(resource.text)) resource = requests.get(graph_api_url, params=access_token) profile = json.loads(resource.text) nickname = '{}{}'.format(profile['last_name'], int(time.time())) logger.info(profile['picture']['data']['url']) user, is_registered = ecomap_user.facebook_register(profile['first_name'], profile['last_name'], nickname, profile['email'], provider, profile['id']) if not db.get_user_avatar(user.uid)[0]: db.insert_user_avatar(user.uid, profile['picture']['data']['url']) login_user(user, remember=True) response = jsonify(iat="???", token=user.get_auth_token(), email=user.email, name=user.first_name, surname=user.last_name, registered=is_registered) response.set_cookie('id', bytes(user.uid), max_age=COOKIE_MAX_AGE) response.set_cookie('role', bytes(user.role), max_age=COOKIE_MAX_AGE) return response