Skip to content

Commit 7a68834

Browse files
ElderMattferruhcihan
authored andcommitted
feat: add the aud claim for oauth2proxy (#253)
* feat: add aud claim for oauth2proxy * fix: use keycloak client id instead of hardcoded (cherry picked from commit f74e44f)
1 parent 11045d9 commit 7a68834

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/operators/keycloak/keycloak.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { extractError } from '../../tasks/keycloak/errors'
2323
import {
2424
createAdminUser,
2525
createClient,
26+
createClientAudClaimMapper,
2627
createClientEmailClaimMapper,
2728
createClientScopes,
2829
createClientSubClaimMapper,
@@ -476,6 +477,13 @@ async function keycloakRealmProviderConfigurer(api: KeycloakApi) {
476477
await api.protocols.adminRealmsRealmClientsClientUuidProtocolMappersModelsPost(keycloakRealm, client.id!, subMapper)
477478
}
478479

480+
// Needed for oauth2-proxy OIDC configuration
481+
if (!allClaims.some((el) => el.name === 'aud')) {
482+
const subMapper = createClientAudClaimMapper()
483+
console.info('Creating client aud claim mapper')
484+
await api.protocols.adminRealmsRealmClientsClientUuidProtocolMappersModelsPost(keycloakRealm, client.id!, subMapper)
485+
}
486+
479487
// set login theme for master realm
480488
console.info('adding theme for login page')
481489
await api.realms.adminRealmsRealmPut(env.KEYCLOAK_REALM, createLoginThemeConfig('APL'))

src/tasks/keycloak/config.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { ProtocolMapperRepresentation } from '@linode/keycloak-client-node'
22
import axios from 'axios'
3+
import { cleanEnv, KEYCLOAK_CLIENT_ID } from '../../validators'
34

45
export const keycloakRealm = 'otomi'
56

7+
const localEnv = cleanEnv({
8+
KEYCLOAK_CLIENT_ID,
9+
})
10+
611
export const defaultsIdpMapperTpl = (
712
idpAlias: string,
813
idpUsernameClaimMapper: string,
@@ -232,6 +237,19 @@ export const clientSubClaimMapper = (): Record<string, unknown> => ({
232237
},
233238
})
234239

240+
export const clientAudClaimMapper = (): Record<string, unknown> => ({
241+
name: 'aud-mapper-otomi',
242+
protocol: 'openid-connect',
243+
protocolMapper: 'oidc-audience-mapper',
244+
config: {
245+
'access.token.claim': 'true',
246+
'id.token.claim': 'true',
247+
'included.client.audience': localEnv.KEYCLOAK_CLIENT_ID,
248+
'introspection.token.claim': 'true',
249+
'lightweight.claim': 'true',
250+
},
251+
})
252+
235253
export const oidcCfg = (
236254
providerCfg: OidcProviderCfg,
237255
clientId: string,

src/tasks/keycloak/realm-factory.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as utils from '../../utils'
1414
import {
1515
TeamMapping,
1616
adminUserCfgTpl,
17+
clientAudClaimMapper,
1718
clientEmailClaimMapper,
1819
clientScopeCfgTpl,
1920
clientSubClaimMapper,
@@ -120,6 +121,11 @@ export function createClientSubClaimMapper(): ProtocolMapperRepresentation {
120121
return subClaimMapper
121122
}
122123

124+
export function createClientAudClaimMapper(): ProtocolMapperRepresentation {
125+
const subClaimMapper = defaultsDeep(new ProtocolMapperRepresentation(), clientAudClaimMapper())
126+
return subClaimMapper
127+
}
128+
123129
export function createAdminUser(username: string, password: string): UserRepresentation {
124130
const userRepresentation = defaultsDeep(new UserRepresentation(), adminUserCfgTpl(username, password))
125131
return userRepresentation

0 commit comments

Comments
 (0)