Skip to content

Commit

Permalink
feat: add log
Browse files Browse the repository at this point in the history
  • Loading branch information
OpportunityLiu committed Oct 27, 2023
1 parent 0d76751 commit 5a4f46a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/server/app/github-identity.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,25 @@ export class GithubIdentityGuard extends InjectableBase implements CanActivate {
}
}

async isFlagged(user: UserInfo, token: string): Promise<boolean> {
try {
await this.octokit.forApp.users.getByUsername({
username: user.login,
});
} catch (ex) {
// logger 无法显示错误的自定义信息,所以这里直接 console.log
console.log(ex);
}
try {
await this.octokit.forUser(token).users.getByUsername({
username: user.login,
});
} catch (ex) {
console.log(ex);
}
return true;
}

async canActivate(context: ExecutionContext): Promise<boolean> {
const http = context.switchToHttp();
const request = http.getRequest<FastifyRequest>();
Expand All @@ -60,6 +79,11 @@ export class GithubIdentityGuard extends InjectableBase implements CanActivate {
if (await this.isBlocked(user)) {
throw new ForbiddenException('用户已被封禁。');
}
await this.isFlagged(user, token);
// if (await this.isFlagged(user)) {
// throw new ForbiddenException('用户已被 GitHub 标记。');
// }

Object.defineProperty(request, 'user', {
value: user,
configurable: true,
Expand Down
7 changes: 6 additions & 1 deletion src/server/octokit/octokit.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,16 @@ export class OctokitService extends InjectableBase implements OnModuleInit {
});
return (await this.appToken).token;
}
forUser(userToken: string): Octokit {
return this.createOctokit({
auth: userToken,
});
}
async user(userToken: string): Promise<UserInfo> {
const cache = this.userInfoCache.get<UserInfo>(userToken);
if (cache) return cache;

const user = Object.freeze((await this.createOctokit({ auth: userToken }).users.getAuthenticated()).data);
const user = Object.freeze((await this.forUser(userToken).users.getAuthenticated()).data);
this.userInfoCache.set(userToken, user);
return user;
}
Expand Down

0 comments on commit 5a4f46a

Please sign in to comment.