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

[server] Fetch User based on JWT Session sub #17710

Merged
merged 1 commit into from
May 24, 2023
Merged
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
[server] Fetch User based on JWT Session sub
  • Loading branch information
easyCZ committed May 23, 2023
commit 8520a977477e0446c0cfd3b1b4be2099bb50e9c1
31 changes: 25 additions & 6 deletions components/server/src/session-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ import { Config as DBConfig } from "@gitpod/gitpod-db/lib/config";
import { Config } from "./config";
import { reportSessionWithJWT } from "./prometheus-metrics";
import { AuthJWT } from "./auth/jwt";
import { UserDB } from "@gitpod/gitpod-db/lib";
import { JwtPayload } from "jsonwebtoken";
import { User } from "@gitpod/gitpod-protocol";

@injectable()
export class SessionHandlerProvider {
@inject(Config) protected readonly config: Config;
@inject(DBConfig) protected readonly dbConfig: DBConfig;
@inject(AuthJWT) protected readonly authJWT: AuthJWT;
@inject(UserDB) protected userDb: UserDB;

public sessionHandler: express.RequestHandler;

Expand Down Expand Up @@ -50,12 +54,8 @@ export class SessionHandlerProvider {
if (jwtToken) {
// we handle the verification async, because we don't yet need to use it in the application
/* tslint:disable-next-line */
this.authJWT
.verify(jwtToken)
.then((claims) => {
log.debug("JWT Session token verified", {
claims,
});
this.jwtSessionHandler(jwtToken)
.then((res) => {
hasJWTCookie = true;
})
.catch((err) => {
Expand All @@ -70,6 +70,25 @@ export class SessionHandlerProvider {
};
}

protected async jwtSessionHandler(jwtToken: string): Promise<[JwtPayload, User]> {
const claims = await this.authJWT.verify(jwtToken);
log.debug("JWT Session token verified", {
claims,
});

const subject = claims.sub;
if (!subject) {
throw new Error("Subject is missing from JWT session claims");
}

const user = await this.userDb.findUserById(subject);
if (!user) {
throw new Error("No user exists.");
}

return [claims, user];
}

protected getCookieOptions(config: Config): express.CookieOptions {
// ############################################################################################################
// WARNING: Whenever we do changes here, we very likely want to have bump the cookie name as well!
Expand Down