コード例 #1
0
ファイル: views.py プロジェクト: zenjutahi/WeConnect3-db
def logout():
    """ logout a user """
    jti = get_raw_jwt()['jti']
    user_identity = get_jwt_identity()
    tokenlist = TokenBlacklist(token=jti, user_identity=user_identity)
    tokenlist.save()
    return jsonify({'message': 'User Successfully logged out'}), 200
コード例 #2
0
 def test_is_token_revoked(self):
     user = User('email', 'password')
     db.session.add(user)
     db.session.commit()
     refresh_token = create_refresh_token(identity=user)
     blacklisted_token = TokenBlacklist(refresh_token, current_app.config['JWT_IDENTITY_CLAIM'])
     db.session.add(blacklisted_token)
     db.session.commit()
     decoded_token = decode_token(refresh_token)
     self.assertFalse(TokenBlacklist.is_token_revoked(decoded_token))
コード例 #3
0
def signup():
    data = request.get_json() or {}
    if 'email' not in data or 'password' not in data:
        raise BadRequest('must include email and password fields')
    if User.query.filter_by(email=data['email']).first():
        raise BadRequest('please use a different email address')

    new_user = User(data['email'].lower(), data['password'])
    db.session.add(new_user)
    db.session.commit()
    token = new_user.generate_confirmation_token()
    confirmation_url = f'http://{current_app.config["CLIENT_BASE_URL"]}/auth/confirm/{token}'
    send_email(new_user.email,
               'Confirm Your Account',
               'email/confirm',
               user=new_user,
               url=confirmation_url)

    refresh_token = create_refresh_token(identity=new_user)
    # Add Refresh Token to Blacklist with status not revoked
    blacklisted_token = TokenBlacklist(
        refresh_token, current_app.config['JWT_IDENTITY_CLAIM'])
    db.session.add(blacklisted_token)
    db.session.commit()
    response_data = {
        'access_token':
        create_access_token(identity=new_user,
                            expires_delta=datetime.timedelta(minutes=15))
    }
    response = jsonify(response_data)
    set_refresh_cookies(response, refresh_token)
    return response, 201
コード例 #4
0
 def test_create_blacklisted_token(self):
     user = User('email', 'password')
     db.session.add(user)
     db.session.commit()
     refresh_token = create_refresh_token(identity=user)
     blacklisted_token = TokenBlacklist(refresh_token, current_app.config['JWT_IDENTITY_CLAIM'])
     db.session.add(blacklisted_token)
     db.session.commit()
     decoded_token = decode_token(refresh_token)
     self.assertEqual(blacklisted_token.jti, decoded_token['jti'])
     self.assertEqual(blacklisted_token.user, user)
     self.assertTrue(blacklisted_token.created_at)
     self.assertEqual(blacklisted_token.expires_at, datetime.datetime.fromtimestamp(decoded_token['exp']))
     self.assertFalse(blacklisted_token.revoked)
コード例 #5
0
ファイル: views.py プロジェクト: zenjutahi/WeConnect3-db
def change_password():
    try:
        required_fields = ['old_password', 'new_password']
        data = check_blank_key(request.get_json(), required_fields)
    except AssertionError as err:
        msg = err.args[0]
        return jsonify({"message": msg}), 422
    old_password = validate_auth_data_null(data.get('old_password'))
    new_password = validate_auth_data_null(data.get('new_password'))
    jti = get_raw_jwt()['jti']

    if not old_password or not new_password:
        return jsonify({'message':
                        'Enter Valid Data: Email and password'}), 400
    current_user = get_jwt_identity()
    user = User.query.filter_by(id=current_user).first()
    if not user.verify_password(old_password):
        return jsonify(
            {'message': 'Enter Valid Password: Old password is wrong'}), 401
    User.update(User, current_user, password=new_password)
    tokenlist = TokenBlacklist(token=jti, user_identity=current_user)
    tokenlist.save()
    return jsonify({'message': 'Password Successfully Changed'}), 201
