Add /auth/ping and prepare for frontend dev

This commit is contained in:
Tulir Asokan 2018-11-02 15:16:30 +02:00
parent a584cba794
commit ec22e5eba7
6 changed files with 41 additions and 6 deletions

View File

@ -22,7 +22,7 @@ from mautrix.types import UserID
from mautrix.util.signed_token import sign_token, verify_token
from .base import routes, get_config
from .responses import ErrBadAuth, ErrBodyNotJSON
from .responses import ErrBadAuth, ErrBodyNotJSON, ErrNoToken, ErrInvalidToken
def is_valid_token(token: str) -> bool:
@ -38,7 +38,24 @@ def create_token(user: UserID) -> str:
})
@routes.post("/login")
@routes.post("/auth/ping")
async def ping(request: web.Request) -> web.Response:
token = request.headers.get("Authorization", "")
if not token or not token.startswith("Bearer "):
return ErrNoToken
data = verify_token(get_config()["server.unshared_secret"], token[len("Bearer "):])
if not data:
return ErrInvalidToken
user = data.get("user_id", None)
if not get_config().is_admin(user):
return ErrInvalidToken
return web.json_response({
"username": user,
})
@routes.post("/auth/login")
async def login(request: web.Request) -> web.Response:
try:
data = await request.json()

View File

@ -24,7 +24,7 @@ Handler = Callable[[web.Request], Awaitable[web.Response]]
@web.middleware
async def auth(request: web.Request, handler: Handler) -> web.Response:
if request.path.endswith("/login"):
if "/auth/" in request.path:
return await handler(request)
token = request.headers.get("Authorization", "")
if not token or not token.startswith("Bearer "):

View File

@ -12,7 +12,7 @@ servers:
- url: /_matrix/maubot/v1
paths:
/login:
/auth/login:
post:
operationId: login
summary: Log in with the unshared secret or username+password
@ -45,6 +45,23 @@ paths:
type: string
401:
description: Invalid credentials
/auth/ping:
post:
operationId: ping
summary: Check if the given token is valid
tags: [Authentication]
responses:
200:
description: Token is OK
content:
application/json:
schema:
type: object
properties:
username:
type: string
401:
description: Token is not OK
/plugins:
get:

View File

@ -21,5 +21,6 @@
"last 3 and_chr versions",
"last 2 safari versions",
"last 2 ios_saf versions"
]
],
"proxy": "http://localhost:29316"
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

View File

@ -19,7 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.png">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="theme-color" content="#50D367">
<link rel="manifest" href="%PUBLIC_URL%/manifest.json">