Skip to content

Commit da74d6f

Browse files
committed
💄
1 parent 424877a commit da74d6f

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

components/server/src/user/gitpod-token-service.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import { GitpodToken, GitpodTokenType } from "@gitpod/gitpod-protocol";
1010
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
1111
import { inject, injectable } from "inversify";
1212
import { Authorizer } from "../authorization/authorizer";
13-
import { GuardedResource, ResourceAccessGuard, ResourceAccessOp } from "../auth/resource-access";
14-
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
1513

1614
@injectable()
1715
export class GitpodTokenService {
@@ -23,21 +21,21 @@ export class GitpodTokenService {
2321
async getGitpodTokens(
2422
requestorId: string,
2523
userId: string,
26-
resouceGuardAccess: ResourceAccessGuard,
24+
oldPermissionCheck?: (token: GitpodToken) => Promise<void>, // @deprecated
2725
): Promise<GitpodToken[]> {
2826
await this.auth.checkPermissionOnUser(requestorId, "read_tokens", userId);
2927
const res = (await this.userDB.findAllGitpodTokensOfUser(userId)).filter((v) => !v.deleted);
30-
await Promise.all(
31-
res.map((tkn) => this.guardAccess(resouceGuardAccess, { kind: "gitpodToken", subject: tkn }, "get")),
32-
);
28+
if (oldPermissionCheck) {
29+
await Promise.all(res.map((tkn) => oldPermissionCheck(tkn)));
30+
}
3331
return res;
3432
}
3533

3634
async generateNewGitpodToken(
3735
requestorId: string,
3836
userId: string,
3937
options: { name?: string; type: GitpodTokenType; scopes?: string[] },
40-
resouceGuardAccess: ResourceAccessGuard,
38+
oldPermissionCheck?: (dbToken: DBGitpodToken) => Promise<void>, // @deprecated
4139
): Promise<string> {
4240
await this.auth.checkPermissionOnUser(requestorId, "write_tokens", userId);
4341
const token = crypto.randomBytes(30).toString("hex");
@@ -50,7 +48,9 @@ export class GitpodTokenService {
5048
scopes: options.scopes || [],
5149
created: new Date().toISOString(),
5250
};
53-
await this.guardAccess(resouceGuardAccess, { kind: "gitpodToken", subject: dbToken }, "create");
51+
if (oldPermissionCheck) {
52+
await oldPermissionCheck(dbToken);
53+
}
5454
await this.userDB.storeGitpodToken(dbToken);
5555
return token;
5656
}
@@ -59,7 +59,7 @@ export class GitpodTokenService {
5959
requestorId: string,
6060
userId: string,
6161
tokenHash: string,
62-
resouceGuardAccess: ResourceAccessGuard,
62+
oldPermissionCheck?: (token: GitpodToken) => Promise<void>, // @deprecated
6363
): Promise<string[]> {
6464
await this.auth.checkPermissionOnUser(requestorId, "read_tokens", userId);
6565
let token: GitpodToken | undefined;
@@ -72,39 +72,27 @@ export class GitpodTokenService {
7272
if (!token || token.deleted) {
7373
return [];
7474
}
75-
await this.guardAccess(resouceGuardAccess, { kind: "gitpodToken", subject: token }, "get");
75+
if (oldPermissionCheck) {
76+
await oldPermissionCheck(token);
77+
}
7678
return token.scopes;
7779
}
7880

7981
async deleteGitpodToken(
8082
requestorId: string,
8183
userId: string,
8284
tokenHash: string,
83-
resouceGuardAccess: ResourceAccessGuard,
85+
oldPermissionCheck?: (token: GitpodToken) => Promise<void>, // @deprecated
8486
): Promise<void> {
8587
await this.auth.checkPermissionOnUser(requestorId, "write_tokens", userId);
86-
const existingTokens = await this.getGitpodTokens(requestorId, userId, resouceGuardAccess);
88+
const existingTokens = await this.getGitpodTokens(requestorId, userId, oldPermissionCheck);
8789
const tkn = existingTokens.find((token) => token.tokenHash === tokenHash);
8890
if (!tkn) {
8991
throw new Error(`User ${requestorId} tries to delete a token ${tokenHash} that does not exist.`);
9092
}
91-
await this.guardAccess(resouceGuardAccess, { kind: "gitpodToken", subject: tkn }, "delete");
92-
await this.userDB.deleteGitpodToken(tokenHash);
93-
}
94-
95-
/**
96-
* @deprecated Will be removed in the near future
97-
*/
98-
private async guardAccess(
99-
resouceGuardAccess: ResourceAccessGuard,
100-
resource: GuardedResource,
101-
op: ResourceAccessOp,
102-
) {
103-
if (!(await resouceGuardAccess.canAccess(resource, op))) {
104-
throw new ApplicationError(
105-
ErrorCodes.PERMISSION_DENIED,
106-
`operation not permitted: missing ${op} permission on ${resource.kind}`,
107-
);
93+
if (oldPermissionCheck) {
94+
await oldPermissionCheck(tkn);
10895
}
96+
await this.userDB.deleteGitpodToken(tokenHash);
10997
}
11098
}

components/server/src/workspace/gitpod-server-impl.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
EmailDomainFilterDB,
1414
TeamDB,
1515
RedisPublisher,
16+
DBGitpodToken,
1617
} from "@gitpod/gitpod-db/lib";
1718
import { BlockedRepositoryDB } from "@gitpod/gitpod-db/lib/blocked-repository-db";
1819
import {
@@ -2892,7 +2893,9 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
28922893

28932894
public async getGitpodTokens(ctx: TraceContext): Promise<GitpodToken[]> {
28942895
const user = await this.checkAndBlockUser("getGitpodTokens");
2895-
return this.gitpodTokenService.getGitpodTokens(user.id, user.id, this.resourceAccessGuard);
2896+
return this.gitpodTokenService.getGitpodTokens(user.id, user.id, (token: GitpodToken) => {
2897+
return this.guardAccess({ kind: "gitpodToken", subject: token }, "get");
2898+
});
28962899
}
28972900

28982901
public async generateNewGitpodToken(
@@ -2902,21 +2905,27 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
29022905
traceAPIParams(ctx, { options });
29032906

29042907
const user = await this.checkAndBlockUser("generateNewGitpodToken");
2905-
return this.gitpodTokenService.generateNewGitpodToken(user.id, user.id, options, this.resourceAccessGuard);
2908+
return this.gitpodTokenService.generateNewGitpodToken(user.id, user.id, options, (dbToken: DBGitpodToken) => {
2909+
return this.guardAccess({ kind: "gitpodToken", subject: dbToken }, "create");
2910+
});
29062911
}
29072912

29082913
public async getGitpodTokenScopes(ctx: TraceContext, tokenHash: string): Promise<string[]> {
29092914
traceAPIParams(ctx, {}); // do not trace tokenHash
29102915

29112916
const user = await this.checkAndBlockUser("getGitpodTokenScopes");
2912-
return this.gitpodTokenService.getGitpodTokenScopes(user.id, user.id, tokenHash, this.resourceAccessGuard);
2917+
return this.gitpodTokenService.getGitpodTokenScopes(user.id, user.id, tokenHash, (token: GitpodToken) => {
2918+
return this.guardAccess({ kind: "gitpodToken", subject: token }, "get");
2919+
});
29132920
}
29142921

29152922
public async deleteGitpodToken(ctx: TraceContext, tokenHash: string): Promise<void> {
29162923
traceAPIParams(ctx, {}); // do not trace tokenHash
29172924

29182925
const user = await this.checkAndBlockUser("deleteGitpodToken");
2919-
return this.gitpodTokenService.deleteGitpodToken(user.id, user.id, tokenHash, this.resourceAccessGuard);
2926+
return this.gitpodTokenService.deleteGitpodToken(user.id, user.id, tokenHash, (token: GitpodToken) => {
2927+
return this.guardAccess({ kind: "gitpodToken", subject: token }, "delete");
2928+
});
29202929
}
29212930

29222931
async guessGitTokenScopes(ctx: TraceContext, params: GuessGitTokenScopesParams): Promise<GuessedGitTokenScopes> {

0 commit comments

Comments
 (0)