コード例 #6
0
ファイル: utils.py プロジェクト: xcodinas/covidcore
def add_token_to_database(encoded_token, identity_claim):
    """
    Adds a new token to the database. It is not revoked when it is added.
    :param identity_claim:
    """
    decoded_token = decode_token(encoded_token)
    db_token = TokenBlacklist(
        jti=decoded_token['jti'],
        token_type=decoded_token['type'],
        user_identity=decoded_token[identity_claim],
        expires=datetime.datetime.fromtimestamp(decoded_token['exp']),
        revoked=False,
    )
    db.session.add(db_token)
    db.session.commit()
コード例 #7
0
def store_token(decoded_token):
    jti = decoded_token['jti']
    token_type = decoded_token['type']
    user_identity = decoded_token['identity']
    expires = datetime.fromtimestamp(decoded_token['exp'])

    db_token = TokenBlacklist(
        jti=jti,
        token_type=token_type,
        user_identity=user_identity,
        expires=expires,
        revoked=True,
    )

    db.session.add(db_token)
コード例 #8
0
ファイル: auth_utils.py プロジェクト: HenryYDJ/flaskAPI
def add_token_to_db(encoded_token, identity_claim):
    decoded_token = decode_token(encoded_token)

    jti = decoded_token['jti']
    token_type = decoded_token['type']
    user_id = decoded_token[identity_claim].get('id')
    expires = posix_utc_to_datetime(decoded_token['exp'])
    revoked = False

    db_token = TokenBlacklist(jti=jti,
                              token_type=token_type,
                              user_id=user_id,
                              expires=expires,
                              revoked=revoked)
    db.session.add(db_token)
    db.session.commit()
コード例 #9
0
def add_token_to_database(decoded_token, identity_claim):
    """
    Adds a decoded token to the database.
    :param decoded_token:
    :param identity_claim:
    :return:
    """
    jti = decoded_token['jti']
    token_type = decoded_token['type']
    user_identity = decoded_token[identity_claim]
    expires = _epoch_utc_to_datetime(decoded_token['exp'])

    db_token = TokenBlacklist(
        jti=jti,
        token_type=token_type,
        user_identity=user_identity,
        expires=expires,
    )
    db.session.add(db_token)
    db.session.commit()
コード例 #10
0
def add_token_to_database(encoded_token, identity_claim):
    """
    Adds a new token to the database. It is not revoked when it is added.
    :param identity_claim: configured key to get user identity
    """
    decoded_token = decode_token(encoded_token)
    jti = decoded_token["jti"]
    token_type = decoded_token["type"]
    user_identity = decoded_token[identity_claim]
    expires = datetime.fromtimestamp(decoded_token["exp"])
    revoked = False

    db_token = TokenBlacklist(
        jti=jti,
        token_type=token_type,
        user_id=user_identity,
        expires=expires,
        revoked=revoked,
    )
    db.session.add(db_token)
    db.session.commit()
コード例 #11
0
def add_token_to_database(encoded_token, identity_claim):
    """
    Adds a new token to the database. It is not revoked when it is added.
    :param identity_claim:
    """
    decoded_token = decode_token(encoded_token)
    jti = decoded_token['jti']
    token_type = decoded_token['type']
    user_identity = decoded_token[identity_claim]
    expires = _epoch_utc_to_datetime(decoded_token['exp'])
    revoked = False

    db_token = TokenBlacklist(
        jti=jti,
        token_type=token_type,
        user_identity=user_identity,
        expires=expires,
        revoked=revoked,
    )
    db.session.add(db_token)
    db.session.commit()
コード例 #12
0
def signin():
    data = request.get_json()
    if 'email' not in data or 'password' not in data:
        raise BadRequest('must include email and password fields')

    user = User.query.filter_by(email=data['email']).first()
    if user and user.verify_password(data['password']):
        refresh_token = create_refresh_token(identity=user)
        # Add Refresh Token to Blacklist with status not revoked
        blacklisted_token = TokenBlacklist(
            refresh_token, current_app.config['JWT_IDENTITY_CLAIM'])
        db.session.add(blacklisted_token)
        db.session.commit()
        response_data = {
            'access_token':
            create_access_token(identity=user,
                                expires_delta=datetime.timedelta(minutes=15))
        }
        response = jsonify(response_data)
        set_refresh_cookies(response, refresh_token)
        return response, 200

    raise Unauthorized('Bad username or password')