Skip to content

Commit afa7a6f

Browse files
committed
add endpoint to send password reset emails
1 parent 3b61430 commit afa7a6f

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

backend/app/keycloak_auth.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
import logging
44
from datetime import datetime
5-
from typing import Optional
5+
from typing import List, Optional
66

77
from fastapi import Depends, HTTPException, Security
88
from fastapi.security import APIKeyCookie, APIKeyHeader, OAuth2AuthorizationCodeBearer
@@ -473,6 +473,22 @@ async def update_user(
473473
return updated_user
474474

475475

476+
def send_update_account(email: str, actions: List[str]):
477+
"""Send an update account email to the user."""
478+
keycloak_admin = KeycloakAdmin(
479+
server_url=settings.auth_server_url,
480+
username=settings.keycloak_username,
481+
password=settings.keycloak_password,
482+
realm_name=settings.keycloak_realm_name,
483+
user_realm_name=settings.keycloak_user_realm_name,
484+
# client_secret_key=settings.auth_client_secret,
485+
# client_id=settings.keycloak_client_id,
486+
verify=True,
487+
)
488+
user_id = keycloak_admin.get_user_id(username=email)
489+
keycloak_admin.send_update_account(user_id, actions)
490+
491+
476492
def delete_user(email: str):
477493
"""Create a user in Keycloak."""
478494
keycloak_admin = KeycloakAdmin(

backend/app/routers/keycloak.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
keycloak_openid,
1010
oauth2_scheme,
1111
retreive_refresh_token,
12+
send_update_account,
1213
)
1314
from app.models.tokens import TokenDB
1415
from app.models.users import UserDB, UserLogin
@@ -17,6 +18,7 @@
1718
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
1819
from jose import ExpiredSignatureError, JWTError, jwt
1920
from keycloak.exceptions import KeycloakAuthenticationError, KeycloakGetError
21+
from pydantic import EmailStr
2022
from starlette.responses import RedirectResponse
2123

2224
router = APIRouter()
@@ -42,6 +44,11 @@ async def loginGet() -> RedirectResponse:
4244
)
4345

4446

47+
@router.post("/reset-password")
48+
async def reset_password(email: EmailStr):
49+
send_update_account(email, ["UPDATE_PASSWORD"])
50+
51+
4552
@router.get("/logout")
4653
async def logout(
4754
credentials: HTTPAuthorizationCredentials = Security(security),

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,27 @@ export class AuthService {
5353
});
5454
}
5555

56+
/**
57+
* Reset Password
58+
* @param email
59+
* @returns any Successful Response
60+
* @throws ApiError
61+
*/
62+
public static resetPasswordApiV2AuthResetPasswordPost(
63+
email: string,
64+
): CancelablePromise<any> {
65+
return __request({
66+
method: 'POST',
67+
path: `/api/v2/auth/reset-password`,
68+
query: {
69+
'email': email,
70+
},
71+
errors: {
72+
422: `Validation Error`,
73+
},
74+
});
75+
}
76+
5677
/**
5778
* Logout
5879
* Logout of keycloak.

openapi.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11992,6 +11992,47 @@
1199211992
}
1199311993
}
1199411994
},
11995+
"/api/v2/auth/reset-password": {
11996+
"post": {
11997+
"tags": [
11998+
"auth"
11999+
],
12000+
"summary": "Reset Password",
12001+
"operationId": "reset_password_api_v2_auth_reset_password_post",
12002+
"parameters": [
12003+
{
12004+
"required": true,
12005+
"schema": {
12006+
"title": "Email",
12007+
"type": "string",
12008+
"format": "email"
12009+
},
12010+
"name": "email",
12011+
"in": "query"
12012+
}
12013+
],
12014+
"responses": {
12015+
"200": {
12016+
"description": "Successful Response",
12017+
"content": {
12018+
"application/json": {
12019+
"schema": {}
12020+
}
12021+
}
12022+
},
12023+
"422": {
12024+
"description": "Validation Error",
12025+
"content": {
12026+
"application/json": {
12027+
"schema": {
12028+
"$ref": "#/components/schemas/HTTPValidationError"
12029+
}
12030+
}
12031+
}
12032+
}
12033+
}
12034+
}
12035+
},
1199512036
"/api/v2/auth/logout": {
1199612037
"get": {
1199712038
"tags": [

0 commit comments

Comments
 (0)