@@ -18,15 +18,13 @@ import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
18
18
import fetch from "node-fetch" ;
19
19
import { Authorizer } from "../authorization/authorizer" ;
20
20
import { ApplicationError , ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error" ;
21
- import { HostContextProvider } from "./host-context-provider" ;
22
21
23
22
@injectable ( )
24
23
export class AuthProviderService {
25
24
constructor (
26
25
@inject ( AuthProviderEntryDB ) private readonly authProviderDB : AuthProviderEntryDB ,
27
26
@inject ( TeamDB ) private readonly teamDB : TeamDB ,
28
27
@inject ( Config ) protected readonly config : Config ,
29
- @inject ( HostContextProvider ) private readonly hostContexts : HostContextProvider ,
30
28
@inject ( Authorizer ) private readonly auth : Authorizer ,
31
29
) { }
32
30
@@ -124,22 +122,25 @@ export class AuthProviderService {
124
122
}
125
123
126
124
// 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 } ) ;
125
+ const isBuiltInProvider = this . isBuiltInProvider ( host ) ;
126
+ if ( isBuiltInProvider ) {
127
+ log . info ( `Attempt to override an existing provider.` , { entry } ) ;
131
128
throw new ApplicationError ( ErrorCodes . CONFLICT , `Attempt to override an existing provider.` ) ;
132
129
}
133
-
134
130
const existing = await this . authProviderDB . findByHost ( entry . host ) ;
135
131
if ( existing ) {
136
- log . info ( `Attempt to override an existing provider .` , { entry } ) ;
132
+ log . info ( `Provider for this host already exists .` , { entry } ) ;
137
133
throw new ApplicationError ( ErrorCodes . CONFLICT , `Provider for this host already exists.` ) ;
138
134
}
135
+
139
136
const authProvider = this . initializeNewProvider ( entry ) ;
140
137
return await this . authProviderDB . storeAuthProvider ( authProvider as AuthProviderEntry , true ) ;
141
138
}
142
139
140
+ private isBuiltInProvider ( host : string ) {
141
+ return this . config . authProviderConfigs . some ( ( config ) => config . host . toLowerCase ( ) === host . toLocaleLowerCase ( ) ) ;
142
+ }
143
+
143
144
async updateAuthProviderOfUser ( userId : string , entry : AuthProviderEntry . UpdateEntry ) : Promise < AuthProviderEntry > {
144
145
await this . auth . checkPermissionOnUser ( userId , "write_info" , userId ) ;
145
146
@@ -181,17 +182,16 @@ export class AuthProviderService {
181
182
throw new ApplicationError ( ErrorCodes . BAD_REQUEST , `Host could not be reached.` ) ;
182
183
}
183
184
184
- const hostContext = this . hostContexts . get ( host ) ;
185
- if ( hostContext ) {
186
- const builtInExists = hostContext . authProvider . params . ownerId === undefined ;
187
- log . info ( `Attempt to override an existing provider.` , { newEntry, builtInExists } ) ;
185
+ const isBuiltInProvider = this . isBuiltInProvider ( host ) ;
186
+ if ( isBuiltInProvider ) {
187
+ log . info ( `Attempt to override an existing provider.` , { newEntry } ) ;
188
188
throw new ApplicationError ( ErrorCodes . CONFLICT , `Attempt to override an existing provider.` ) ;
189
189
}
190
190
191
191
const orgProviders = await this . authProviderDB . findByOrgId ( newEntry . organizationId ) ;
192
192
const existing = orgProviders . find ( ( p ) => p . host === host ) ;
193
193
if ( existing ) {
194
- log . info ( `Attempt to override an existing provider .` , { newEntry } ) ;
194
+ log . info ( `Provider for this host already exists .` , { newEntry } ) ;
195
195
throw new ApplicationError ( ErrorCodes . CONFLICT , `Provider for this host already exists.` ) ;
196
196
}
197
197
@@ -231,7 +231,7 @@ export class AuthProviderService {
231
231
return await this . authProviderDB . storeAuthProvider ( authProvider as AuthProviderEntry , true ) ;
232
232
}
233
233
234
- protected initializeNewProvider ( newEntry : AuthProviderEntry . NewEntry ) : AuthProviderEntry {
234
+ private initializeNewProvider ( newEntry : AuthProviderEntry . NewEntry ) : AuthProviderEntry {
235
235
const { host, type, clientId, clientSecret } = newEntry ;
236
236
let urls ;
237
237
switch ( type ) {
@@ -317,7 +317,7 @@ export class AuthProviderService {
317
317
}
318
318
}
319
319
320
- protected callbackUrl = ( ) => {
320
+ private callbackUrl = ( ) => {
321
321
const pathname = `/auth/callback` ;
322
322
return this . config . hostUrl . with ( { pathname } ) . toString ( ) ;
323
323
} ;
0 commit comments