Skip to content

feat: add aud claim for oauth2proxy #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/operators/keycloak/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { extractError } from '../../tasks/keycloak/errors'
import {
createAdminUser,
createClient,
createClientAudClaimMapper,
createClientEmailClaimMapper,
createClientScopes,
createClientSubClaimMapper,
Expand Down Expand Up @@ -476,6 +477,13 @@ async function keycloakRealmProviderConfigurer(api: KeycloakApi) {
await api.protocols.adminRealmsRealmClientsClientUuidProtocolMappersModelsPost(keycloakRealm, client.id!, subMapper)
}

// Needed for oauth2-proxy OIDC configuration
if (!allClaims.some((el) => el.name === 'aud')) {
const subMapper = createClientAudClaimMapper()
console.info('Creating client aud claim mapper')
await api.protocols.adminRealmsRealmClientsClientUuidProtocolMappersModelsPost(keycloakRealm, client.id!, subMapper)
}

// set login theme for master realm
console.info('adding theme for login page')
await api.realms.adminRealmsRealmPut(env.KEYCLOAK_REALM, createLoginThemeConfig('APL'))
Expand Down
18 changes: 18 additions & 0 deletions src/tasks/keycloak/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { ProtocolMapperRepresentation } from '@linode/keycloak-client-node'
import axios from 'axios'
import { cleanEnv, KEYCLOAK_CLIENT_ID } from '../../validators'

export const keycloakRealm = 'otomi'

const localEnv = cleanEnv({
KEYCLOAK_CLIENT_ID,
})

export const defaultsIdpMapperTpl = (
idpAlias: string,
idpUsernameClaimMapper: string,
Expand Down Expand Up @@ -232,6 +237,19 @@ export const clientSubClaimMapper = (): Record<string, unknown> => ({
},
})

export const clientAudClaimMapper = (): Record<string, unknown> => ({
name: 'aud-mapper-otomi',
protocol: 'openid-connect',
protocolMapper: 'oidc-audience-mapper',
config: {
'access.token.claim': 'true',
'id.token.claim': 'true',
'included.client.audience': localEnv.KEYCLOAK_CLIENT_ID,
'introspection.token.claim': 'true',
'lightweight.claim': 'true',
},
})

export const oidcCfg = (
providerCfg: OidcProviderCfg,
clientId: string,
Expand Down
6 changes: 6 additions & 0 deletions src/tasks/keycloak/realm-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as utils from '../../utils'
import {
TeamMapping,
adminUserCfgTpl,
clientAudClaimMapper,
clientEmailClaimMapper,
clientScopeCfgTpl,
clientSubClaimMapper,
Expand Down Expand Up @@ -120,6 +121,11 @@ export function createClientSubClaimMapper(): ProtocolMapperRepresentation {
return subClaimMapper
}

export function createClientAudClaimMapper(): ProtocolMapperRepresentation {
const subClaimMapper = defaultsDeep(new ProtocolMapperRepresentation(), clientAudClaimMapper())
return subClaimMapper
}

export function createAdminUser(username: string, password: string): UserRepresentation {
const userRepresentation = defaultsDeep(new UserRepresentation(), adminUserCfgTpl(username, password))
return userRepresentation
Expand Down
Loading