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

fix: prevent anonymous user to generate personal access token #929

Merged
merged 9 commits into from
Oct 25, 2023
16 changes: 14 additions & 2 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,16 @@ export class UserService {
);
}
const accessToken = await this.jwtService.generateToken(this.currentUser);
const user = await this.userRepository.findById(
this.currentUser[securityId],
);
if (!user) {
throw new HttpErrors.UnprocessableEntity('Unauthorized');
}
const pat = new UserPersonalAccessToken({
...data,
token: accessToken,
userId: this.currentUser[securityId],
userId: user.id,
});

return this.userPersonalAccessTokenRepository.create(pat);
Expand All @@ -332,10 +338,16 @@ export class UserService {
};
await this.userPersonalAccessTokenRepository.deleteAll(filter);
const accessToken = await this.jwtService.generateToken(this.currentUser);
const user = await this.userRepository.findById(
this.currentUser[securityId],
);
if (!user) {
throw new HttpErrors.UnprocessableEntity('Unauthorized');
}
const pat = new UserPersonalAccessToken({
...data,
token: accessToken,
userId: this.currentUser[securityId],
userId: user.id,
});

return this.userPersonalAccessTokenRepository.create(pat);
Expand Down
Loading