Skip to content

Commit 15c47d2

Browse files
authored
Fix refresh token issue when using password flow (#1205)
* deprecate `/login` endpoint and add refresh token saving in `/auth/login` * fix code formatting * update API docs * refactor `TokenDB` logic
1 parent 9a41c8a commit 15c47d2

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

backend/app/routers/authentication.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
)
99
from app.models.datasets import DatasetDBViewList
1010
from app.models.users import UserDB, UserIn, UserLogin, UserOut
11+
from app.routers.utils import save_refresh_token
1112
from beanie import PydanticObjectId
1213
from fastapi import APIRouter, Depends, HTTPException
1314
from keycloak.exceptions import (
@@ -69,6 +70,7 @@ async def save_user(userIn: UserIn):
6970
async def login(userIn: UserLogin):
7071
try:
7172
token = keycloak_openid.token(userIn.email, userIn.password)
73+
await save_refresh_token(token["refresh_token"], userIn.email)
7274
return {"token": token["access_token"]}
7375
# bad credentials
7476
except KeycloakAuthenticationError as e:

backend/app/routers/keycloak.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
retreive_refresh_token,
1212
)
1313
from app.models.tokens import TokenDB
14-
from app.models.users import UserDB, UserIn
14+
from app.models.users import UserDB, UserLogin
15+
from app.routers.utils import save_refresh_token
1516
from fastapi import APIRouter, HTTPException, Security
1617
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
1718
from jose import ExpiredSignatureError, JWTError, jwt
@@ -80,10 +81,11 @@ async def logout(
8081

8182

8283
@router.post("/login")
83-
async def loginPost(userIn: UserIn):
84+
async def loginPost(userIn: UserLogin):
8485
"""Client can use this to login when redirect is not available."""
8586
try:
8687
token = keycloak_openid.token(userIn.email, userIn.password)
88+
await save_refresh_token(token["refresh_token"], userIn.email)
8789
return {"token": token["access_token"]}
8890
# bad credentials
8991
except KeycloakAuthenticationError as e:

backend/app/routers/utils.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import Optional
33

44
from app.models.files import ContentType
5+
from app.models.tokens import TokenDB
56

67

78
def get_content_type(
@@ -23,3 +24,14 @@ def get_content_type(
2324
content_type = "application/octet-stream"
2425
type_main = content_type.split("/")[0] if type(content_type) is str else "N/A"
2526
return ContentType(content_type=content_type, main_type=type_main)
27+
28+
29+
async def save_refresh_token(refresh_token: str, email: str):
30+
"""Store/update refresh token and link to that userid."""
31+
token_exist = await TokenDB.find_one(TokenDB.email == email)
32+
if token_exist is not None:
33+
token_exist.refresh_token = refresh_token
34+
await token_exist.save()
35+
else:
36+
token_created = TokenDB(email=email, refresh_token=refresh_token)
37+
await token_created.insert()

frontend/src/openapi/v2/services/AuthService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* istanbul ignore file */
22
/* tslint:disable */
33
/* eslint-disable */
4-
import type { UserIn } from '../models/UserIn';
4+
import type { UserLogin } from '../models/UserLogin';
55
import type { CancelablePromise } from '../core/CancelablePromise';
66
import { request as __request } from '../core/request';
77

@@ -40,7 +40,7 @@ export class AuthService {
4040
* @throws ApiError
4141
*/
4242
public static loginPostApiV2AuthLoginPost(
43-
requestBody: UserIn,
43+
requestBody: UserLogin,
4444
): CancelablePromise<any> {
4545
return __request({
4646
method: 'POST',

openapi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11912,7 +11912,7 @@
1191211912
"content": {
1191311913
"application/json": {
1191411914
"schema": {
11915-
"$ref": "#/components/schemas/UserIn"
11915+
"$ref": "#/components/schemas/UserLogin"
1191611916
}
1191711917
}
1191811918
},

0 commit comments

Comments
 (0)