Esempio n. 1
0
 def post(self):
     # get the post data
     post_data = request.get_json()
     try:
         # fetch the user data
         user = User.query.filter_by(email=post_data.get('email')).first()
         if user and bcrypt.check_password_hash(user.password,
                                                post_data.get('password')):
             refresh_token = user.encode_refresh_token(user.id)
             if refresh_token:
                 responseObject = {
                     'status':
                     'success',
                     'message':
                     'Successfully logged in.',
                     'refresh_token':
                     refresh_token.decode(),
                     'access_token':
                     (User.encode_access_token(user.id)).decode()
                 }
                 return make_response(jsonify(responseObject)), 200
         else:
             responseObject = {
                 'status': 'fail',
                 'message': 'User does not exist.'
             }
             return make_response(jsonify(responseObject)), 404
     except Exception as e:
         print(e)
         responseObject = {'status': 'fail', 'message': 'Try again'}
         return make_response(jsonify(responseObject)), 500
Esempio n. 2
0
 def post(self):
     # get the refresh token
     auth_header = request.headers.get('Authorization')
     if auth_header:
         refresh_token = auth_header.split(" ")[1]
     else:
         refresh_token = ''
     if refresh_token:
         resp = User.decode_token(refresh_token, 'refresh')
         if not isinstance(resp, str):
             responseObject = {
                 'status':
                 'success',
                 'message':
                 'successfully created new access token',
                 'access_token':
                 (User.encode_access_token(resp['sub'])).decode()
             }
             return make_response(jsonify(responseObject)), 200
         else:
             responseObject = {
                 'status': 'fail',
                 'message': 'Provide a valid auth token.'
             }
             return make_response(jsonify(responseObject)), 401
     else:
         responseObject = {
             'status': 'fail',
             'message': 'Provide a valid refresh token.'
         }
         return make_response(jsonify(responseObject)), 403
Esempio n. 3
0
 def refresh_authorized(claims):
     """
     method for refreshing an authorized refresh token
     """
     return make_response(
         jsonify({
             'status':
             'success',
             'message':
             'successfully created new access token',
             'access_token':
             (User.encode_access_token(claims['sub'])).decode()
         })), 200
Esempio n. 4
0
    def post(self):
        # get the post data
        post_data = request.get_json()
        # check if user already exists
        user = User.query.filter((User.email == post_data.get('email')) | (
            User.username == post_data.get('username'))).first()
        if not user:
            try:
                user = User(email=post_data.get('email'),
                            password=post_data.get('password'),
                            username=post_data.get('username'))

                # insert the user
                db.session.add(user)
                db.session.commit()
                # generate the auth token
                refresh_token = user.encode_refresh_token(user.id)
                responseObject = {
                    'status': 'success',
                    'message': 'Successfully registered.',
                    'refresh_token': refresh_token.decode(),
                    'access_token':
                    (User.encode_access_token(user.id)).decode()
                }
                return make_response(jsonify(responseObject)), 201
            except Exception as e:
                responseObject = {
                    'status': 'fail',
                    'message': 'Some error occurred. Please try again.'
                }
                return make_response(jsonify(responseObject)), 401
        else:
            responseObject = {
                'status': 'fail',
                'message': 'Already registered. Please Log in.',
            }
            return make_response(jsonify(responseObject)), 202