|
| 1 | +/*************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (C) 2018-2019 - ZmartZone Holding BV - www.zmartzone.eu |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero General Public License as |
| 7 | + * published by the Free Software Foundation, either version 3 of the |
| 8 | + * License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU Affero General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Affero General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + * |
| 18 | + * @Author: Hans Zandbelt - hans.zandbelt@zmartzone.eu |
| 19 | + * |
| 20 | + **************************************************************************/ |
| 21 | + |
| 22 | +#include "check_liboauth2.h" |
| 23 | +#include "oauth2/apache.h" |
| 24 | +#include <check.h> |
| 25 | + |
| 26 | +static apr_pool_t *pool = NULL; |
| 27 | +static request_rec *request = NULL; |
| 28 | +static oauth2_log_t *log = 0; |
| 29 | + |
| 30 | +static request_rec *setup_request(apr_pool_t *pool) |
| 31 | +{ |
| 32 | + // const unsigned int kIdx = 0; |
| 33 | + // const unsigned int kEls = kIdx + 1; |
| 34 | + request_rec *request = |
| 35 | + (request_rec *)apr_pcalloc(pool, sizeof(request_rec)); |
| 36 | + |
| 37 | + request->pool = pool; |
| 38 | + |
| 39 | + request->headers_in = apr_table_make(request->pool, 0); |
| 40 | + request->headers_out = apr_table_make(request->pool, 0); |
| 41 | + request->err_headers_out = apr_table_make(request->pool, 0); |
| 42 | + |
| 43 | + apr_table_set(request->headers_in, "Host", "www.example.com"); |
| 44 | + apr_table_set(request->headers_in, "OIDC_foo", "some-value"); |
| 45 | + apr_table_set(request->headers_in, "Cookie", |
| 46 | + "foo=bar; " |
| 47 | + "oauth2_openidc_session" |
| 48 | + "=0123456789abcdef; baz=zot"); |
| 49 | + |
| 50 | + request->server = apr_pcalloc(request->pool, sizeof(struct server_rec)); |
| 51 | + request->server->process = |
| 52 | + apr_pcalloc(request->pool, sizeof(struct process_rec)); |
| 53 | + request->server->process->pool = request->pool; |
| 54 | + request->connection = |
| 55 | + apr_pcalloc(request->pool, sizeof(struct conn_rec)); |
| 56 | + request->connection->bucket_alloc = |
| 57 | + apr_bucket_alloc_create(request->pool); |
| 58 | + request->connection->local_addr = |
| 59 | + apr_pcalloc(request->pool, sizeof(apr_sockaddr_t)); |
| 60 | + |
| 61 | + apr_pool_userdata_set("https", "scheme", NULL, request->pool); |
| 62 | + request->server->server_hostname = "www.example.com"; |
| 63 | + request->connection->local_addr->port = 443; |
| 64 | + request->unparsed_uri = "/bla?foo=bar¶m1=value1"; |
| 65 | + request->args = "foo=bar¶m1=value1"; |
| 66 | + apr_uri_parse(request->pool, |
| 67 | + "https://www.example.com/bla?foo=bar¶m1=value1", |
| 68 | + &request->parsed_uri); |
| 69 | + /* |
| 70 | + auth_openidc_module.module_index = kIdx; |
| 71 | + oidc_cfg *cfg = oidc_create_server_config(request->pool, |
| 72 | + request->server); cfg->provider.issuer = "https://idp.example.com"; |
| 73 | + cfg->provider.authorization_endpoint_url = |
| 74 | + "https://idp.example.com/authorize"; |
| 75 | + cfg->provider.scope = "openid"; |
| 76 | + cfg->provider.client_id = "client_id"; |
| 77 | + cfg->provider.token_binding_policy = |
| 78 | + OIDC_TOKEN_BINDING_POLICY_OPTIONAL; cfg->redirect_uri = |
| 79 | + "https://www.example.com/protected/"; |
| 80 | +
|
| 81 | + oidc_dir_cfg *d_cfg = oidc_create_dir_config(request->pool, |
| 82 | + NULL); |
| 83 | + */ |
| 84 | + /* |
| 85 | + request->server->module_config = apr_pcalloc(request->pool, |
| 86 | + sizeof(ap_conf_vector_t *) * kEls); |
| 87 | + request->per_dir_config = apr_pcalloc(request->pool, |
| 88 | + sizeof(ap_conf_vector_t *) * kEls); |
| 89 | + */ |
| 90 | + /* |
| 91 | + ap_set_module_config(request->server->module_config, |
| 92 | + &auth_openidc_module, cfg); |
| 93 | + ap_set_module_config(request->per_dir_config, |
| 94 | + &auth_openidc_module, d_cfg); |
| 95 | +
|
| 96 | + cfg->crypto_passphrase = "12345678901234567890123456789012"; |
| 97 | + cfg->cache = &oidc_cache_shm; |
| 98 | + cfg->cache_cfg = NULL; |
| 99 | + cfg->cache_shm_size_max = 500; |
| 100 | + cfg->cache_shm_entry_size_max = 16384 + 255 + 17; |
| 101 | + cfg->cache_encrypt = 1; |
| 102 | + if (cfg->cache->post_config(request->server) != OK) { |
| 103 | + printf("cfg->cache->post_config failed!\n"); |
| 104 | + exit(-1); |
| 105 | + } |
| 106 | + */ |
| 107 | + return request; |
| 108 | +} |
| 109 | + |
| 110 | +static void check_apache_log_request(oauth2_log_sink_t *sink, |
| 111 | + const char *filename, unsigned long line, |
| 112 | + const char *function, |
| 113 | + oauth2_log_level_t level, const char *msg) |
| 114 | +{ |
| 115 | + oauth2_log(log, filename, line, function, level, "%s", msg); |
| 116 | +} |
| 117 | + |
| 118 | +static void setup(void) |
| 119 | +{ |
| 120 | + log = oauth2_init(OAUTH2_LOG_TRACE1, 0); |
| 121 | + |
| 122 | + apr_initialize(); |
| 123 | + apr_pool_create(&pool, NULL); |
| 124 | + request = setup_request(pool); |
| 125 | +} |
| 126 | + |
| 127 | +static void teardown(void) |
| 128 | +{ |
| 129 | + apr_pool_destroy(pool); |
| 130 | + apr_terminate(); |
| 131 | + |
| 132 | + oauth2_shutdown(log); |
| 133 | +} |
| 134 | + |
| 135 | +START_TEST(test_apache_request_state) |
| 136 | +{ |
| 137 | + json_error_t err; |
| 138 | + const char *s_claims = "{ \"sub\": \"joe\" }"; |
| 139 | + json_t *in_claims = NULL, *out_claims = NULL; |
| 140 | + const char *key = "C"; |
| 141 | + char *value = NULL; |
| 142 | + oauth2_apache_request_ctx_t *ctx = NULL; |
| 143 | + |
| 144 | + ctx = oauth2_apache_request_context(request, check_apache_log_request, |
| 145 | + "check_apache"); |
| 146 | + in_claims = json_loads(s_claims, 0, &err); |
| 147 | + |
| 148 | + oauth2_apache_request_state_set_json(ctx, key, in_claims); |
| 149 | + |
| 150 | + oauth2_apache_request_state_get_json(ctx, key, &out_claims); |
| 151 | + ck_assert_ptr_ne(out_claims, NULL); |
| 152 | + |
| 153 | + oauth2_json_string_get(log, out_claims, "sub", &value, NULL); |
| 154 | + ck_assert_ptr_ne(value, NULL); |
| 155 | + ck_assert_str_eq(value, "joe"); |
| 156 | + |
| 157 | + json_decref(in_claims); |
| 158 | + json_decref(out_claims); |
| 159 | +} |
| 160 | +END_TEST |
| 161 | + |
| 162 | +START_TEST(test_apache_authz_match_claim) |
| 163 | +{ |
| 164 | + json_error_t err; |
| 165 | + oauth2_apache_request_ctx_t *ctx = NULL; |
| 166 | + const char *s_claims = "{ \"sub\": \"joe\" }"; |
| 167 | + json_t *claims = NULL; |
| 168 | + bool rc = false; |
| 169 | + |
| 170 | + ctx = oauth2_apache_request_context(request, check_apache_log_request, |
| 171 | + "check_apache"); |
| 172 | + claims = json_loads(s_claims, 0, &err); |
| 173 | + |
| 174 | + rc = oauth2_apache_authz_match_claim(ctx, "sub:joe", claims); |
| 175 | + ck_assert_int_eq(rc, true); |
| 176 | + |
| 177 | + rc = oauth2_apache_authz_match_claim(ctx, "sub:hans", claims); |
| 178 | + ck_assert_int_eq(rc, false); |
| 179 | + |
| 180 | + json_decref(claims); |
| 181 | +} |
| 182 | +END_TEST |
| 183 | + |
| 184 | +START_TEST(test_apache_authorize) |
| 185 | +{ |
| 186 | + json_error_t err; |
| 187 | + oauth2_apache_request_ctx_t *ctx = NULL; |
| 188 | + const char *s_claims = "{ \"sub\": \"joe\" }"; |
| 189 | + json_t *claims = NULL; |
| 190 | + authz_status rc = AUTHZ_DENIED; |
| 191 | + |
| 192 | + ctx = oauth2_apache_request_context(request, check_apache_log_request, |
| 193 | + "check_apache"); |
| 194 | + claims = json_loads(s_claims, 0, &err); |
| 195 | + |
| 196 | + rc = oauth2_apache_authorize(ctx, claims, "sub:hans", |
| 197 | + oauth2_apache_authz_match_claim); |
| 198 | + ck_assert_int_eq(rc, AUTHZ_DENIED_NO_USER); |
| 199 | + |
| 200 | + request->user = "joe"; |
| 201 | + rc = oauth2_apache_authorize(ctx, claims, "sub:hans", |
| 202 | + oauth2_apache_authz_match_claim); |
| 203 | + ck_assert_int_eq(rc, AUTHZ_DENIED); |
| 204 | + |
| 205 | + rc = oauth2_apache_authorize(ctx, claims, "sub:joe", |
| 206 | + oauth2_apache_authz_match_claim); |
| 207 | + ck_assert_int_eq(rc, AUTHZ_GRANTED); |
| 208 | + |
| 209 | + json_decref(claims); |
| 210 | +} |
| 211 | +END_TEST |
| 212 | + |
| 213 | +Suite *oauth2_check_apache_suite() |
| 214 | +{ |
| 215 | + Suite *s = suite_create("apache"); |
| 216 | + TCase *c = tcase_create("core"); |
| 217 | + |
| 218 | + tcase_add_checked_fixture(c, setup, teardown); |
| 219 | + |
| 220 | + tcase_add_test(c, test_apache_request_state); |
| 221 | + tcase_add_test(c, test_apache_authz_match_claim); |
| 222 | + tcase_add_test(c, test_apache_authorize); |
| 223 | + |
| 224 | + suite_add_tcase(s, c); |
| 225 | + |
| 226 | + return s; |
| 227 | +} |
0 commit comments