Exemplo n.º 1
0
def generate_link(request, data, type='email_verification'):
    """Generate the link based on the type provided.

    Args:
        request (object): Request object
        data (dict): User data
        type (str): The type of token to be generated
            Examples:
                1. email_verification
                2. password_reset

    Returns:
        Tuple: A link and the token.

    """

    # gets the root url
    url_root = request.url_root

    token_payload = {
        'email': data['email'],
    }

    # generates a token
    token = Encryption.tokenize(token_payload, subject=type, minutes=10)

    # maps the methods with link type
    mapper = {
        'password_reset': password_reset_link(url_root, token),
        'email_verification': email_verification_link(url_root, token)
    }

    return mapper[type]
Exemplo n.º 2
0
    def test_detokenize_with_expired_token_fails(self):
        """Test token decoding with no token"""

        with pytest.raises(CustomError) as e_info:
            token = Encryption.tokenize(USER_DATA,
                                        subject='Testing',
                                        minutes=-10)
            Encryption.detokenize(token)
Exemplo n.º 3
0
    def test_detokenize_succeeds(self):
        """Test token decoding"""

        token = Encryption.tokenize(USER_DATA, subject='Testing', minutes=10)

        decoded_token = Encryption.detokenize(token)

        assert decoded_token['data'] == DECODED_TOKEN['data']
        assert decoded_token['aud'] == DECODED_TOKEN['aud']
        assert decoded_token['iss'] == DECODED_TOKEN['iss']
        assert decoded_token['sub'] == DECODED_TOKEN['sub']
Exemplo n.º 4
0
    def post(self):
        """Post request for user login"""

        # initialize the schema
        schema = UserSchema()

        # get the request details as json
        request_json = request.get_json()

        # serialize and find the user data in the database
        user_details = schema.load_into_schema(request_json, partial=True)
        found_user = User.query_(email=user_details['email'], deleted=False)\
            .first()

        # throw an error if not found
        if not found_user:
            return {'status': 'error', 'message': MESSAGES['NOT_FOUND']}, 404

        # deserialize the user data if found, and verify the password
        user = schema.dump(found_user).data
        is_match = Encryption.verify(user['password'],
                                     user_details['password'])

        # if password did not match throw an error
        if not is_match:
            return {
                'status': 'error',
                'message': MESSAGES['UNAUTHORIZED']
            }, 401

        else:

            # format the data and generate a JWT token.
            formatted_data = format_user(user)
            token = Encryption.tokenize(
                formatted_data, subject='User_Login', days=14)

            return {
                'status': 'success',
                'message': MESSAGES['LOGIN'],
                'data': {
                    'token': token,
                }
            }, 200
Exemplo n.º 5
0
FIXTURE_NEW_USER_TWO = {
    'first_name':
    'Test',
    'last_name':
    'User',
    'username':
    PushID().next_id()[8:],
    'email':
    '*****@*****.**',
    'password':
    Encryption.hash('Password@1234'),
    'verified':
    False,
    'token':
    Encryption.tokenize(
        dict(email='*****@*****.**'),
        subject='Email_verification',
        minutes=5),
    'password_reset':
    Encryption.tokenize(
        dict(email='*****@*****.**'),
        subject='password_reset',
        minutes=5)
}

INCOMPLETE_USER = {
    'firstname': 'i',
    'lastname': 'i',
    'email': '*****@*****.**',
    'password': '******'
}
Exemplo n.º 6
0
 def _encrypt(payload, subject, **kwargs):
     return Encryption.tokenize(payload, subject=subject, **kwargs)
Exemplo n.º 7
0
    def test_tokenize_succeeds(self):
        """Test token generation"""

        token = Encryption.tokenize(USER_DATA, subject='Testing', minutes=10)

        assert type(token) == str