2 * Copyright (c) 2019 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "token_validator_plugin.h"
39 const char *token_kind
;
41 const char * const *audiences
;
44 krb5_principal actual_principal
;
45 krb5_times token_times
;
48 static krb5_error_code KRB5_LIB_CALL
49 plcallback(krb5_context context
, const void *plug
, void *plugctx
, void *userctx
)
51 const krb5plugin_token_validator_ftable
*validator
= plug
;
53 struct plctx
*plctx
= userctx
;
55 ret
= validator
->validate(plugctx
, context
, plctx
->realm
,
56 plctx
->token_kind
, plctx
->token
,
57 plctx
->audiences
, plctx
->naudiences
,
58 &plctx
->result
, &plctx
->actual_principal
,
61 krb5_free_principal(context
, plctx
->actual_principal
);
62 plctx
->actual_principal
= NULL
;
67 static const char *plugin_deps
[] = { "krb5", NULL
};
69 static struct heim_plugin_data token_validator_data
= {
78 * Invoke a plugin to validate a JWT/SAML/OIDC token and partially-evaluate
81 KDC_LIB_FUNCTION krb5_error_code KDC_LIB_CALL
82 kdc_validate_token(krb5_context context
,
84 const char *token_kind
,
86 const char * const *audiences
,
88 krb5_principal
*actual_principal
,
89 krb5_times
*token_times
)
94 memset(&ctx
, 0, sizeof(ctx
));
96 ctx
.token_kind
= token_kind
;
98 ctx
.audiences
= audiences
;
99 ctx
.naudiences
= naudiences
;
101 ctx
.actual_principal
= NULL
;
103 krb5_clear_error_message(context
);
104 ret
= _krb5_plugin_run_f(context
, &token_validator_data
, 0, &ctx
,
106 if (ret
== 0 && ctx
.result
&& actual_principal
) {
107 *actual_principal
= ctx
.actual_principal
;
108 ctx
.actual_principal
= NULL
;
112 *token_times
= ctx
.token_times
;
114 krb5_free_principal(context
, ctx
.actual_principal
);
116 krb5_prepend_error_message(context
, ret
, "bearer token validation "
118 else if (!ctx
.result
)
119 krb5_set_error_message(context
, ret
= EACCES
,
120 "bearer token validation failed");