/src/mod_auth_openidc/src/cfg/oauth.c
Line | Count | Source |
1 | | /* |
2 | | * Licensed to the Apache Software Foundation (ASF) under one |
3 | | * or more contributor license agreements. See the NOTICE file |
4 | | * distributed with this work for additional information |
5 | | * regarding copyright ownership. The ASF licenses this file |
6 | | * to you under the Apache License, Version 2.0 (the |
7 | | * "License"); you may not use this file except in compliance |
8 | | * with the License. You may obtain a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, |
13 | | * software distributed under the License is distributed on an |
14 | | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | * KIND, either express or implied. See the License for the |
16 | | * specific language governing permissions and limitations |
17 | | * under the License. |
18 | | */ |
19 | | |
20 | | /*************************************************************************** |
21 | | * Copyright (C) 2017-2026 ZmartZone Holding BV |
22 | | * All rights reserved. |
23 | | * |
24 | | * DISCLAIMER OF WARRANTIES: |
25 | | * |
26 | | * THE SOFTWARE PROVIDED HEREUNDER IS PROVIDED ON AN "AS IS" BASIS, WITHOUT |
27 | | * ANY WARRANTIES OR REPRESENTATIONS EXPRESS, IMPLIED OR STATUTORY; INCLUDING, |
28 | | * WITHOUT LIMITATION, WARRANTIES OF QUALITY, PERFORMANCE, NONINFRINGEMENT, |
29 | | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NOR ARE THERE ANY |
30 | | * WARRANTIES CREATED BY A COURSE OR DEALING, COURSE OF PERFORMANCE OR TRADE |
31 | | * USAGE. FURTHERMORE, THERE ARE NO WARRANTIES THAT THE SOFTWARE WILL MEET |
32 | | * YOUR NEEDS OR BE FREE FROM ERRORS, OR THAT THE OPERATION OF THE SOFTWARE |
33 | | * WILL BE UNINTERRUPTED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR |
34 | | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
35 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF |
36 | | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
37 | | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
38 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39 | | * |
40 | | * @Author: Hans Zandbelt - hans.zandbelt@openidc.com |
41 | | */ |
42 | | |
43 | | #include "cfg/oauth.h" |
44 | | #include "cfg/cfg_int.h" |
45 | | #include "cfg/parse.h" |
46 | | #include "jose.h" |
47 | | #include "proto/proto.h" |
48 | | |
49 | | /* |
50 | | * Generate the declaration, initialization, and merge logic for simple OAuth members. Members |
51 | | * with special ownership or merge rules remain explicit below. |
52 | | */ |
53 | | #define OIDC_OAUTH_CFG_SIMPLE_MEMBERS(PTR, INT) \ |
54 | 44 | PTR(char *, metadata_url) \ |
55 | 44 | PTR(char *, verify_jwks_uri) \ |
56 | 44 | PTR(apr_array_header_t *, verify_aud_values) \ |
57 | 44 | PTR(char *, verify_issuer) \ |
58 | 44 | PTR(apr_hash_t *, verify_shared_keys) \ |
59 | 44 | PTR(apr_hash_t *, decrypt_shared_keys) \ |
60 | 44 | PTR(char *, client_id) \ |
61 | 44 | PTR(char *, client_secret) \ |
62 | 44 | PTR(char *, introspection_endpoint_url) \ |
63 | 44 | INT(introspection_endpoint_method) \ |
64 | 44 | PTR(char *, introspection_token_param_name) \ |
65 | 44 | PTR(char *, introspection_endpoint_params) \ |
66 | 44 | PTR(char *, introspection_endpoint_auth) \ |
67 | 44 | PTR(char *, introspection_endpoint_auth_alg) \ |
68 | 44 | PTR(char *, introspection_endpoint_auth_aud) \ |
69 | 44 | PTR(const char *, introspection_client_auth_bearer_token) \ |
70 | 44 | PTR(char *, introspection_endpoint_tls_client_key) \ |
71 | 44 | PTR(char *, introspection_endpoint_tls_client_key_pwd) \ |
72 | 44 | PTR(char *, introspection_endpoint_tls_client_cert) \ |
73 | 44 | INT(ssl_validate_server) |
74 | | |
75 | | #define OIDC_OAUTH_M_DECL_PTR(type, name) type name; |
76 | | #define OIDC_OAUTH_M_DECL_INT(name) int name; |
77 | 792 | #define OIDC_OAUTH_M_CREATE_PTR(type, name) o->name = NULL; |
78 | 88 | #define OIDC_OAUTH_M_CREATE_INT(name) o->name = OIDC_CONFIG_POS_INT_UNSET; |
79 | 0 | #define OIDC_OAUTH_M_MERGE_PTR(type, name) dst->name = _oidc_cfg_merge_ptr(add->name, base->name); |
80 | 0 | #define OIDC_OAUTH_M_MERGE_INT(name) dst->name = _oidc_cfg_merge_pos_int(add->name, base->name); |
81 | | |
82 | | struct oidc_oauth_t { |
83 | | |
84 | | OIDC_OAUTH_CFG_SIMPLE_MEMBERS(OIDC_OAUTH_M_DECL_PTR, OIDC_OAUTH_M_DECL_INT) |
85 | | |
86 | | /* special semantics, handled by hand in create/merge: deep-copied on merge */ |
87 | | apr_array_header_t *verify_public_keys; |
88 | | /* grouped: set/merged together with the format/required members below */ |
89 | | char *introspection_token_expiry_claim_name; |
90 | | oidc_oauth_introspection_token_expiry_claim_format_t introspection_token_expiry_claim_format; |
91 | | oidc_oauth_introspection_token_expiry_claim_required_t introspection_token_expiry_claim_required; |
92 | | /* grouped: claim_name/reg_exp/replace are set/merged together */ |
93 | | oidc_remote_user_claim_t remote_user_claim; |
94 | | }; |
95 | | |
96 | | /* |
97 | | * Body generators for the OAuth resource-server (oidc_oauth_t) accessors |
98 | | * declared in cfg/oauth.h: for member `foo` they emit oidc_cmd_oauth_foo_set(), |
99 | | * oidc_cfg_oauth_foo_set() and oidc_cfg_oauth_foo_get(); those names are |
100 | | * token-pasted here and so appear in no source line. |
101 | | */ |
102 | | #define OIDC_OAUTH_MEMBER_FUNC_GET(member, type) \ |
103 | 22.5k | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ |
104 | 22.5k | return cfg->oauth->member; \ |
105 | 22.5k | } oidc_cfg_oauth_metadata_url_get Line | Count | Source | 103 | 4.08k | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 104 | 4.08k | return cfg->oauth->member; \ | 105 | 4.08k | } |
Unexecuted instantiation: oidc_cfg_oauth_client_id_get Unexecuted instantiation: oidc_cfg_oauth_client_secret_get Unexecuted instantiation: oidc_cfg_oauth_introspection_endpoint_params_get Unexecuted instantiation: oidc_cfg_oauth_introspection_endpoint_auth_aud_get Unexecuted instantiation: oidc_cfg_oauth_introspection_endpoint_tls_client_cert_get Unexecuted instantiation: oidc_cfg_oauth_introspection_endpoint_tls_client_key_get Unexecuted instantiation: oidc_cfg_oauth_introspection_endpoint_tls_client_key_pwd_get oidc_cfg_oauth_verify_public_keys_get Line | Count | Source | 103 | 6.06k | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 104 | 6.06k | return cfg->oauth->member; \ | 105 | 6.06k | } |
oidc_cfg_oauth_verify_aud_values_get Line | Count | Source | 103 | 926 | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 104 | 926 | return cfg->oauth->member; \ | 105 | 926 | } |
oidc_cfg_oauth_verify_issuer_get Line | Count | Source | 103 | 1.27k | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 104 | 1.27k | return cfg->oauth->member; \ | 105 | 1.27k | } |
Unexecuted instantiation: oidc_cfg_oauth_introspection_client_auth_bearer_token_get Unexecuted instantiation: oidc_cfg_oauth_introspection_token_expiry_claim_format_get Unexecuted instantiation: oidc_cfg_oauth_introspection_token_expiry_claim_required_get oidc_cfg_oauth_verify_shared_keys_get Line | Count | Source | 103 | 7.36k | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 104 | 7.36k | return cfg->oauth->member; \ | 105 | 7.36k | } |
oidc_cfg_oauth_decrypt_shared_keys_get Line | Count | Source | 103 | 2.80k | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 104 | 2.80k | return cfg->oauth->member; \ | 105 | 2.80k | } |
|
106 | | |
107 | | #define OIDC_OAUTH_MEMBER_FUNCS_TYPE(member, type, valid) \ |
108 | 2 | const char *oidc_cmd_oauth_##member##_set(cmd_parms *cmd, void *ptr, const char *arg) { \ |
109 | 2 | oidc_cfg_t *cfg = \ |
110 | 2 | (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); \ |
111 | 2 | const char *rv = valid; \ |
112 | 2 | if (rv == NULL) \ |
113 | 2 | cfg->oauth->member = apr_pstrdup(cmd->pool, arg); \ |
114 | 2 | return OIDC_CONFIG_DIR_RV(cmd, rv); \ |
115 | 2 | } \ |
116 | | OIDC_OAUTH_MEMBER_FUNC_GET(member, type) |
117 | | |
118 | | #define OIDC_OAUTH_MEMBER_FUNCS_INT(member, parse, type, def_val) \ |
119 | 0 | const char *oidc_cmd_oauth_##member##_set(cmd_parms *cmd, void *ptr, const char *arg) { \ |
120 | 0 | oidc_cfg_t *cfg = \ |
121 | 0 | (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); \ |
122 | 0 | int v = -1; \ |
123 | 0 | const char *rv = parse; \ |
124 | 0 | if (rv == NULL) \ |
125 | 0 | cfg->oauth->member = v; \ |
126 | 0 | return OIDC_CONFIG_DIR_RV(cmd, rv); \ |
127 | 0 | } \ |
128 | | \ |
129 | 1.29k | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ |
130 | 1.29k | if (cfg->oauth->member == OIDC_CONFIG_POS_INT_UNSET) \ |
131 | 1.29k | return def_val; \ |
132 | 1.29k | return cfg->oauth->member; \ |
133 | 1.29k | } oidc_cfg_oauth_ssl_validate_server_get Line | Count | Source | 129 | 1.29k | type oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 130 | 1.29k | if (cfg->oauth->member == OIDC_CONFIG_POS_INT_UNSET) \ | 131 | 1.29k | return def_val; \ | 132 | 1.29k | return cfg->oauth->member; \ | 133 | 1.29k | } |
Unexecuted instantiation: oidc_cfg_oauth_introspection_endpoint_method_get |
134 | | |
135 | | #define OIDC_OAUTH_MEMBER_FUNCS_BOOL(member, def_val) \ |
136 | | OIDC_OAUTH_MEMBER_FUNCS_INT(member, oidc_cfg_parse_boolean(cmd->pool, arg, &v), int, def_val) |
137 | | |
138 | | #define OIDC_OAUTH_MEMBER_FUNCS_KEYS(member) \ |
139 | 0 | const char *oidc_cmd_oauth_##member##_set(cmd_parms *cmd, void *ptr, const char *arg) { \ |
140 | 0 | oidc_cfg_t *cfg = \ |
141 | 0 | (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); \ |
142 | 0 | return oidc_cfg_parse_public_key_files(cmd->pool, arg, &cfg->oauth->member); \ |
143 | 0 | } \ |
144 | | OIDC_OAUTH_MEMBER_FUNC_GET(member, const apr_array_header_t *) |
145 | | |
146 | | #define OIDC_OAUTH_MEMBER_FUNCS_STR_LIST(member) \ |
147 | 2 | const char *oidc_cmd_oauth_##member##_set(cmd_parms *cmd, void *ptr, const char *arg) { \ |
148 | 2 | oidc_cfg_t *cfg = \ |
149 | 2 | (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); \ |
150 | 2 | const char *rv = oidc_cfg_string_list_add(cmd->pool, &cfg->oauth->member, arg); \ |
151 | 2 | return OIDC_CONFIG_DIR_RV(cmd, rv); \ |
152 | 2 | } \ |
153 | | OIDC_OAUTH_MEMBER_FUNC_GET(member, const apr_array_header_t *) |
154 | | |
155 | | #define OIDC_OAUTH_MEMBER_FUNCS_STR(member) OIDC_OAUTH_MEMBER_FUNCS_TYPE(member, const char *, NULL) |
156 | | #define OIDC_OAUTH_MEMBER_FUNCS_URL(member) \ |
157 | | OIDC_OAUTH_MEMBER_FUNCS_TYPE(member, const char *, oidc_cfg_parse_is_valid_http_url(cmd->pool, arg)) |
158 | | |
159 | | #define OIDC_OAUTH_MEMBER_FUNC_STR_GET_DEF(member, def_val) \ |
160 | 14.5k | const char *oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ |
161 | 14.5k | return cfg->oauth->member ? cfg->oauth->member : def_val; \ |
162 | 14.5k | } Unexecuted instantiation: oidc_cfg_oauth_introspection_token_expiry_claim_name_get oidc_cfg_oauth_introspection_endpoint_url_get Line | Count | Source | 160 | 8.47k | const char *oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 161 | 8.47k | return cfg->oauth->member ? cfg->oauth->member : def_val; \ | 162 | 8.47k | } |
oidc_cfg_oauth_verify_jwks_uri_get Line | Count | Source | 160 | 6.06k | const char *oidc_cfg_oauth_##member##_get(const oidc_cfg_t *cfg) { \ | 161 | 6.06k | return cfg->oauth->member ? cfg->oauth->member : def_val; \ | 162 | 6.06k | } |
Unexecuted instantiation: oidc_cfg_oauth_introspection_token_param_name_get |
163 | | |
164 | | #define OIDC_OAUTH_MEMBER_FUNCS_FILE(member) \ |
165 | 0 | const char *oidc_cmd_oauth_##member##_set(cmd_parms *cmd, void *ptr, const char *arg) { \ |
166 | 0 | oidc_cfg_t *cfg = \ |
167 | 0 | (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); \ |
168 | 0 | const char *rv = oidc_cfg_parse_filename(cmd->pool, arg, &cfg->oauth->member); \ |
169 | 0 | return OIDC_CONFIG_DIR_RV(cmd, rv); \ |
170 | 0 | } \ |
171 | | OIDC_OAUTH_MEMBER_FUNC_GET(member, const char *) |
172 | | |
173 | | #define OIDC_OAUTH_MEMBER_FUNCS_PASSPHRASE(member) \ |
174 | 0 | const char *oidc_cmd_oauth_##member##_set(cmd_parms *cmd, void *ptr, const char *arg) { \ |
175 | 0 | oidc_cfg_t *cfg = \ |
176 | 0 | (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); \ |
177 | 0 | const char *rv = oidc_cfg_parse_passphrase(cmd->pool, arg, &cfg->oauth->member); \ |
178 | 0 | return OIDC_CONFIG_DIR_RV(cmd, rv); \ |
179 | 0 | } \ |
180 | | OIDC_OAUTH_MEMBER_FUNC_GET(member, const char *) |
181 | | |
182 | 0 | OIDC_OAUTH_MEMBER_FUNCS_URL(metadata_url) |
183 | 0 | OIDC_OAUTH_MEMBER_FUNCS_STR(client_id) |
184 | 0 | OIDC_OAUTH_MEMBER_FUNCS_STR(client_secret) |
185 | 0 | OIDC_OAUTH_MEMBER_FUNCS_STR(introspection_endpoint_params) |
186 | 0 | OIDC_OAUTH_MEMBER_FUNCS_STR(introspection_endpoint_auth_aud) |
187 | 0 | OIDC_OAUTH_MEMBER_FUNCS_FILE(introspection_endpoint_tls_client_cert) |
188 | 0 | OIDC_OAUTH_MEMBER_FUNCS_FILE(introspection_endpoint_tls_client_key) |
189 | 0 | OIDC_OAUTH_MEMBER_FUNCS_PASSPHRASE(introspection_endpoint_tls_client_key_pwd) |
190 | | OIDC_OAUTH_MEMBER_FUNCS_KEYS(verify_public_keys) |
191 | 2 | OIDC_OAUTH_MEMBER_FUNCS_STR_LIST(verify_aud_values) |
192 | 10 | OIDC_OAUTH_MEMBER_FUNCS_STR(verify_issuer) |
193 | 10 | OIDC_OAUTH_MEMBER_FUNC_GET(introspection_client_auth_bearer_token, const char *) |
194 | 10 | OIDC_OAUTH_MEMBER_FUNC_GET(introspection_token_expiry_claim_format, |
195 | 10 | oidc_oauth_introspection_token_expiry_claim_format_t) |
196 | 10 | OIDC_OAUTH_MEMBER_FUNC_GET(introspection_token_expiry_claim_required, |
197 | 10 | oidc_oauth_introspection_token_expiry_claim_required_t) |
198 | 10 | OIDC_OAUTH_MEMBER_FUNC_GET(verify_shared_keys, apr_hash_t *) |
199 | 10 | OIDC_OAUTH_MEMBER_FUNC_GET(decrypt_shared_keys, apr_hash_t *) |
200 | 10 | |
201 | 10 | #define OIDC_DEFAULT_OAUTH_SSL_VALIDATE_SERVER 1 |
202 | 10 | OIDC_OAUTH_MEMBER_FUNCS_BOOL(ssl_validate_server, OIDC_DEFAULT_OAUTH_SSL_VALIDATE_SERVER) |
203 | 0 |
|
204 | 0 | #define OIDC_DEFAULT_OAUTH_EXPIRY_CLAIM_NAME OIDC_PROTO_EXPIRES_IN |
205 | 0 | OIDC_OAUTH_MEMBER_FUNC_STR_GET_DEF(introspection_token_expiry_claim_name, OIDC_DEFAULT_OAUTH_EXPIRY_CLAIM_NAME) |
206 | 0 |
|
207 | 0 | #define OIDC_OAUTH_MEMBER_FUNCS_STR_VALID(member, valid, def_val) \ |
208 | 0 | const char *oidc_cfg_oauth_##member##_set(apr_pool_t *pool, oidc_cfg_t *cfg, const char *arg) { \ |
209 | 0 | const char *rv = valid; \ |
210 | 0 | if (rv == NULL) \ |
211 | 0 | cfg->oauth->member = apr_pstrdup(pool, arg); \ |
212 | 0 | return rv; \ |
213 | 0 | } \ |
214 | 0 | const char *oidc_cmd_oauth_##member##_set(cmd_parms *cmd, void *ptr, const char *arg) { \ |
215 | 0 | oidc_cfg_t *cfg = \ |
216 | 0 | (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); \ |
217 | 0 | const char *rv = oidc_cfg_oauth_##member##_set(cmd->pool, cfg, arg); \ |
218 | 0 | return OIDC_CONFIG_DIR_RV(cmd, rv); \ |
219 | 0 | } \ |
220 | | OIDC_OAUTH_MEMBER_FUNC_STR_GET_DEF(member, def_val) |
221 | | |
222 | 0 | OIDC_OAUTH_MEMBER_FUNCS_STR_VALID(introspection_endpoint_url, oidc_cfg_parse_is_valid_http_url(pool, arg), NULL) Unexecuted instantiation: oidc_cfg_oauth_introspection_endpoint_url_set Unexecuted instantiation: oidc_cmd_oauth_introspection_endpoint_url_set |
223 | 0 | OIDC_OAUTH_MEMBER_FUNCS_STR_VALID(verify_jwks_uri, oidc_cfg_parse_is_valid_http_url(pool, arg), NULL) Unexecuted instantiation: oidc_cfg_oauth_verify_jwks_uri_set Unexecuted instantiation: oidc_cmd_oauth_verify_jwks_uri_set |
224 | | |
225 | | #define OIDC_DEFAULT_OAUTH_TOKEN_PARAM_NAME "token" |
226 | 0 | OIDC_OAUTH_MEMBER_FUNCS_STR_VALID(introspection_token_param_name, NULL, OIDC_DEFAULT_OAUTH_TOKEN_PARAM_NAME) Unexecuted instantiation: oidc_cfg_oauth_introspection_token_param_name_set Unexecuted instantiation: oidc_cmd_oauth_introspection_token_param_name_set |
227 | | |
228 | 0 | const char *oidc_cfg_oauth_introspection_endpoint_auth_set(apr_pool_t *pool, oidc_cfg_t *cfg, const char *arg) { |
229 | 0 | return oidc_cfg_endpoint_auth_set(pool, cfg, arg, &cfg->oauth->introspection_endpoint_auth, |
230 | 0 | &cfg->oauth->introspection_endpoint_auth_alg); |
231 | 0 | } |
232 | | |
233 | 0 | const char *oidc_cmd_oauth_introspection_endpoint_auth_set(cmd_parms *cmd, void *ptr, const char *arg) { |
234 | 0 | oidc_cfg_t *cfg = (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); |
235 | 0 | const char *rv = oidc_cfg_oauth_introspection_endpoint_auth_set(cmd->pool, cfg, arg); |
236 | 0 | return OIDC_CONFIG_DIR_RV(cmd, rv); |
237 | 0 | } |
238 | | |
239 | 0 | const char *oidc_cfg_oauth_introspection_endpoint_auth_get(const oidc_cfg_t *cfg) { |
240 | 0 | return cfg->oauth->introspection_endpoint_auth; |
241 | 0 | } |
242 | | |
243 | 0 | const char *oidc_cfg_oauth_introspection_endpoint_auth_alg_get(const oidc_cfg_t *cfg) { |
244 | 0 | return cfg->oauth->introspection_endpoint_auth_alg; |
245 | 0 | } |
246 | | |
247 | 2.00k | oidc_remote_user_claim_t *oidc_cfg_oauth_remote_user_claim_get(const oidc_cfg_t *cfg) { |
248 | 2.00k | return &cfg->oauth->remote_user_claim; |
249 | 2.00k | } |
250 | | |
251 | 1.34k | #define OIDC_DEFAULT_OAUTH_CLAIM_REMOTE_USER "sub" |
252 | | |
253 | 1.34k | const char *oidc_cfg_oauth_remote_user_claim_name_get(const oidc_cfg_t *cfg) { |
254 | 1.34k | return cfg->oauth->remote_user_claim.claim_name != NULL ? cfg->oauth->remote_user_claim.claim_name |
255 | 1.34k | : OIDC_DEFAULT_OAUTH_CLAIM_REMOTE_USER; |
256 | 1.34k | } |
257 | | |
258 | | const char *oidc_cmd_oauth_remote_user_claim_set(cmd_parms *cmd, void *ptr, const char *v1, const char *v2, |
259 | 0 | const char *v3) { |
260 | 0 | oidc_cfg_t *cfg = (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); |
261 | 0 | const char *rv = oidc_parse_remote_user_claim(cmd->pool, v1, v2, v3, &cfg->oauth->remote_user_claim); |
262 | 0 | return OIDC_CONFIG_DIR_RV(cmd, rv); |
263 | 0 | } |
264 | | |
265 | 0 | #define OIDC_INTROSPECTION_METHOD_GET_STR "GET" |
266 | 0 | #define OIDC_INTROSPECTION_METHOD_POST_STR "POST" |
267 | | |
268 | 0 | static const char *oidc_parse_introspection_endpoint_method(apr_pool_t *pool, const char *arg, int *v) { |
269 | 0 | static const oidc_cfg_option_t options[] = { |
270 | 0 | {OIDC_INTROSPECTION_METHOD_GET, OIDC_INTROSPECTION_METHOD_GET_STR}, |
271 | 0 | {OIDC_INTROSPECTION_METHOD_POST, OIDC_INTROSPECTION_METHOD_POST_STR}, |
272 | 0 | }; |
273 | 0 | return oidc_cfg_parse_option(pool, options, OIDC_CFG_OPTIONS_SIZE(options), arg, v); |
274 | 0 | } |
275 | | |
276 | | #define OIDC_DEFAULT_OAUTH_ENDPOINT_METHOD OIDC_INTROSPECTION_METHOD_POST |
277 | | |
278 | 0 | OIDC_OAUTH_MEMBER_FUNCS_INT(introspection_endpoint_method, oidc_parse_introspection_endpoint_method(cmd->pool, arg, &v), |
279 | | oidc_oauth_introspection_endpoint_method_t, OIDC_DEFAULT_OAUTH_ENDPOINT_METHOD) |
280 | | |
281 | | /* |
282 | | * set the introspection authorization static bearer token |
283 | | */ |
284 | | const char *oidc_cmd_oauth_introspection_client_auth_bearer_token_set(cmd_parms *cmd, void *struct_ptr, |
285 | 0 | const char *args) { |
286 | 0 | oidc_cfg_t *cfg = (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); |
287 | 0 | const char *w = ap_getword_conf(cmd->pool, &args); |
288 | 0 | cfg->oauth->introspection_client_auth_bearer_token = (*w == '\0' || *args != 0) ? "" : w; |
289 | 0 | return NULL; |
290 | 0 | } |
291 | | |
292 | 0 | #define OIDC_CLAIM_FORMAT_RELATIVE_STR "relative" |
293 | 0 | #define OIDC_CLAIM_FORMAT_ABSOLUTE_STR "absolute" |
294 | | |
295 | 0 | #define OIDC_CLAIM_REQUIRED_MANDATORY_STR "mandatory" |
296 | 0 | #define OIDC_CLAIM_REQUIRED_OPTIONAL_STR "optional" |
297 | | |
298 | | /* |
299 | | * set the syntax of the token expiry claim in the introspection response |
300 | | */ |
301 | | const char *oidc_cmd_oauth_token_expiry_claim_set(cmd_parms *cmd, void *dummy, const char *claim_name, |
302 | 0 | const char *claim_format, const char *claim_required) { |
303 | 0 | oidc_cfg_t *cfg = (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); |
304 | 0 | static const oidc_cfg_option_t claim_format_options[] = { |
305 | 0 | {OIDC_TOKEN_EXPIRY_CLAIM_FORMAT_RELATIVE, OIDC_CLAIM_FORMAT_RELATIVE_STR}, |
306 | 0 | {OIDC_TOKEN_EXPIRY_CLAIM_FORMAT_ABSOLUTE, OIDC_CLAIM_FORMAT_ABSOLUTE_STR}}; |
307 | 0 | static const oidc_cfg_option_t claim_required_options[] = { |
308 | 0 | {OIDC_TOKEN_EXPIRY_CLAIM_REQUIRED_MANDATORY, OIDC_CLAIM_REQUIRED_MANDATORY_STR}, |
309 | 0 | {OIDC_TOKEN_EXPIRY_CLAIM_REQUIRED_OPTIONAL, OIDC_CLAIM_REQUIRED_OPTIONAL_STR}}; |
310 | |
|
311 | 0 | const char *rv = NULL; |
312 | |
|
313 | 0 | cfg->oauth->introspection_token_expiry_claim_name = apr_pstrdup(cmd->pool, claim_name); |
314 | |
|
315 | 0 | if ((rv == NULL) && (claim_format != NULL)) |
316 | 0 | rv = oidc_cfg_parse_option(cmd->pool, claim_format_options, OIDC_CFG_OPTIONS_SIZE(claim_format_options), |
317 | 0 | claim_format, (int *)&cfg->oauth->introspection_token_expiry_claim_format); |
318 | |
|
319 | 0 | if ((rv == NULL) && (claim_required != NULL)) |
320 | 0 | rv = oidc_cfg_parse_option(cmd->pool, claim_required_options, |
321 | 0 | OIDC_CFG_OPTIONS_SIZE(claim_required_options), claim_required, |
322 | 0 | (int *)&cfg->oauth->introspection_token_expiry_claim_required); |
323 | |
|
324 | 0 | return OIDC_CONFIG_DIR_RV(cmd, rv); |
325 | 0 | } |
326 | | |
327 | | /* |
328 | | * parse a shared-key record from a directive value and add the resulting symmetric JWK to the given |
329 | | * (lazily created) hash of shared keys |
330 | | */ |
331 | 2 | static const char *oidc_cmd_oauth_shared_keys_add(cmd_parms *cmd, const char *arg, apr_hash_t **keys) { |
332 | 2 | oidc_jose_error_t err; |
333 | 2 | oidc_jwk_t *jwk = NULL; |
334 | 2 | char *use = NULL; |
335 | 2 | char *kid = NULL; |
336 | 2 | char *secret = NULL; |
337 | 2 | int key_len = 0; |
338 | | |
339 | 2 | const char *rv = |
340 | 2 | oidc_cfg_parse_key_record(cmd->pool, arg, &kid, &secret, &key_len, &use, NULL, OIDC_KEY_RECORD_TRIPLET); |
341 | 2 | if (rv != NULL) |
342 | 0 | return rv; |
343 | | |
344 | 2 | jwk = oidc_jwk_create_symmetric_key(cmd->pool, kid, (const unsigned char *)secret, key_len, TRUE, &err); |
345 | 2 | if (jwk == NULL) { |
346 | 0 | return apr_psprintf(cmd->pool, "oidc_jwk_create_symmetric_key failed for (kid=%s) \"%s\": %s", kid, |
347 | 0 | secret, oidc_jose_e2s(cmd->pool, err)); |
348 | 0 | } |
349 | | |
350 | 2 | if (*keys == NULL) |
351 | 2 | *keys = apr_hash_make(cmd->pool); |
352 | 2 | if (use) |
353 | 0 | jwk->use = apr_pstrdup(cmd->pool, use); |
354 | 2 | apr_hash_set(*keys, jwk->kid, APR_HASH_KEY_STRING, jwk); |
355 | | |
356 | 2 | return NULL; |
357 | 2 | } |
358 | | |
359 | | /* |
360 | | * add a shared key to a list of JWKs with shared keys |
361 | | */ |
362 | 2 | const char *oidc_cmd_oauth_verify_shared_keys_set(cmd_parms *cmd, void *struct_ptr, const char *arg) { |
363 | 2 | oidc_cfg_t *cfg = (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); |
364 | 2 | return oidc_cmd_oauth_shared_keys_add(cmd, arg, &cfg->oauth->verify_shared_keys); |
365 | 2 | } |
366 | | |
367 | | /* |
368 | | * add a shared key to the list of JWKs used to decrypt encrypted JWT access tokens |
369 | | */ |
370 | 0 | const char *oidc_cmd_oauth_decrypt_shared_keys_set(cmd_parms *cmd, void *struct_ptr, const char *arg) { |
371 | 0 | oidc_cfg_t *cfg = (oidc_cfg_t *)ap_get_module_config(cmd->server->module_config, &auth_openidc_module); |
372 | 0 | return oidc_cmd_oauth_shared_keys_add(cmd, arg, &cfg->oauth->decrypt_shared_keys); |
373 | 0 | } |
374 | | |
375 | | /* default OAuth 2.0 non-spec compliant introspection expiry claim format */ |
376 | | #define OIDC_DEFAULT_OAUTH_EXPIRY_CLAIM_FORMAT OIDC_CLAIM_FORMAT_RELATIVE_STR |
377 | | /* default OAuth 2.0 non-spec compliant introspection expiry claim required */ |
378 | | #define OIDC_DEFAULT_OAUTH_EXPIRY_CLAIM_REQUIRED TRUE |
379 | | |
380 | 44 | oidc_oauth_t *oidc_cfg_oauth_create(apr_pool_t *pool) { |
381 | 44 | oidc_oauth_t *o = apr_pcalloc(pool, sizeof(oidc_oauth_t)); |
382 | 792 | OIDC_OAUTH_CFG_SIMPLE_MEMBERS(OIDC_OAUTH_M_CREATE_PTR, OIDC_OAUTH_M_CREATE_INT) |
383 | 44 | o->verify_public_keys = NULL; |
384 | 44 | o->introspection_token_expiry_claim_name = NULL; |
385 | 44 | o->introspection_token_expiry_claim_format = OIDC_TOKEN_EXPIRY_CLAIM_FORMAT_RELATIVE; |
386 | 44 | o->introspection_token_expiry_claim_required = OIDC_TOKEN_EXPIRY_CLAIM_REQUIRED_MANDATORY; |
387 | 44 | o->remote_user_claim.claim_name = NULL; |
388 | 44 | o->remote_user_claim.reg_exp = NULL; |
389 | 44 | o->remote_user_claim.replace = NULL; |
390 | 44 | return o; |
391 | 44 | } |
392 | | |
393 | | /* Shallow request copy for endpoint overrides; members still belong to the shared source. */ |
394 | 0 | oidc_oauth_t *oidc_cfg_oauth_shallow_copy(apr_pool_t *pool, const oidc_oauth_t *src) { |
395 | 0 | return apr_pmemdup(pool, src, sizeof(*src)); |
396 | 0 | } |
397 | | |
398 | 0 | void oidc_cfg_oauth_merge(apr_pool_t *pool, oidc_oauth_t *dst, const oidc_oauth_t *base, const oidc_oauth_t *add) { |
399 | 0 | OIDC_OAUTH_CFG_SIMPLE_MEMBERS(OIDC_OAUTH_M_MERGE_PTR, OIDC_OAUTH_M_MERGE_INT) |
400 | | /* introspection_endpoint_auth carries an optional ":<alg>" suffix parsed into |
401 | | * introspection_endpoint_auth_alg; merge the pair as a unit so a vhost that re-sets the method |
402 | | * takes its (possibly absent) alg instead of inheriting the base server's alg */ |
403 | 0 | if (add->introspection_endpoint_auth != NULL) |
404 | 0 | dst->introspection_endpoint_auth_alg = add->introspection_endpoint_auth_alg; |
405 | |
|
406 | 0 | dst->verify_public_keys = |
407 | 0 | oidc_jwk_list_copy(pool, _oidc_cfg_merge_ptr(add->verify_public_keys, base->verify_public_keys)); |
408 | |
|
409 | 0 | if (add->introspection_token_expiry_claim_name != NULL) { |
410 | 0 | dst->introspection_token_expiry_claim_name = add->introspection_token_expiry_claim_name; |
411 | 0 | dst->introspection_token_expiry_claim_format = add->introspection_token_expiry_claim_format; |
412 | 0 | dst->introspection_token_expiry_claim_required = add->introspection_token_expiry_claim_required; |
413 | 0 | } else { |
414 | 0 | dst->introspection_token_expiry_claim_name = base->introspection_token_expiry_claim_name; |
415 | 0 | dst->introspection_token_expiry_claim_format = base->introspection_token_expiry_claim_format; |
416 | 0 | dst->introspection_token_expiry_claim_required = base->introspection_token_expiry_claim_required; |
417 | 0 | } |
418 | |
|
419 | 0 | if (add->remote_user_claim.claim_name != NULL) { |
420 | 0 | dst->remote_user_claim = add->remote_user_claim; |
421 | 0 | } else { |
422 | 0 | dst->remote_user_claim = base->remote_user_claim; |
423 | 0 | } |
424 | 0 | } |
425 | | |
426 | 0 | void oidc_cfg_oauth_destroy(oidc_oauth_t *o) { |
427 | 0 | if (o == NULL) |
428 | 0 | return; |
429 | 0 | oidc_jwk_list_destroy(o->verify_public_keys); |
430 | 0 | o->verify_public_keys = NULL; |
431 | 0 | oidc_jwk_list_destroy_hash(o->verify_shared_keys); |
432 | 0 | o->verify_shared_keys = NULL; |
433 | 0 | oidc_jwk_list_destroy_hash(o->decrypt_shared_keys); |
434 | | o->decrypt_shared_keys = NULL; |
435 | 0 | } |