auth-backend: add a default auth handler for the oauth2-proxy provider

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-05-02 17:09:55 +02:00
parent ba901234cf
commit 0caff59719
2 changed files with 20 additions and 14 deletions
-12
View File
@@ -98,18 +98,6 @@ export default async function createPlugin(
// as how to sign a user in without a matching user entity in the catalog.
// You can try it out using `<ProxiedSignInPage {...props} provider="myproxy" />`
myproxy: providers.oauth2Proxy.create({
async authHandler(result) {
const user = result.getHeader('x-forwarded-user');
if (!user) {
throw new Error('Profile must have a username');
}
return {
profile: {
email: result.getHeader('x-forwarded-email'),
displayName: user,
},
};
},
signIn: {
async resolver({ result }, ctx) {
const entityRef = stringifyEntityRef({
@@ -23,6 +23,7 @@ import {
AuthProviderRouteHandlers,
AuthResponse,
AuthResolverContext,
AuthHandlerResult,
} from '../types';
import { decodeJwt } from 'jose';
import { prepareBackstageIdentityResponse } from '../prepareBackstageIdentityResponse';
@@ -175,6 +176,19 @@ export class Oauth2ProxyAuthProvider<JWTPayload>
}
}
async function defaultAuthHandler(
result: OAuth2ProxyResult<unknown>,
): Promise<AuthHandlerResult> {
return {
profile: {
email: result.getHeader('x-forwarded-email'),
displayName:
result.getHeader('x-forwarded-preferred-username') ||
result.getHeader('x-forwarded-user'),
},
};
}
/**
* Auth provider integration for oauth2-proxy auth
*
@@ -184,8 +198,12 @@ export const oauth2Proxy = createAuthProviderIntegration({
create<JWTPayload>(options: {
/**
* Configure an auth handler to generate a profile for the user.
*
* The default implementation uses the value of the `X-Forwarded-Preferred-Username`
* header as the display name, falling back to `X-Forwarded-User`, and the value of
* the `X-Forwarded-Email` header as the email address.
*/
authHandler: AuthHandler<OAuth2ProxyResult<JWTPayload>>;
authHandler?: AuthHandler<OAuth2ProxyResult<JWTPayload>>;
/**
* Configure sign-in for this provider, without it the provider can not be used to sign users in.
@@ -203,7 +221,7 @@ export const oauth2Proxy = createAuthProviderIntegration({
return new Oauth2ProxyAuthProvider<JWTPayload>({
resolverContext,
signInResolver,
authHandler,
authHandler: authHandler ?? defaultAuthHandler,
});
};
},