Skip to content

Commit

Permalink
Use keycloak token for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoliaw committed Oct 31, 2024
1 parent ceef81c commit f5bd52a
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions policy/diamond/policy/token/token.rego
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@ package diamond.policy.token

import rego.v1

verify(token) := profile.sub if {
profile := http.send({
"url": opa.runtime().env.USERINFO_ENDPOINT,
"method": "GET",
"headers": {"authorization": token},
})
}
fetch_jwks(url) := http.send({
"url": url,
"method": "GET",
"force_cache": true,
"force_cache_duration_seconds": 86400,
})

jwks_endpoint := opa.runtime().env.JWKS_ENDPOINT

issuer := opa.runtime().env.ISSUER

unverified := io.jwt.decode(input.token)

jwt_header := unverified[0]

jwks_url := concat("?", [jwks_endpoint, urlquery.encode_object({"kid": jwt_header.kid})])

jwks := fetch_jwks(jwks_url).raw_body

verified := io.jwt.decode_verify(input.token, {
"cert": jwks,
"iss": issuer,
"aud": input.audience,
})

claims := verified[2] if verified[0]

0 comments on commit f5bd52a

Please sign in to comment.