Skip to content

Commit 17fae49

Browse files
committed
split internal upsert method updateAuthProvider into create and update
1 parent 9559892 commit 17fae49

File tree

2 files changed

+57
-51
lines changed

2 files changed

+57
-51
lines changed

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

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -112,45 +112,61 @@ export class AuthProviderService {
112112
return result;
113113
}
114114

115-
async updateAuthProvider(
116-
userId: string,
117-
entry: AuthProviderEntry.UpdateEntry | AuthProviderEntry.NewEntry,
118-
): Promise<AuthProviderEntry> {
115+
async createAuthProviderOfUser(userId: string, entry: AuthProviderEntry.NewEntry): Promise<AuthProviderEntry> {
119116
await this.auth.checkPermissionOnUser(userId, "write_info", userId);
120117

121-
let authProvider: AuthProviderEntry;
122-
if ("id" in entry) {
123-
const { id, ownerId } = entry;
124-
const existing = (await this.authProviderDB.findByUserId(ownerId)).find((p) => p.id === id);
125-
if (!existing) {
126-
throw new Error("Provider does not exist.");
127-
}
128-
const changed =
129-
entry.clientId !== existing.oauth.clientId ||
130-
(entry.clientSecret && entry.clientSecret !== existing.oauth.clientSecret);
118+
const host = entry.host && entry.host.toLowerCase();
131119

132-
if (!changed) {
133-
return existing;
134-
}
120+
// reachability test
121+
if (!(await this.isHostReachable(host))) {
122+
log.info(`Host could not be reached.`, { entry });
123+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, `Host could not be reached.`);
124+
}
135125

136-
// update config on demand
137-
const oauth = {
138-
...existing.oauth,
139-
clientId: entry.clientId,
140-
clientSecret: entry.clientSecret || existing.oauth.clientSecret, // FE may send empty ("") if not changed
141-
};
142-
authProvider = {
143-
...existing,
144-
oauth,
145-
status: "pending",
146-
};
147-
} else {
148-
const existing = await this.authProviderDB.findByHost(entry.host);
149-
if (existing) {
150-
throw new Error("Provider for this host already exists.");
151-
}
152-
authProvider = this.initializeNewProvider(entry);
126+
// checking for already existing runtime providers
127+
const hostContext = this.hostContexts.get(host);
128+
if (hostContext) {
129+
const builtInExists = hostContext.authProvider.params.ownerId === undefined;
130+
log.info(`Attempt to override an existing provider.`, { entry, builtInExists });
131+
throw new ApplicationError(ErrorCodes.CONFLICT, `Attempt to override an existing provider.`);
132+
}
133+
134+
const existing = await this.authProviderDB.findByHost(entry.host);
135+
if (existing) {
136+
log.info(`Attempt to override an existing provider.`, { entry });
137+
throw new ApplicationError(ErrorCodes.CONFLICT, `Provider for this host already exists.`);
153138
}
139+
const authProvider = this.initializeNewProvider(entry);
140+
return await this.authProviderDB.storeAuthProvider(authProvider as AuthProviderEntry, true);
141+
}
142+
143+
async updateAuthProviderOfUser(userId: string, entry: AuthProviderEntry.UpdateEntry): Promise<AuthProviderEntry> {
144+
await this.auth.checkPermissionOnUser(userId, "write_info", userId);
145+
146+
const { id, ownerId } = entry;
147+
const existing = (await this.authProviderDB.findByUserId(ownerId)).find((p) => p.id === id);
148+
if (!existing) {
149+
throw new Error("Provider does not exist.");
150+
}
151+
const changed =
152+
entry.clientId !== existing.oauth.clientId ||
153+
(entry.clientSecret && entry.clientSecret !== existing.oauth.clientSecret);
154+
155+
if (!changed) {
156+
return existing;
157+
}
158+
159+
// update config on demand
160+
const oauth = {
161+
...existing.oauth,
162+
clientId: entry.clientId,
163+
clientSecret: entry.clientSecret || existing.oauth.clientSecret, // FE may send empty ("") if not changed
164+
};
165+
const authProvider = {
166+
...existing,
167+
oauth,
168+
status: "pending",
169+
};
154170
return await this.authProviderDB.storeAuthProvider(authProvider as AuthProviderEntry, true);
155171
}
156172

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2939,25 +2939,15 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
29392939

29402940
const safeProvider = this.redactUpdateOwnAuthProviderParams({ entry });
29412941
try {
2942+
if ("id" in safeProvider) {
2943+
const result = await this.authProviderService.updateAuthProviderOfUser(user.id, safeProvider);
2944+
return AuthProviderEntry.redact(result);
2945+
}
29422946
if ("host" in safeProvider) {
2943-
// on creating we're are checking for already existing runtime providers
2944-
2945-
const host = safeProvider.host && safeProvider.host.toLowerCase();
2946-
2947-
if (!(await this.authProviderService.isHostReachable(host))) {
2948-
log.debug(`Host could not be reached.`, { entry, safeProvider });
2949-
throw new Error("Host could not be reached.");
2950-
}
2951-
2952-
const hostContext = this.hostContextProvider.get(host);
2953-
if (hostContext) {
2954-
const builtInExists = hostContext.authProvider.params.ownerId === undefined;
2955-
log.debug(`Attempt to override existing auth provider.`, { entry, safeProvider, builtInExists });
2956-
throw new Error("Provider for this host already exists.");
2957-
}
2947+
const result = await this.authProviderService.createAuthProviderOfUser(user.id, safeProvider);
2948+
return AuthProviderEntry.redact(result);
29582949
}
2959-
const result = await this.authProviderService.updateAuthProvider(safeProvider);
2960-
return AuthProviderEntry.redact(result);
2950+
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Unexpected parameters.");
29612951
} catch (error) {
29622952
if (ApplicationError.hasErrorCode(error)) {
29632953
throw error;

0 commit comments

Comments
 (0)