Skip to content

Commit

Permalink
bugfix: check if user exists before create a new refresh token (#3076)
Browse files Browse the repository at this point in the history
check if user exists before create a new refresh token
  • Loading branch information
Cristhianzl authored Jul 31, 2024
1 parent 61c921e commit d7aed90
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/backend/base/langflow/api/v1/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,14 @@ async def refresh_token(
request: Request,
response: Response,
settings_service: "SettingsService" = Depends(get_settings_service),
db: Session = Depends(get_session),
):
auth_settings = settings_service.auth_settings

token = request.cookies.get("refresh_token_lf")

if token:
tokens = create_refresh_token(token)
tokens = create_refresh_token(token, db)
response.set_cookie(
"refresh_token_lf",
tokens["refresh_token"],
Expand Down
8 changes: 7 additions & 1 deletion src/backend/base/langflow/services/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,13 @@ def create_refresh_token(refresh_token: str, db: Session = Depends(get_session))
)
user_id: UUID = payload.get("sub") # type: ignore
token_type: str = payload.get("type") # type: ignore
if user_id is None or token_type is None:

if user_id is None or token_type == "":
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid refresh token")

user_exists = get_user_by_id(db, user_id)

if user_exists is None:
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid refresh token")

return create_user_tokens(user_id, db)
Expand Down
43 changes: 26 additions & 17 deletions src/frontend/src/controllers/API/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ function ApiInterceptor() {
await tryToRenewAccessToken(error);

const accessToken = cookies.get(LANGFLOW_ACCESS_TOKEN);

if (!accessToken && error?.config?.url?.includes("login")) {
return Promise.reject(error);
}

await remakeRequest(error);
setSaveLoading(false);
authenticationErrorCount = 0;
}
}
await clearBuildVerticesState(error);
return Promise.reject(error);
if (
error?.response?.status !== 401 &&
error?.response?.status !== 403
) {
return Promise.reject(error);
}
},
);

Expand Down Expand Up @@ -141,21 +141,30 @@ function ApiInterceptor() {
}

async function tryToRenewAccessToken(error: AxiosError) {
try {
if (window.location.pathname.includes("/login")) return;
mutationRenewAccessToken({});
} catch (error) {
clearBuildVerticesState(error);
mutationLogout(undefined, {
onSuccess: () => {
logout();
if (window.location.pathname.includes("/login")) return;
mutationRenewAccessToken(
{},
{
onSuccess: async (data) => {
authenticationErrorCount = 0;
await remakeRequest(error);
setSaveLoading(false);
authenticationErrorCount = 0;
},
onError: (error) => {
console.error(error);
mutationLogout(undefined, {
onSuccess: () => {
logout();
},
onError: (error) => {
console.error(error);
},
});
return Promise.reject("Authentication error");
},
});
return Promise.reject("Authentication error");
}
},
);
}

async function clearBuildVerticesState(error) {
Expand Down

0 comments on commit d7aed90

Please sign in to comment.