Skip to content

Commit 3077ef5

Browse files
selfcontainedroboquat
authored andcommitted
creating distinct fns for org provider calls
1 parent 6a5440e commit 3077ef5

File tree

4 files changed

+62
-10
lines changed

4 files changed

+62
-10
lines changed

components/gitpod-protocol/src/gitpod-service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,10 @@ export namespace GitpodServer {
467467
readonly id: string;
468468
}
469469
export interface CreateOrgAuthProviderParams {
470-
readonly entry: Omit<AuthProviderEntry.NewEntry, "ownerId">;
470+
readonly entry: AuthProviderEntry.NewOrgEntry;
471471
}
472472
export interface UpdateOrgAuthProviderParams {
473-
readonly entry: Omit<AuthProviderEntry.UpdateEntry, "ownerId">;
473+
readonly entry: AuthProviderEntry.UpdateOrgEntry;
474474
}
475475
export interface GetOrgAuthProviderParams {
476476
readonly organizationId: string;

components/gitpod-protocol/src/protocol.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1445,12 +1445,20 @@ export interface OAuth2Config {
14451445
export namespace AuthProviderEntry {
14461446
export type Type = "GitHub" | "GitLab" | string;
14471447
export type Status = "pending" | "verified";
1448-
export type NewEntry = Pick<AuthProviderEntry, "ownerId" | "host" | "type" | "organizationId"> & {
1448+
export type NewEntry = Pick<AuthProviderEntry, "ownerId" | "host" | "type"> & {
14491449
clientId?: string;
14501450
clientSecret?: string;
14511451
};
1452-
export type UpdateEntry = Pick<AuthProviderEntry, "id" | "ownerId" | "organizationId"> &
1452+
export type UpdateEntry = Pick<AuthProviderEntry, "id" | "ownerId"> &
14531453
Pick<OAuth2Config, "clientId" | "clientSecret">;
1454+
export type NewOrgEntry = NewEntry & {
1455+
organizationId: string;
1456+
};
1457+
export type UpdateOrgEntry = Pick<AuthProviderEntry, "id"> & {
1458+
clientId: string;
1459+
clientSecret: string;
1460+
organizationId: string;
1461+
};
14541462
export function redact(entry: AuthProviderEntry): AuthProviderEntry {
14551463
return {
14561464
...entry,

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

+45
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,51 @@ export class AuthProviderService {
117117
}
118118
return await this.authProviderDB.storeAuthProvider(authProvider as AuthProviderEntry, true);
119119
}
120+
121+
async createOrgAuthProvider(entry: AuthProviderEntry.NewOrgEntry): Promise<AuthProviderEntry> {
122+
// TODO: only restrict existing provider w/ same host if it's the same organization
123+
const existing = await this.authProviderDB.findByHost(entry.host);
124+
if (existing) {
125+
throw new Error("Provider for this host already exists.");
126+
}
127+
128+
const authProvider = this.initializeNewProvider(entry);
129+
130+
return await this.authProviderDB.storeAuthProvider(authProvider as AuthProviderEntry, true);
131+
}
132+
133+
async updateOrgAuthProvider(entry: AuthProviderEntry.UpdateOrgEntry): Promise<AuthProviderEntry> {
134+
let authProvider: AuthProviderEntry;
135+
136+
const { id, organizationId } = entry;
137+
// TODO can we change this to query for the provider by id and org instead of loading all from org?
138+
const existing = (await this.authProviderDB.findByOrgId(organizationId)).find((p) => p.id === id);
139+
if (!existing) {
140+
throw new Error("Provider does not exist.");
141+
}
142+
const changed =
143+
entry.clientId !== existing.oauth.clientId ||
144+
(entry.clientSecret && entry.clientSecret !== existing.oauth.clientSecret);
145+
146+
if (!changed) {
147+
return existing;
148+
}
149+
150+
// update config on demand
151+
const oauth = {
152+
...existing.oauth,
153+
clientId: entry.clientId,
154+
clientSecret: entry.clientSecret || existing.oauth.clientSecret, // FE may send empty ("") if not changed
155+
};
156+
authProvider = {
157+
...existing,
158+
oauth,
159+
status: "pending",
160+
};
161+
162+
return await this.authProviderDB.storeAuthProvider(authProvider as AuthProviderEntry, true);
163+
}
164+
120165
protected initializeNewProvider(newEntry: AuthProviderEntry.NewEntry): AuthProviderEntry {
121166
const { host, type, clientId, clientSecret } = newEntry;
122167
let urls;

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -2925,7 +2925,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
29252925
let user = this.checkAndBlockUser("createOrgAuthProvider");
29262926

29272927
// map params to a new provider
2928-
const newProvider = <AuthProviderEntry.NewEntry>{
2928+
const newProvider = <AuthProviderEntry.NewOrgEntry>{
29292929
host: entry.host,
29302930
type: entry.type,
29312931
clientId: entry.clientId,
@@ -2966,7 +2966,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
29662966
throw new Error("Provider for this host already exists.");
29672967
}
29682968

2969-
const result = await this.authProviderService.updateAuthProvider(newProvider);
2969+
const result = await this.authProviderService.createOrgAuthProvider(newProvider);
29702970
return AuthProviderEntry.redact(result);
29712971
} catch (error) {
29722972
const message = error && error.message ? error.message : "Failed to create the provider.";
@@ -2980,12 +2980,11 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
29802980
): Promise<AuthProviderEntry> {
29812981
traceAPIParams(ctx, {}); // entry contains PII
29822982

2983-
const user = this.checkAndBlockUser("updateOrgAuthProvider");
2983+
this.checkAndBlockUser("updateOrgAuthProvider");
29842984

29852985
// map params to a provider update
2986-
const providerUpdate: AuthProviderEntry.UpdateEntry = {
2986+
const providerUpdate: AuthProviderEntry.UpdateOrgEntry = {
29872987
id: entry.id,
2988-
ownerId: user.id,
29892988
clientId: entry.clientId,
29902989
clientSecret: entry.clientSecret,
29912990
organizationId: entry.organizationId,
@@ -3000,7 +2999,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
30002999
await this.guardTeamOperation(providerUpdate.organizationId, "update");
30013000

30023001
try {
3003-
const result = await this.authProviderService.updateAuthProvider(providerUpdate);
3002+
const result = await this.authProviderService.updateOrgAuthProvider(providerUpdate);
30043003
return AuthProviderEntry.redact(result);
30053004
} catch (error) {
30063005
const message = error && error.message ? error.message : "Failed to update the provider.";

0 commit comments

Comments
 (0)