feat(cloudflare-auth-access-provider): Change defaults and add provider docs

Signed-off-by: Jason Diaz G. <jason.rene.diaz.gonzalez@ericsson.com>
This commit is contained in:
Jason Diaz G.
2024-07-18 16:45:13 -06:00
parent 9614e649ed
commit 034c0b3cb2
2 changed files with 20 additions and 19 deletions
+6
View File
@@ -32,6 +32,12 @@ auth:
serviceTokens:
- token: '1uh2fh19efvfh129f1f919u21f2f19jf2.access'
subject: 'bot-user@your-company.com'
# You can customize the header name that contains the jwt token, by default
# cf-access-jwt-assertion is used
jwtHeaderName: <my-header>
# You can customize the authorization cookie name, by default
# CF_Authorization is used
authorizationCookieName: <MY_CAUTHORIZATION_COOKIE_NAME>
# This picks what sign in resolver(s) you want to use.
signIn:
resolvers:
@@ -40,10 +40,10 @@ export class AuthHelper {
options?: { cache?: CacheService },
): AuthHelper {
const teamName = config.getString('teamName');
const jwtHeaderName = config.getOptionalString('jwtHeaderName');
const authorizationCookieName = config.getOptionalString(
'authorizationCookieName',
);
const jwtHeaderName =
config.getOptionalString('jwtHeaderName') ?? CF_JWT_HEADER;
const authorizationCookieName =
config.getOptionalString('authorizationCookieName') ?? COOKIE_AUTH_NAME;
const serviceTokens = (
config.getOptionalConfigArray('serviceTokens') ?? []
)?.map(cfg => {
@@ -60,37 +60,35 @@ export class AuthHelper {
return new AuthHelper(
teamName,
serviceTokens,
keySet,
options?.cache,
jwtHeaderName,
authorizationCookieName,
keySet,
options?.cache,
);
}
private constructor(
private readonly teamName: string,
private readonly serviceTokens: ServiceToken[],
private readonly jwtHeaderName: string,
private readonly authorizationCookieName: string,
private readonly keySet: ReturnType<typeof createRemoteJWKSet>,
private readonly cache?: CacheService,
private readonly jwtHeaderName?: string,
private readonly authorizationCookieName?: string,
) {}
async authenticate(req: express.Request): Promise<CloudflareAccessResult> {
// JWTs generated by Access are available in a request header as
// Cf-Access-Jwt-Assertion and as cookies as CF_Authorization.
let jwt = req.header(this.jwtHeaderName || CF_JWT_HEADER);
let jwt = req.header(this.jwtHeaderName);
if (!jwt) {
jwt = req.cookies[this.authorizationCookieName || COOKIE_AUTH_NAME];
jwt = req.cookies[this.authorizationCookieName];
}
if (!jwt) {
// Only throw if both are not provided by Cloudflare Access since either
// can be used.
throw new AuthenticationError(
`Missing ${this.jwtHeaderName || CF_JWT_HEADER} and
${
this.authorizationCookieName || COOKIE_AUTH_NAME
} from Cloudflare Access`,
`Missing ${this.jwtHeaderName} and
${this.authorizationCookieName} from Cloudflare Access`,
);
}
@@ -171,11 +169,8 @@ export class AuthHelper {
): Promise<CloudflareAccessIdentityProfile> {
const headers = new Headers();
// set both headers just the way inbound responses are set
headers.set(this.jwtHeaderName || CF_JWT_HEADER, jwt);
headers.set(
'cookie',
`${this.authorizationCookieName || COOKIE_AUTH_NAME}=${jwt}`,
);
headers.set(this.jwtHeaderName, jwt);
headers.set('cookie', `${this.authorizationCookieName}=${jwt}`);
try {
const res = await fetch(
`https://${this.teamName}.cloudflareaccess.com/cdn-cgi/access/get-identity`,