Skip to content
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
20 changes: 13 additions & 7 deletions src/auth/guards/repo-scope.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import { Repository } from 'typeorm';
import { Repository as RepositoryEntity } from '../../common/entities';
import { User } from '../../common/entities';
import { UserRole } from '../../common/enums';
import { ROLES_KEY } from '../decorators/roles.decorator';
import { REPO_SCOPED_SYNC_KEY } from '../decorators/repo-scoped-sync.decorator';
import { AppConfig } from '../../config/configuration';

interface AuthenticatedRequest extends Request {
interface AuthenticatedRequest extends Request<{
owner?: string;
repo?: string;
}> {
user?: { userId: string };
params?: { owner?: string; repo?: string };
}

/**
Expand Down Expand Up @@ -75,7 +76,8 @@ export class RepoScopeGuard implements CanActivate {
where: { id: request.user.userId },
select: { id: true, roles: true },
});
if (!user) throw new ForbiddenException('Authenticated user no longer exists');
if (!user)
throw new ForbiddenException('Authenticated user no longer exists');
if (!user.roles.includes(UserRole.MAINTAINER)) {
throw new ForbiddenException('Maintainer role required');
}
Expand Down Expand Up @@ -104,14 +106,18 @@ export class RepoScopeGuard implements CanActivate {

// Tracked with no maintainer yet: allow, and claim it for this user so the
// scope is explicit from here on.
await this.repositoryRepo.update({ id: tracked.id }, { maintainerId: user.id });
await this.repositoryRepo.update(
{ id: tracked.id },
{ maintainerId: user.id },
);
return true;
}

/** Exact `owner/repo` entries from `GITHUB_SYNC_ALLOWED_REPOS`. */
private isAllowlisted(fullName: string): boolean {
const raw = this.configService.get('github', { infer: true })
.syncAllowedRepos;
const raw = this.configService.get('github', {
infer: true,
}).syncAllowedRepos;
if (!raw) return false;
return raw
.split(',')
Expand Down