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
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': 'were made' } return make_response(jsonify(responseObject)), 201 except Exception as e: responseObject = { 'status': 'fail', 'message': "Something went wrong" } return make_response(jsonify(responseObject)), 401 else: responseObject = { 'status': 'fail', 'message': 'Already registered. Please Log in.', } return make_response(jsonify(responseObject)), 202
def test_encode_auth_token(self): user = User(username='******', email='*****@*****.**', password='******') db.session.add(user) db.session.commit() auth_token = user.encode_auth_token(user.id) self.assertTrue(isinstance(auth_token, bytes))
def test_user_creation(self): user = User(username='******', email='*****@*****.**', password='******') db.session.add(user) db.session.commit() verify_user = User.query.filter_by(email='*****@*****.**').first()
def get(self): # get the auth token auth_header = request.headers.get('Authorization') if auth_header: auth_token = auth_header.split(" ")[1] else: auth_token = '' if auth_token: resp = User.decode_token(auth_token, 'access') if not isinstance(resp, str): user = User.query.filter_by(id=resp['sub']).first() responseObject = { 'status': 'success', 'data': { 'user_id': user.id, 'email': user.email, 'admin': user.admin, 'registered_on': user.registered_on } } return make_response(jsonify(responseObject)), 200 responseObject = {'status': 'fail', 'message': resp} return make_response(jsonify(responseObject)), 401 else: responseObject = { 'status': 'fail', 'message': 'Provide a valid access token.' } return make_response(jsonify(responseObject)), 401
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
def post(self): # get auth token auth_header = request.headers.get('Authorization') if auth_header: auth_token = auth_header.split(" ")[1] else: auth_token = '' if auth_token: resp = User.decode_token(auth_token, 'refresh') if not isinstance(resp, str): # mark the token as blacklisted blacklist_token = BlacklistToken(token=auth_token) try: # insert the token db.session.add(blacklist_token) db.session.commit() responseObject = { 'status': 'success', 'message': 'Successfully logged out.' } return make_response(jsonify(responseObject)), 200 except Exception as e: responseObject = {'status': 'fail', 'message': e} return make_response(jsonify(responseObject)), 200 else: responseObject = {'status': 'fail', 'message': resp} return make_response(jsonify(responseObject)), 401 else: responseObject = { 'status': 'fail', 'message': 'Provide a valid auth token.' } return make_response(jsonify(responseObject)), 403
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
def test_registered_with_already_registered_username(self): """ Test registration with already registered user""" user = User(email='*****@*****.**', password='******', username='******') db.session.add(user) db.session.commit() with self.client: response = register_user(self, 'yuanti', '*****@*****.**', 'freewifi') data = json.loads(response.data.decode()) self.assertTrue(data['status'] == 'fail') self.assertTrue(response.content_type == 'application/json') self.assertEqual(response.status_code, 202)
def protected_resource(auth_token, authorized_callback, token_needed='access', unauthorized_callback=default_unauthorized_callback): """ Method used to protect a resource. This is used in a view to determine whether they are authorized or unauthorized. User passes an$ :param auth_token: Token the user is attempting to authorized with. :param authorized_callback: This is a callback method we used to return a json string to the view to be presented. The callback me$ :param unauthorized_callback: If there is still information that we want to present if the token is not authorized we can pass a c$ """ # If a valid auth_token is present if auth_token: auth_token = auth_token.split(" ")[1] resp = User.decode_token(auth_token, token_needed) #when decoding the token, if the response is not a string, else we have an error and pass it to the default_expired_token_$ if not isinstance(resp, str): return authorized_callback(resp) elif resp == 'Expired': return default_needs_fresh_token_callback return unauthorized_callback('Unauthorized') else: return unauthorized_callback('Unauthorized')