Skip to content

Commit 9be91ec

Browse files
committed
extract helper functions for scopes
1 parent 65c63ad commit 9be91ec

File tree

2 files changed

+55
-52
lines changed

2 files changed

+55
-52
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Copyright (c) 2023 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { AuthProviderEntry } from "@gitpod/gitpod-protocol";
8+
import { GitHubScope } from "../github/scopes";
9+
import { GitLabScope } from "../gitlab/scopes";
10+
import { BitbucketOAuthScopes } from "../bitbucket/bitbucket-oauth-scopes";
11+
import { BitbucketServerOAuthScopes } from "../bitbucket-server/bitbucket-server-oauth-scopes";
12+
13+
export function getRequiredScopes(entry: AuthProviderEntry) {
14+
switch (entry.type) {
15+
case "GitHub":
16+
return {
17+
default: GitHubScope.Requirements.DEFAULT,
18+
publicRepo: GitHubScope.Requirements.PUBLIC_REPO,
19+
privateRepo: GitHubScope.Requirements.PRIVATE_REPO,
20+
};
21+
case "GitLab":
22+
return {
23+
default: GitLabScope.Requirements.DEFAULT,
24+
publicRepo: GitLabScope.Requirements.DEFAULT,
25+
privateRepo: GitLabScope.Requirements.REPO,
26+
};
27+
case "Bitbucket":
28+
return {
29+
default: BitbucketOAuthScopes.Requirements.DEFAULT,
30+
publicRepo: BitbucketOAuthScopes.Requirements.DEFAULT,
31+
privateRepo: BitbucketOAuthScopes.Requirements.DEFAULT,
32+
};
33+
case "BitbucketServer":
34+
return {
35+
default: BitbucketServerOAuthScopes.Requirements.DEFAULT,
36+
publicRepo: BitbucketServerOAuthScopes.Requirements.DEFAULT,
37+
privateRepo: BitbucketServerOAuthScopes.Requirements.DEFAULT,
38+
};
39+
}
40+
}
41+
export function getScopesOfProvider(entry: AuthProviderEntry) {
42+
switch (entry.type) {
43+
case "GitHub":
44+
return GitHubScope.All;
45+
case "GitLab":
46+
return GitLabScope.All;
47+
case "Bitbucket":
48+
return BitbucketOAuthScopes.ALL;
49+
case "BitbucketServer":
50+
return BitbucketServerOAuthScopes.ALL;
51+
}
52+
}

components/server/src/auth/auth-provider-service.ts

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
1818
import fetch from "node-fetch";
1919
import { Authorizer } from "../authorization/authorizer";
2020
import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
21-
import { GitHubScope } from "../github/scopes";
22-
import { GitLabScope } from "../gitlab/scopes";
23-
import { BitbucketOAuthScopes } from "../bitbucket/bitbucket-oauth-scopes";
24-
import { BitbucketServerOAuthScopes } from "../bitbucket-server/bitbucket-server-oauth-scopes";
21+
import { getRequiredScopes, getScopesOfProvider } from "./auth-provider-scopes";
2522

2623
@injectable()
2724
export class AuthProviderService {
@@ -107,8 +104,8 @@ export class AuthProviderService {
107104
hiddenOnDashboard: ap.hiddenOnDashboard,
108105
disallowLogin: ap.disallowLogin,
109106
description: ap.description,
110-
scopes: this.toScopes(ap),
111-
requirements: this.toScopeRequirements(ap),
107+
scopes: getScopesOfProvider(ap),
108+
requirements: getRequiredScopes(ap),
112109
};
113110

114111
const result: AuthProviderInfo[] = [];
@@ -131,52 +128,6 @@ export class AuthProviderService {
131128
}
132129
return result;
133130
}
134-
/**
135-
* This is extracted from auth provider handlers in order to avoid a
136-
* dependency to host contexts.
137-
*
138-
* TODO(at) these defaults of provider types needs to be centralized.
139-
*/
140-
private toScopeRequirements(entry: AuthProviderEntry) {
141-
switch (entry.type) {
142-
case "GitHub":
143-
return {
144-
default: GitHubScope.Requirements.DEFAULT,
145-
publicRepo: GitHubScope.Requirements.PUBLIC_REPO,
146-
privateRepo: GitHubScope.Requirements.PRIVATE_REPO,
147-
};
148-
case "GitLab":
149-
return {
150-
default: GitLabScope.Requirements.DEFAULT,
151-
publicRepo: GitLabScope.Requirements.DEFAULT,
152-
privateRepo: GitLabScope.Requirements.REPO,
153-
};
154-
case "Bitbucket":
155-
return {
156-
default: BitbucketOAuthScopes.Requirements.DEFAULT,
157-
publicRepo: BitbucketOAuthScopes.Requirements.DEFAULT,
158-
privateRepo: BitbucketOAuthScopes.Requirements.DEFAULT,
159-
};
160-
case "BitbucketServer":
161-
return {
162-
default: BitbucketServerOAuthScopes.Requirements.DEFAULT,
163-
publicRepo: BitbucketServerOAuthScopes.Requirements.DEFAULT,
164-
privateRepo: BitbucketServerOAuthScopes.Requirements.DEFAULT,
165-
};
166-
}
167-
}
168-
private toScopes(entry: AuthProviderEntry) {
169-
switch (entry.type) {
170-
case "GitHub":
171-
return GitHubScope.All;
172-
case "GitLab":
173-
return GitLabScope.All;
174-
case "Bitbucket":
175-
return BitbucketOAuthScopes.ALL;
176-
case "BitbucketServer":
177-
return BitbucketServerOAuthScopes.ALL;
178-
}
179-
}
180131

181132
async getAuthProvidersOfUser(user: User | string): Promise<AuthProviderEntry[]> {
182133
const userId = User.is(user) ? user.id : user;

0 commit comments

Comments
 (0)