-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from mozilla-services/wstuckey/add-gcp-iap-bac…
…kend-plugin Attempt to fix the GCP IAP auth
- Loading branch information
Showing
6 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { createBackendModule } from '@backstage/backend-plugin-api'; | ||
import { gcpIapAuthenticator } from '@backstage/plugin-auth-backend-module-gcp-iap-provider'; | ||
import { | ||
authProvidersExtensionPoint, | ||
createProxyAuthProviderFactory, | ||
} from '@backstage/plugin-auth-node'; | ||
import { | ||
stringifyEntityRef, | ||
DEFAULT_NAMESPACE, | ||
} from '@backstage/catalog-model'; | ||
|
||
export const gcpIapCustomAuthProvider = createBackendModule({ | ||
pluginId: 'auth', | ||
moduleId: 'custom-gcp-iap-auth-provider', | ||
register(reg) { | ||
reg.registerInit({ | ||
deps: { providers: authProvidersExtensionPoint }, | ||
async init({ providers }) { | ||
providers.registerProvider({ | ||
providerId: 'gcpIap', | ||
factory: createProxyAuthProviderFactory({ | ||
authenticator: gcpIapAuthenticator, | ||
async signInResolver(info, ctx) { | ||
const { | ||
profile: { email }, | ||
} = info; | ||
|
||
if (!email) { | ||
throw new Error('User profile contained no email'); | ||
} | ||
|
||
const [name, domain] = email.split('@'); | ||
if (domain !== 'mozilla.com') { | ||
throw new Error( | ||
`Login failed, this email ${email} does not belong to the expected domain`, | ||
); | ||
} | ||
|
||
// try to resolve an existing gh username to the name part of the email | ||
// otherwise, issue a log in token. | ||
try { | ||
return await ctx.signInWithCatalogUser({ entityRef: { name } }); | ||
} catch (_) { | ||
const userEntity = stringifyEntityRef({ | ||
kind: 'User', | ||
name, | ||
namespace: DEFAULT_NAMESPACE, | ||
}); | ||
|
||
return ctx.issueToken({ | ||
claims: { | ||
sub: userEntity, | ||
ent: [userEntity], | ||
}, | ||
}); | ||
} | ||
}, | ||
}), | ||
}); | ||
}, | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters