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

SWAP 3206 3 - Refactor jwt endpoint to provide valid jwt #441

Merged
merged 1 commit into from
Apr 13, 2023
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
1 change: 1 addition & 0 deletions src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const configuration = () => ({
jwt: {
secret: process.env.JWT_SECRET,
expiresIn: parseInt(process.env.JWT_EXPIRES_IN ?? "3600", 10),
neverExpires: process.env.JWT_NEVER_EXPIRES ?? "100y",
},
ldap: {
server: {
Expand Down
18 changes: 11 additions & 7 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,17 @@ export class UsersController {
@Param("id") id: string,
@Body() jwtProperties: CreateCustomJwt,
): Promise<CreateUserJWT | null> {
const viewedUser = (await this.usersService.findById2JWTUser(
id,
)) as JWTUser;
const viewedUser = (await this.usersService.findById(id)) as Omit<
User,
"password"
>;

return this.usersService.createCustomJWT(
viewedUser,
jwtProperties as JwtSignOptions,
);
if (viewedUser) {
return this.usersService.createCustomJWT(
JSON.parse(JSON.stringify(viewedUser)),
jwtProperties as JwtSignOptions,
);
}
return null;
}
}
14 changes: 7 additions & 7 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,27 +290,27 @@ export class UsersService implements OnModuleInit {
}

async createCustomJWT(
user: JWTUser,
user: Omit<User, "password">,
jwtProperties: JwtSignOptions,
): Promise<CreateUserJWT | null> {
const signAndVerifyOptions: JwtSignOptions = {
...jwtProperties,
} as JwtSignOptions;
if (signAndVerifyOptions.expiresIn == "never") {
delete signAndVerifyOptions.expiresIn;
signAndVerifyOptions.expiresIn =
this.configService.get<string>("jwt.neverExpires") || "100y";
} else if (
typeof signAndVerifyOptions.expiresIn === "string" &&
signAndVerifyOptions.expiresIn &&
!isNaN(+signAndVerifyOptions.expiresIn)
) {
signAndVerifyOptions.expiresIn = parseInt(signAndVerifyOptions.expiresIn);
} else if (!signAndVerifyOptions.expiresIn) {
signAndVerifyOptions.expiresIn =
this.configService.get<string>("jwt.expiresIn") || "1h";
}
signAndVerifyOptions.secret = this.configService.get<string>("jwt.secret");
const payload = {
username: user._id,
groups: user.currentGroups,
};
const jwtString = this.jwtService.sign(payload, signAndVerifyOptions);
const jwtString = this.jwtService.sign(user, signAndVerifyOptions);
return { jwt: jwtString };
}
}