Skip to content

Commit

Permalink
Hashed user ID cookie improvements (#20232)
Browse files Browse the repository at this point in the history
* refetch JWT cookie 20s after load

* set cookie on login

* make the cookie trigger at the right place
  • Loading branch information
filiptronicek authored Sep 19, 2024
1 parent 92f3ec0 commit abb191f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
24 changes: 14 additions & 10 deletions components/dashboard/src/user-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ const UserContext = createContext<{
setUser: () => null,
});

const refetchCookie = async () => {
await fetch("/api/auth/jwt-cookie", {
credentials: "include",
})
.then((resp) => resp.text())
.then((text) => console.log(`Completed JWT Cookie refresh: ${text}`))
.catch((err) => {
console.log("Failed to update jwt-cookie", err);
});
};

const UserContextProvider: React.FC = ({ children }) => {
const [user, setUser] = useState<User>();

Expand Down Expand Up @@ -46,16 +57,9 @@ const UserContextProvider: React.FC = ({ children }) => {
const frequencyMs = 1000 * 60 * 5; // 5 mins
if (!_gp.jwttimer) {
// Store the timer on the window, to avoid queuing up multiple
_gp.jwtTimer = setInterval(() => {
fetch("/api/auth/jwt-cookie", {
credentials: "include",
})
.then((resp) => resp.text())
.then((text) => console.log(`Completed JWT Cookie refresh: ${text}`))
.catch((err) => {
console.log("Failed to update jwt-cookie", err);
});
}, frequencyMs);
_gp.jwtTimer = setInterval(refetchCookie, frequencyMs);

setTimeout(refetchCookie, 20_000);
}
},
[user, client],
Expand Down
1 change: 1 addition & 0 deletions components/server/src/auth/login-completion-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class LoginCompletionHandler {
// (default case) If we got redirected here onto the base domain of the Gitpod installation, we can just issue the cookie right away.
const cookie = await this.session.createJWTSessionCookie(user.id);
response.cookie(cookie.name, cookie.value, cookie.opts);
this.session.setHashedUserIdCookie(request, response);
reportJWTCookieIssued();

log.info(logContext, `User is logged in successfully. Redirect to: ${returnTo}`);
Expand Down
2 changes: 1 addition & 1 deletion components/server/src/session-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class SessionHandler {
});
}

private setHashedUserIdCookie(req: express.Request, res: express.Response): void {
public setHashedUserIdCookie(req: express.Request, res: express.Response): void {
const user = req.user as User;
if (!user) return;

Expand Down

0 comments on commit abb191f

Please sign in to comment.