Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: check if user exists before create a new refresh token #3076

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading