Ejemplo n.º 1
0
def check_auth(username: str, password: str):
    auth_user = cmdbus.dispatch(GetUserCommand(username))
    if not auth_user:
        return None
    if auth_user.type == 'guest':
        return auth_user
    is_valid = cmdbus.dispatch(ValidateUserPasswordCommand(
        auth_user, password))
    return auth_user if is_valid else None
Ejemplo n.º 2
0
 def handle(self):
     highscore = cmdbus.dispatch(GetOsrsHighScoreCommand(self.nickname))
     if not highscore:
         raise ValueError('Account does not exist')
     account = self.account_service.create(self.nickname)
     evntbus.emit(AccountCreatedEvent(account))
     return account
Ejemplo n.º 3
0
def get_account(slug) -> Response:
    try:
        account = cmdbus.dispatch(GetAccountCommand(slug))
        return jsonify(account)
    except NotFoundError as e:
        abort(HTTPStatus.NOT_FOUND, str(e))
    except ValueError as e:
        abort(HTTPStatus.BAD_REQUEST, str(e))
Ejemplo n.º 4
0
def check_jwt(token: str) -> typing.Union[Jwt, None]:
    try:
        jwt = cmdbus.dispatch(DecodeJwtCommand(token))  # type: Jwt
    except InvalidTokenError:
        return None
    if not jwt.valid():
        # TODO: TokenExpiredError
        return None
    return jwt
Ejemplo n.º 5
0
def put_account(slug) -> Response:
    body = request.get_json()
    try:
        account = cmdbus.dispatch(UpdateAccountCommand(slug, body))
        return jsonify(account)
    except NotFoundError as e:
        abort(HTTPStatus.NOT_FOUND, str(e))
    except ValueError as e:
        abort(HTTPStatus.BAD_REQUEST, str(e))
Ejemplo n.º 6
0
def get_highscore(slug: str, id: str) -> Response:
    try:
        highscore = cmdbus.dispatch(GetHighScoreCommand(
            slug, id
        ))
        return jsonify(highscore)
    except NotFoundError as e:
        abort(HTTPStatus.NOT_FOUND, str(e))
    except ValueError as e:
        abort(HTTPStatus.BAD_REQUEST, str(e))
Ejemplo n.º 7
0
def post_account() -> Response:
    data = request.get_json()
    try:
        account = cmdbus.dispatch(CreateAccountCommand(data['nickname']))
        return jsonify(account)
    except DuplicateError:
        abort(HTTPStatus.BAD_REQUEST,
              'Account already exists: {}'.format(data['nickname']))
    except ValueError as e:
        abort(HTTPStatus.BAD_REQUEST, str(e))
Ejemplo n.º 8
0
def post_highscore(slug: str) -> Response:
    data = request.get_json()
    try:
        highscore = cmdbus.dispatch(CreateHighScoreCommand(
            slug, data['skills']
        ))
        return jsonify(highscore)
    except NotFoundError as e:
        abort(HTTPStatus.NOT_FOUND, str(e))
    except ValueError as e:
        abort(HTTPStatus.BAD_REQUEST, str(e))
Ejemplo n.º 9
0
def get_accounts() -> Response:
    last_ran_before = request.args.get('last_ran_before')
    runs_unchanged_min = request.args.get('runs_unchanged_min', type=int)
    runs_unchanged_max = request.args.get('runs_unchanged_max', type=int)
    prioritise = request.args.get('prioritise', False, type=bool)
    try:
        accounts = cmdbus.dispatch(
            GetAccountsCommand(last_ran_before, runs_unchanged_min,
                               runs_unchanged_max, prioritise))
        return jsonify(accounts)
    except ValueError as e:
        abort(HTTPStatus.BAD_REQUEST, str(e))
Ejemplo n.º 10
0
def get_highscores(slug: str) -> Response:
    created_after = request.args.get('created_after')
    created_before = request.args.get('created_before')
    skills = request.args.get('skills')
    if skills:
        skills = skills.split(',')
    try:
        highscores = cmdbus.dispatch(GetHighScoresCommand(
            slug, created_after, created_before, skills
        ))
        return jsonify(highscores)
    except ValueError as e:
        abort(HTTPStatus.BAD_REQUEST, str(e))
Ejemplo n.º 11
0
    def decorated(*args, **kwargs):
        auth_header = request.headers.get('Authorization')
        if not auth_header:
            raise Unauthorized('Missing Authorization Header')

        parts = auth_header.split()
        if len(parts) != 2 or parts[0] != 'Bearer':
            raise Unauthorized('Invalid Authorization Header')

        token = parts[1]
        jwt = check_jwt(token)
        if not jwt:
            raise Unauthorized('Invalid token')
        auth_user = cmdbus.dispatch(GetUserFromJwtCommand(jwt))
        if not auth_user:
            raise Unauthorized('Invalid token')
        content_valid = cmdbus.dispatch(
            ValidateJwtContentCommand(auth_user, jwt))
        if not content_valid:
            raise Unauthorized('Invalid token')
        g.jwt = jwt
        g.user = auth_user
        return f(*args, **kwargs)
Ejemplo n.º 12
0
def get_highscore_count() -> Response:
    count = cmdbus.dispatch(GetHighScoreCountCommand())
    return jsonify(count)
Ejemplo n.º 13
0
def test_dispatch():
    cmd = AddCommand(3, 5)
    result = cmdbus.dispatch(cmd)
    assert result is 8
Ejemplo n.º 14
0
 def handle(self):
     user_id = self.jwt_service.user_id_from_subject(self.jwt.subject)
     return cmdbus.dispatch(GetUserByIdCommand(user_id))
Ejemplo n.º 15
0
 def handle(self):
     permissions = cmdbus.dispatch(GeneratePermissionsCommand(self.user))
     return cmdbus.dispatch(
         CheckPermissionCommand(self.scope, permissions, self.required)
     )
Ejemplo n.º 16
0
def authenticate() -> Response:
    try:
        jwt = cmdbus.dispatch(CreateJwtCommand(user()))  # type: Jwt
        return jsonify({'token': jwt.encode()})
    except ValueError as e:
        abort(HTTPStatus.BAD_REQUEST, str(e))
Ejemplo n.º 17
0
def get_account_count() -> Response:
    count = cmdbus.dispatch(GetAccountCountCommand())
    return jsonify(count)
Ejemplo n.º 18
0
 def decorated(*args, **kwargs):
     if not cmdbus.dispatch(
             CheckUserPermissionCommand(user(), self.scope,
                                        self.required)):
         raise Forbidden()
     return f(*args, **kwargs)