Browse Source

add whoami API endpoint

merge-requests/165/head
pollev 2 months ago
parent
commit
b6ba19e281
  1. 17
      tildes/openapi_beta.yaml
  2. 1
      tildes/tildes/routes.py
  3. 13
      tildes/tildes/views/api/beta/auth.py

17
tildes/openapi_beta.yaml

@ -52,6 +52,23 @@ paths:
$ref: "#/components/responses/ValidationError"
"401":
$ref: "#/components/responses/AuthenticationError"
/whoami:
get:
summary: Get username for currently logged in user
responses:
"200":
description: Username of the currently logged in user
content:
application/json:
schema:
type: object
required:
- msg
properties:
msg:
type: string
/topics:
get:
summary: Get a list of topics

1
tildes/tildes/routes.py

@ -135,6 +135,7 @@ def includeme(config: Configurator) -> None:
with config.route_prefix_context("/api/beta"):
config.add_route("apibeta.login", "/login")
config.add_route("apibeta.whoami", "/whoami")
config.add_route("apibeta.topics", "/topics")
config.add_route("apibeta.topic", "/topic/{topic_id36}")
config.add_route("apibeta.user", "/user/{username}")

13
tildes/tildes/views/api/beta/auth.py

@ -109,3 +109,16 @@ def login(request: Request) -> dict:
token = jwt_policy.create_jwt_token(user.user_id)
return {"token": token}
@view_config(route_name="apibeta.whoami", openapi=True, renderer="json")
def whoami(request: Request) -> dict:
"""Endpoint to let a user verify that they are authenticated."""
user = request.user
if not user:
msg = "You are not logged in"
else:
msg = f"You are logged in as {user.username}"
return {"msg": msg}
Loading…
Cancel
Save