Ejemplo n.º 1
0
def _get_user(ctx: rest.Context, bump_login: bool) -> Optional[model.User]:
    if not ctx.has_header('Authorization'):
        return None

    auth_token = None

    try:
        auth_type, credentials = ctx.get_header('Authorization').split(' ', 1)
        if auth_type.lower() == 'basic':
            username, password = base64.decodebytes(
                credentials.encode('ascii')).decode('utf8').split(':', 1)
            auth_user = _authenticate_basic_auth(username, password)
        elif auth_type.lower() == 'token':
            username, token = base64.decodebytes(
                credentials.encode('ascii')).decode('utf8').split(':', 1)
            auth_user, auth_token = _authenticate_token(username, token)
        else:
            raise HttpBadRequest(
                'ValidationError',
                'Only basic or token HTTP authentication is supported.')
    except ValueError as err:
        msg = (
            'Authorization header values are not properly formed. '
            'Supplied header {0}. Got error: {1}')
        raise HttpBadRequest(
            'ValidationError',
            msg.format(ctx.get_header('Authorization'), str(err)))

    if bump_login and auth_user.user_id:
        users.bump_user_login_time(auth_user)
        if auth_token is not None:
            user_tokens.bump_usage_time(auth_token)
        ctx.session.commit()

    return auth_user
Ejemplo n.º 2
0
def _get_user(ctx: rest.Context, bump_login: bool) -> Optional[model.User]:
    if not ctx.has_header('Authorization'):
        return None

    auth_token = None

    try:
        auth_type, credentials = ctx.get_header('Authorization').split(' ', 1)
        if auth_type.lower() == 'basic':
            username, password = base64.decodebytes(
                credentials.encode('ascii')).decode('utf8').split(':', 1)
            auth_user = _authenticate_basic_auth(username, password)
        elif auth_type.lower() == 'token':
            username, token = base64.decodebytes(
                credentials.encode('ascii')).decode('utf8').split(':', 1)
            auth_user, auth_token = _authenticate_token(username, token)
        else:
            raise HttpBadRequest('ValidationError', '기본 및 토큰 HTTP 인증만을 지원합니다.')
    except ValueError as err:
        msg = ('인증 헤더 값이 적절한 형식이 아닙니다. ' '전달된 헤더 {0}. 오류: {1}')
        raise HttpBadRequest(
            'ValidationError',
            msg.format(ctx.get_header('Authorization'), str(err)))

    if bump_login and auth_user.user_id:
        users.bump_user_login_time(auth_user)
        if auth_token is not None:
            user_tokens.bump_usage_time(auth_token)
        ctx.session.commit()

    return auth_user
Ejemplo n.º 3
0
def _get_user(ctx: rest.Context, bump_login: bool) -> Optional[model.User]:
    if not ctx.has_header("Authorization"):
        return None

    auth_token = None

    try:
        auth_type, credentials = ctx.get_header("Authorization").split(" ", 1)
        if auth_type.lower() == "basic":
            username, password = (
                base64.decodebytes(credentials.encode("ascii"))
                .decode("utf8")
                .split(":", 1)
            )
            auth_user = _authenticate_basic_auth(username, password)
        elif auth_type.lower() == "token":
            username, token = (
                base64.decodebytes(credentials.encode("ascii"))
                .decode("utf8")
                .split(":", 1)
            )
            auth_user, auth_token = _authenticate_token(username, token)
        else:
            raise HttpBadRequest(
                "ValidationError",
                "Only basic or token HTTP authentication is supported.",
            )
    except ValueError as err:
        msg = (
            "Authorization header values are not properly formed. "
            "Supplied header {0}. Got error: {1}"
        )
        raise HttpBadRequest(
            "ValidationError",
            msg.format(ctx.get_header("Authorization"), str(err)),
        )

    if bump_login and auth_user.user_id:
        users.bump_user_login_time(auth_user)
        if auth_token is not None:
            user_tokens.bump_usage_time(auth_token)
        ctx.session.commit()

    return auth_user
Ejemplo n.º 4
0
def _get_user(ctx: rest.Context) -> Optional[model.User]:
    if not ctx.has_header('Authorization'):
        return None

    try:
        auth_type, credentials = ctx.get_header('Authorization').split(' ', 1)
        if auth_type.lower() != 'basic':
            raise HttpBadRequest(
                'ValidationError',
                'Only basic HTTP authentication is supported.')
        username, password = base64.decodebytes(
            credentials.encode('ascii')).decode('utf8').split(':')
        return _authenticate(username, password)
    except ValueError as err:
        msg = ('Basic authentication header value are not properly formed. '
               'Supplied header {0}. Got error: {1}')
        raise HttpBadRequest(
            'ValidationError',
            msg.format(ctx.get_header('Authorization'), str(err)))