Skip to content

Commit dd396b8

Browse files
committed
Follow changes
1 parent d2eda00 commit dd396b8

File tree

2 files changed

+76
-22
lines changed

2 files changed

+76
-22
lines changed

conf/apache.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LogLevel debug
2+
OAuth2TokenVerify aws_alb arn:aws:elasticloadbalancing:ap-southeast-2:test:loadbalancer/app/test/test
3+
OAuth2AcceptTokenIn header "name=X-Amzn-Oidc-Accesstoken&type="
4+
ErrorLog /dev/stderr
5+
CustomLog /dev/stdout combined
6+
7+
<Directory /var/www/html>
8+
AuthType oauth2
9+
Require valid-user
10+
</Directory>

src/jose.c

Lines changed: 66 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,33 +2236,77 @@ oauth2_jose_jwks_eckey_url_resolve(oauth2_log_t *log,
22362236
_oauth2_jose_jwks_eckey_url_resolve_response_callback);
22372237
}
22382238

2239+
static const char *_oauth2_jose_jwks_aws_alb_region(const char *arn) {
2240+
if (!arn) return NULL;
2241+
2242+
char *arn_copy = oauth2_strdup(arn);
2243+
if (!arn_copy) return NULL;
2244+
2245+
char *token = strtok(arn_copy, ":");
2246+
int count = 0;
2247+
const char *region = NULL;
2248+
2249+
while (token) {
2250+
if (count == 3) {
2251+
region = oauth2_strdup(token);
2252+
break;
2253+
}
2254+
token = strtok(NULL, ":");
2255+
count++;
2256+
}
2257+
2258+
oauth2_mem_free(arn_copy);
2259+
return region;
2260+
}
2261+
22392262
static oauth2_jose_jwk_list_t *
22402263
oauth2_jose_jwks_aws_alb_resolve(oauth2_log_t *log,
22412264
oauth2_jose_jwks_provider_t *provider,
22422265
bool *refresh, const cjose_header_t *hdr)
22432266
{
2244-
/*
2245-
* 1. pull the 'signer' and `kid` claims from the header (a typedef-ed
2246-
* JSON object)
2247-
* 2. check it against the configured provider->arb_arn value, and if
2248-
* they match:
2249-
* 3. construct the EC keys URL:
2250-
* https://public-keys.auth.elb.<region from
2251-
* ALB_ARN>.amazonaws.com/<kid>
2252-
* TODO: make the base URL configurable in
2253-
* oauth2_jose_verify_options_jwk_set_aws_alb and add a member
2254-
* alb_arn_base_url to oauth2_jose_jwks_provider_t
2255-
* 4. construct a temporary provider->jwks_uri
2256-
* 5. call:
2257-
* _oauth2_jose_jwks_resolve_from_uri(log, provider, refresh,
2258-
* oauth2_jose_jwks_eckey_url_resolve_response_callback);
2259-
* and save the result (oauth2_jose_jwk_list_t *)
2260-
* 6. free the temporary provider->jwks_uri (TODO: caching?)
2261-
* 7. return the result
2262-
*
2263-
* add unit tests
2264-
*/
2265-
return NULL;
2267+
cjose_err err;
2268+
2269+
// TODO - error here, issue with const cjose_header_t *hdr
2270+
const char *signer = cjose_header_get(hdr, "signer", &err);
2271+
const char *kid = cjose_header_get(hdr, "kid", &err);
2272+
2273+
if (!signer || !kid) {
2274+
oauth2_error(log, "missing 'signer' or 'kid' in JWT header: signer=%s, kid=%s", signer, kid);
2275+
return NULL;
2276+
}
2277+
2278+
// TODO - determine if theres a better place for this?
2279+
if (strcmp(signer, provider->alb_arn) != 0) {
2280+
oauth2_error(log, "signer does not match configured ARN: signer=%s, arn=%s", signer, provider->alb_arn);
2281+
return NULL;
2282+
}
2283+
2284+
const char *region = _oauth2_jose_jwks_aws_alb_region(provider->alb_arn);
2285+
if (!region) {
2286+
oauth2_error(log, "failed to extract region from ARN: arn=%s", provider->alb_arn);
2287+
return NULL;
2288+
}
2289+
2290+
size_t url_len = strlen("https://public-keys.auth.elb.") + strlen(region) + strlen(".amazonaws.com/") + strlen(kid) + 1;
2291+
char *url = oauth2_mem_alloc(url_len);
2292+
if (!url) {
2293+
oauth2_error(log, "oauth2_mem_alloc failed for JWKS URL");
2294+
return NULL;
2295+
}
2296+
2297+
oauth2_snprintf(url, url_len, "https://public-keys.auth.elb.%s.amazonaws.com/%s", region, kid);
2298+
oauth2_debug(log, "constructed JWKS URL: %s", url);
2299+
2300+
// TODO - should probably be a copy of provider?
2301+
oauth2_cfg_endpoint_set_url(provider->jwks_uri->endpoint, url);
2302+
2303+
oauth2_jose_jwk_list_t *result = _oauth2_jose_jwks_resolve_from_uri(
2304+
log, provider, refresh, oauth2_jose_jwks_eckey_url_resolve_response_callback
2305+
);
2306+
2307+
oauth2_mem_free(url);
2308+
2309+
return result;
22662310
}
22672311

22682312
/*

0 commit comments

Comments
 (0)