include/pmcp/pmcp_auth.h

filter: Statementstmt 91.2% 220 / 241 Branchbran 67.5% 127 / 188 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.8% 347 / 429 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pmcp/pmcp_compat.h ↑ Summary xs/mcp.xs »
1 /* pmcp_auth.h - who may call: the `auth` guards, the `oauth` resource server,
2 * the RFC 9728 document, and `principal`.
3 *
4 * Needs: pmcp_http.h.
5 *
6 * Everything here fills the one guard list the POST, GET and DELETE routes
7 * share (07, 09, 10), so the three can never disagree about who is let in:
8 *
9 * auth any Punk guard, or a list: a code ref or a 'Controller#method'
10 * oauth a Punk::OAuth2::Checker guard over this resource, which answers
11 * 401 with the RFC 6750 challenge naming the metadata document, and
12 * 403 insufficient_scope. The claims land in stash->{auth}{oauth}
13 *
14 * The client's token is never sent anywhere. 12's re-entry carries it only to
15 * this application's own mount.
16 */
17  
18 #ifndef PMCP_AUTH_H
19 #define PMCP_AUTH_H
20  
21 #define PMCP_OAUTH_SINCE "0.08"
22 #define PMCP_PRM_PATH "/.well-known/oauth-protected-resource"
23  
24 static const char *const PMCP_OAUTH_KEYS[] = {
25 "issuer", "jwks_url", "key", "introspect", "scopes", "audience", "leeway",
26 "algs", "realm", NULL
27 };
28  
29 /* ---- the resource ---------------------------------------------------------------- */
30  
31 /* This endpoint's URL: the application's `host` and the plugin's path. The
32 * token's audience, so it has to be the exact URL clients use. Mortal. */
33 20 static SV *pmcp_resource_url(pTHX_ SV *app, HV *opts)
34 {
35 20 SV *host = sv_2mortal(pmcp_call_method(aTHX_ app, "host", NULL, 0, G_SCALAR, NULL));
36 STRLEN l;
37 const char *p;
38 SV *out;
39 20 ▶ if (!host || !SvOK(host)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!host || !SvOK(host)) return NULL; 20 0 20
if (!host || !SvOK(host)) return NULL; 1 19 20
40 19 p = SvPV_const(host, l);
41 19 ▶ if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 7 && memEQ(p, "http://", 7))))
Branch: 4 branches
BranchTrueFalseTotal
if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 7 && memEQ(p, "http://", 7)))) 19 0 19
if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 7 && memEQ(p, "http://", 7)))) 19 0 19
if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 7 && memEQ(p, "http://", 7)))) 19 0 19
if (!((l > 8 && memEQ(p, "https://", 8)) || (l > 7 && memEQ(p, "http://", 7)))) 0 19 19
42 0 return NULL;
43 19 ▶ while (l && p[l - 1] == '/') l--;
Branch: 2 branches
BranchTrueFalseTotal
while (l && p[l - 1] == '/') l--; 19 0 19
while (l && p[l - 1] == '/') l--; 0 19 19
44 19 out = sv_2mortal(newSVpvn(p, l));
45 19 sv_catsv(out, pmcp_hget(aTHX_ opts, "path"));
46 19 return out;
47 }
48  
49 /* The metadata document's URL, path-suffixed (RFC 9728 3.1). Mortal. */
50 8 static SV *pmcp_prm_url(pTHX_ SV *app, HV *opts)
51 {
52 8 SV *host = sv_2mortal(pmcp_call_method(aTHX_ app, "host", NULL, 0, G_SCALAR, NULL));
53 STRLEN l;
54 8 const char *p = SvPV_const(host, l);
55 SV *out;
56 8 ▶ while (l && p[l - 1] == '/') l--;
Branch: 2 branches
BranchTrueFalseTotal
while (l && p[l - 1] == '/') l--; 8 0 8
while (l && p[l - 1] == '/') l--; 0 8 8
57 8 out = sv_2mortal(newSVpvn(p, l));
58 8 sv_catpvs(out, PMCP_PRM_PATH);
59 8 sv_catsv(out, pmcp_hget(aTHX_ opts, "path"));
60 8 return out;
61 }
62  
63 /* ---- checkers ---------------------------------------------------------------------- */
64  
65 /* Any of several checkers. The checker contract is $checker->($credential,
66 * $c, $operationId, $scopes), the claims or false. cap = [checkers AV] */
67 3 XS_INTERNAL(pmcp_any_checker)
68 {
69 3 dXSARGS;
70 3 SV *list = pmcp_cap(aTHX_ cv, 0);
71 3 ▶ AV *av = pmcp_is_array(list) ? (AV *)SvRV(list) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *av = pmcp_is_array(list) ? (AV *)SvRV(list) : NULL; 3 0 3
72 3 ▶ SSize_t i, n = av ? av_len(av) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t i, n = av ? av_len(av) + 1 : 0; 3 0 3
73 3 SV *argv[4], *got = NULL;
74 int k;
75 15 ▶ for (k = 0; k < 4; k++) argv[k] = k < items ? ST(k) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < 4; k++) argv[k] = k < items ? ST(k) : &PL_sv_undef; 12 0 12
for (k = 0; k < 4; k++) argv[k] = k < items ? ST(k) : &PL_sv_undef; 12 3 15
76 8 ▶ for (i = 0; i < n && !got; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && !got; i++) { 6 2 8
for (i = 0; i < n && !got; i++) { 5 1 6
77 5 SV *err = NULL;
78 5 SV *r = pmcp_call_sv(aTHX_ *av_fetch(av, i, 0), argv, 4, G_SCALAR, &err);
79 5 ▶ if (!r) { SvREFCNT_dec(err); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { SvREFCNT_dec(err); continue; } 0 5 5
80 5 ▶ if (SvTRUE(r)) got = sv_2mortal(r);
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(r)) got = sv_2mortal(r); 2 3 5
81 3 else SvREFCNT_dec(r);
82 }
83 3 SP = PL_stack_base + ax - 1;
84 3 ▶ ST(0) = got ? got : &PL_sv_no;
Branch: 1 branch
BranchTrueFalseTotal
ST(0) = got ? got : &PL_sv_no; 2 1 3
85 3 XSRETURN(1);
86 }
87  
88 /* Introspection says nothing about audience unless the server sends `aud`,
89 * and a token not issued for this resource must be refused (RFC 8707, the MCP
90 * authorization rules). So the claims must carry one of the audiences.
91 * cap = [checker, audiences AV] */
92 5 XS_INTERNAL(pmcp_aud_checker)
93 {
94 5 dXSARGS;
95 5 SV *checker = pmcp_cap(aTHX_ cv, 0), *auds = pmcp_cap(aTHX_ cv, 1);
96 5 SV *argv[4], *r, *err = NULL, *ok = &PL_sv_no;
97 int k;
98 25 ▶ for (k = 0; k < 4; k++) argv[k] = k < items ? ST(k) : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < 4; k++) argv[k] = k < items ? ST(k) : &PL_sv_undef; 5 15 20
for (k = 0; k < 4; k++) argv[k] = k < items ? ST(k) : &PL_sv_undef; 20 5 25
99 5 r = pmcp_call_sv(aTHX_ checker, argv, 4, G_SCALAR, &err);
100 5 ▶ if (!r) SvREFCNT_dec(err);
Branch: 1 branch
BranchTrueFalseTotal
if (!r) SvREFCNT_dec(err); 0 5 5
101 else {
102 5 sv_2mortal(r);
103 5 ▶ if (pmcp_is_hash(r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(r)) { 4 1 5
104 4 SV *aud = pmcp_hget(aTHX_ (HV *)SvRV(r), "aud");
105 4 AV *want = (AV *)SvRV(auds);
106 4 SSize_t i, j, n = av_len(want) + 1;
107 8 ▶ for (i = 0; i < n && ok == &PL_sv_no; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && ok == &PL_sv_no; i++) { 4 4 8
for (i = 0; i < n && ok == &PL_sv_no; i++) { 4 0 4
108 4 SV *w = *av_fetch(want, i, 0);
109 5 ▶ if (aud && pmcp_is_array(aud)) {
Branch: 2 branches
BranchTrueFalseTotal
if (aud && pmcp_is_array(aud)) { 3 1 4
if (aud && pmcp_is_array(aud)) { 1 2 3
110 1 AV *have = (AV *)SvRV(aud);
111 2 ▶ for (j = 0; j <= av_len(have); j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j <= av_len(have); j++) { 2 0 2
112 2 SV **h = av_fetch(have, j, 0);
113 2 ▶ if (h && sv_eq(*h, w)) { ok = r; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (h && sv_eq(*h, w)) { ok = r; break; } 2 0 2
if (h && sv_eq(*h, w)) { ok = r; break; } 1 1 2
114 }
115 }
116 3 ▶ else if (aud && sv_eq(aud, w)) ok = r;
Branch: 2 branches
BranchTrueFalseTotal
else if (aud && sv_eq(aud, w)) ok = r; 2 1 3
else if (aud && sv_eq(aud, w)) ok = r; 1 1 2
117 }
118 }
119 }
120 5 SP = PL_stack_base + ax - 1;
121 5 ST(0) = ok;
122 5 XSRETURN(1);
123 }
124  
125 /* A 401 that names no scope gets the ones this endpoint needs, so a client
126 * asks for them up front rather than learning them from a 403 (2025-11-25).
127 * cap = [guard, scope string] */
128 21 XS_INTERNAL(pmcp_scoped_guard)
129 {
130 21 dXSARGS;
131 21 SV *guard = pmcp_cap(aTHX_ cv, 0), *scope = pmcp_cap(aTHX_ cv, 1);
132 21 ▶ SV *c = items > 0 ? ST(0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *c = items > 0 ? ST(0) : &PL_sv_undef; 21 0 21
133 21 SV *err = NULL;
134 21 SV *r = pmcp_call_sv(aTHX_ guard, &c, 1, G_SCALAR, &err);
135 21 ▶ if (!r) pmcp_croak_sv(aTHX_ sv_2mortal(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!r) pmcp_croak_sv(aTHX_ sv_2mortal(err)); 0 21 21
136 21 ▶ if (scope && SvCUR(scope) && pmcp_is_array(r) && av_len((AV *)SvRV(r)) >= 1) {
Branch: 4 branches
BranchTrueFalseTotal
if (scope && SvCUR(scope) && pmcp_is_array(r) && av_len((AV *)SvRV(r)) >= 1) { 21 0 21
if (scope && SvCUR(scope) && pmcp_is_array(r) && av_len((AV *)SvRV(r)) >= 1) { 21 0 21
if (scope && SvCUR(scope) && pmcp_is_array(r) && av_len((AV *)SvRV(r)) >= 1) { 12 9 21
if (scope && SvCUR(scope) && pmcp_is_array(r) && av_len((AV *)SvRV(r)) >= 1) { 12 0 12
137 12 AV *t = (AV *)SvRV(r);
138 12 SV **st = av_fetch(t, 0, 0), **hp = av_fetch(t, 1, 0);
139 12 ▶ if (st && SvIV(*st) == 401 && hp && pmcp_is_array(*hp)) {
Branch: 4 branches
BranchTrueFalseTotal
if (st && SvIV(*st) == 401 && hp && pmcp_is_array(*hp)) { 12 0 12
if (st && SvIV(*st) == 401 && hp && pmcp_is_array(*hp)) { 10 2 12
if (st && SvIV(*st) == 401 && hp && pmcp_is_array(*hp)) { 10 0 10
if (st && SvIV(*st) == 401 && hp && pmcp_is_array(*hp)) { 10 0 10
140 10 AV *h = (AV *)SvRV(*hp);
141 SSize_t i;
142 30 ▶ for (i = 0; i + 1 <= av_len(h); i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 <= av_len(h); i += 2) { 20 10 30
143 20 SV *k = *av_fetch(h, i, 0), *v = *av_fetch(h, i + 1, 0);
144 20 ▶ if (strcasecmp(SvPV_nolen(k), "WWW-Authenticate") == 0
Branch: 1 branch
BranchTrueFalseTotal
if (strcasecmp(SvPV_nolen(k), "WWW-Authenticate") == 0 10 10 20
145 10 ▶ && !instr(SvPV_nolen(v), "scope=")) {
Branch: 1 branch
BranchTrueFalseTotal
&& !instr(SvPV_nolen(v), "scope=")) { 10 0 10
146 10 SV *nv = newSVsv(v);
147 10 sv_catpvf(nv, ", scope=\"%" SVf "\"", SVfARG(scope));
148 10 (void)av_store(h, i + 1, nv);
149 }
150 }
151 }
152 }
153 21 SP = PL_stack_base + ax - 1;
154 21 ST(0) = sv_2mortal(r);
155 21 XSRETURN(1);
156 }
157  
158 24 static SV *pmcp_list_or_one(pTHX_ SV *v)
159 {
160 24 AV *av = newAV();
161 24 ▶ if (pmcp_is_array(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_array(v)) { 17 7 24
162 17 SSize_t i, n = av_len((AV *)SvRV(v)) + 1;
163 37 ▶ for (i = 0; i < n; i++) av_push(av, newSVsv(*av_fetch((AV *)SvRV(v), i, 0)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(av, newSVsv(*av_fetch((AV *)SvRV(v), i, 0))); 20 17 37
164 }
165 7 ▶ else if (v && SvOK(v)) av_push(av, newSVsv(v));
Branch: 2 branches
BranchTrueFalseTotal
else if (v && SvOK(v)) av_push(av, newSVsv(v)); 7 0 7
else if (v && SvOK(v)) av_push(av, newSVsv(v)); 7 0 7
166 24 return sv_2mortal(newRV_noinc((SV *)av));
167 }
168  
169 /* The oauth guard, built at on_compile. Croaks naming what is wrong. +1 */
170 13 static SV *pmcp_oauth_guard(pTHX_ SV *app, HV *opts, HV *o)
171 {
172 13 SV *err = NULL, *v, *resource, *auds, *checker, *scopes, *guard, *argv[12], *r;
173 13 SV *checker_class = sv_2mortal(newSVpvs("Punk::OAuth2::Checker"));
174 13 int kinds = 0;
175 AV *list;
176  
177 13 pmcp_check_keys(aTHX_ "oauth option", o, PMCP_OAUTH_KEYS);
178 12 ▶ if (!pmcp_require(aTHX_ "Punk::OAuth2::Checker", &err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_require(aTHX_ "Punk::OAuth2::Checker", &err)) { 0 12 12
179 0 sv_2mortal(err);
180 0 croak("%s: oauth needs Punk-OAuth2 %s or later, and it does not load: %" SVf,
181 PMCP_WHO, PMCP_OAUTH_SINCE, SVfARG(err));
182 }
183 {
184 12 SV *ver = get_sv("Punk::OAuth2::VERSION", 0);
185 12 ▶ if (!ver || !SvOK(ver) || SvNV(ver) < 0.08)
Branch: 3 branches
BranchTrueFalseTotal
if (!ver || !SvOK(ver) || SvNV(ver) < 0.08) 12 0 12
if (!ver || !SvOK(ver) || SvNV(ver) < 0.08) 12 0 12
if (!ver || !SvOK(ver) || SvNV(ver) < 0.08) 0 12 12
186 0 ▶ croak("%s: oauth needs Punk-OAuth2 %s or later, and found %s", PMCP_WHO,
Branch: 2 branches
BranchTrueFalseTotal
croak("%s: oauth needs Punk-OAuth2 %s or later, and found %s", PMCP_WHO, 0 0 0
croak("%s: oauth needs Punk-OAuth2 %s or later, and found %s", PMCP_WHO, 0 0 0
187 PMCP_OAUTH_SINCE, ver && SvOK(ver) ? SvPV_nolen(ver) : "none");
188 }
189 12 resource = pmcp_resource_url(aTHX_ app, opts);
190 12 ▶ if (!resource)
Branch: 1 branch
BranchTrueFalseTotal
if (!resource) 1 11 12
191 1 croak("%s: oauth needs the application's `host` (https://...): the token's "
192 "audience is this endpoint's URL, and the metadata document names it",
193 PMCP_WHO);
194 11 ▶ if (!pmcp_is_text(pmcp_hget(aTHX_ o, "issuer")))
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_text(pmcp_hget(aTHX_ o, "issuer"))) 1 10 11
195 1 croak("%s: oauth needs `issuer`, the authorization server", PMCP_WHO);
196 10 kinds = !!pmcp_hget(aTHX_ o, "jwks_url") + !!pmcp_hget(aTHX_ o, "key")
197 10 + !!pmcp_hget(aTHX_ o, "introspect");
198 10 ▶ if (kinds != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (kinds != 1) 2 8 10
199 2 croak("%s: oauth needs exactly one of `jwks_url`, `key` or `introspect`",
200 PMCP_WHO);
201  
202 8 v = pmcp_hget(aTHX_ o, "audience");
203 8 ▶ auds = v ? pmcp_list_or_one(aTHX_ v) : pmcp_list_or_one(aTHX_ resource);
Branch: 1 branch
BranchTrueFalseTotal
auds = v ? pmcp_list_or_one(aTHX_ v) : pmcp_list_or_one(aTHX_ resource); 1 7 8
204 8 ▶ if (av_len((AV *)SvRV(auds)) < 0) croak("%s: oauth `audience` is empty", PMCP_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (av_len((AV *)SvRV(auds)) < 0) croak("%s: oauth `audience` is empty", PMCP_WHO); 0 8 8
205 8 scopes = pmcp_list_or_one(aTHX_ pmcp_hget(aTHX_ o, "scopes"));
206  
207 8 list = newAV();
208 8 ▶ if ((v = pmcp_hget(aTHX_ o, "introspect"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ o, "introspect"))) { 0 8 8
209 HV *ih;
210 HE *he;
211 0 int n = 0;
212 AV *cap;
213 0 ▶ if (!pmcp_is_hash(v)) croak("%s: oauth `introspect` must be a hash", PMCP_WHO);
Branch: 1 branch
BranchTrueFalseTotal
if (!pmcp_is_hash(v)) croak("%s: oauth `introspect` must be a hash", PMCP_WHO); 0 0 0
214 0 ih = (HV *)SvRV(v);
215 0 hv_iterinit(ih);
216 0 ▶ while ((he = hv_iternext(ih)) && n < 8) {
Branch: 2 branches
BranchTrueFalseTotal
while ((he = hv_iternext(ih)) && n < 8) { 0 0 0
while ((he = hv_iternext(ih)) && n < 8) { 0 0 0
217 0 argv[n++] = sv_2mortal(newSVsv(hv_iterkeysv(he)));
218 0 argv[n++] = HeVAL(he);
219 }
220 0 r = pmcp_call_method(aTHX_ checker_class, "introspect", argv, n, G_SCALAR, &err);
221 0 ▶ if (!r) { SvREFCNT_dec((SV *)list); pmcp_croak_sv(aTHX_ sv_2mortal(err)); }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { SvREFCNT_dec((SV *)list); pmcp_croak_sv(aTHX_ sv_2mortal(err)); } 0 0 0
222 0 cap = newAV();
223 0 av_push(cap, r);
224 0 av_push(cap, newSVsv(auds));
225 0 av_push(list, pmcp_closure(aTHX_ pmcp_aud_checker, cap));
226 }
227 else {
228 8 SSize_t i, na = av_len((AV *)SvRV(auds)) + 1;
229 17 ▶ for (i = 0; i < na; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < na; i++) { 9 8 17
230 9 int n = 0;
231 9 argv[n++] = sv_2mortal(newSVpvs("issuer"));
232 9 argv[n++] = pmcp_hget(aTHX_ o, "issuer");
233 9 argv[n++] = sv_2mortal(newSVpvs("audience"));
234 9 argv[n++] = *av_fetch((AV *)SvRV(auds), i, 0);
235 9 ▶ if ((v = pmcp_hget(aTHX_ o, "jwks_url"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ o, "jwks_url"))) { 0 9 9
236 0 argv[n++] = sv_2mortal(newSVpvs("jwks_url"));
237 0 argv[n++] = v;
238 }
239 else {
240 9 argv[n++] = sv_2mortal(newSVpvs("key"));
241 9 argv[n++] = pmcp_hget(aTHX_ o, "key");
242 }
243 9 ▶ if ((v = pmcp_hget(aTHX_ o, "algs"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ o, "algs"))) { 9 0 9
244 9 argv[n++] = sv_2mortal(newSVpvs("algs"));
245 9 argv[n++] = v;
246 }
247 9 ▶ if ((v = pmcp_hget(aTHX_ o, "leeway"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ o, "leeway"))) { 0 9 9
248 0 argv[n++] = sv_2mortal(newSVpvs("leeway"));
249 0 argv[n++] = v;
250 }
251 9 r = pmcp_call_method(aTHX_ checker_class, "jwt", argv, n, G_SCALAR, &err);
252 9 ▶ if (!r) { SvREFCNT_dec((SV *)list); pmcp_croak_sv(aTHX_ sv_2mortal(err)); }
Branch: 1 branch
BranchTrueFalseTotal
if (!r) { SvREFCNT_dec((SV *)list); pmcp_croak_sv(aTHX_ sv_2mortal(err)); } 0 9 9
253 9 av_push(list, r);
254 }
255 }
256 8 ▶ if (av_len(list) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(list) == 0) { 7 1 8
257 7 checker = sv_2mortal(newSVsv(*av_fetch(list, 0, 0)));
258 7 SvREFCNT_dec((SV *)list);
259 }
260 else {
261 1 AV *cap = newAV();
262 1 av_push(cap, newRV_noinc((SV *)list));
263 1 checker = sv_2mortal(pmcp_closure(aTHX_ pmcp_any_checker, cap));
264 }
265  
266 {
267 8 int n = 0;
268 8 argv[n++] = checker;
269 8 argv[n++] = sv_2mortal(newSVpvs("scopes"));
270 8 argv[n++] = scopes;
271 8 argv[n++] = sv_2mortal(newSVpvs("resource_metadata"));
272 8 argv[n++] = pmcp_prm_url(aTHX_ app, opts);
273 8 argv[n++] = sv_2mortal(newSVpvs("realm"));
274 8 ▶ argv[n++] = (v = pmcp_hget(aTHX_ o, "realm")) ? v : sv_2mortal(newSVpvs("mcp"));
Branch: 1 branch
BranchTrueFalseTotal
argv[n++] = (v = pmcp_hget(aTHX_ o, "realm")) ? v : sv_2mortal(newSVpvs("mcp")); 8 0 8
275 8 guard = pmcp_call_method(aTHX_ checker_class, "guard", argv, n, G_SCALAR, &err);
276 8 ▶ if (!guard) pmcp_croak_sv(aTHX_ sv_2mortal(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!guard) pmcp_croak_sv(aTHX_ sv_2mortal(err)); 0 8 8
277 }
278 {
279 8 AV *cap = newAV();
280 8 SV *joined = newSVpvs("");
281 8 SSize_t i, ns = av_len((AV *)SvRV(scopes)) + 1;
282 17 ▶ for (i = 0; i < ns; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ns; i++) { 9 8 17
283 9 ▶ if (i) sv_catpvs(joined, " ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(joined, " "); 1 8 9
284 9 sv_catsv(joined, *av_fetch((AV *)SvRV(scopes), i, 0));
285 }
286 8 av_push(cap, guard);
287 8 av_push(cap, joined);
288 8 return pmcp_closure(aTHX_ pmcp_scoped_guard, cap);
289 }
290 }
291  
292 /* ---- the guard list ---------------------------------------------------------------- */
293  
294 /* `auth` then `oauth`, resolved: the list the routes share. +1 AV ref. */
295 112 static SV *pmcp_auth_guards(pTHX_ SV *app, HV *opts)
296 {
297 112 AV *out = newAV();
298 112 SV *a = pmcp_hget(aTHX_ opts, "auth"), *o = pmcp_hget(aTHX_ opts, "oauth");
299 112 ▶ if (a) {
Branch: 1 branch
BranchTrueFalseTotal
if (a) { 11 101 112
300 11 AV *g = pmcp_resolve_guards(aTHX_ app, "endpoint", sv_2mortal(newSVpvs("auth")), a);
301 11 ▶ if (g) {
Branch: 1 branch
BranchTrueFalseTotal
if (g) { 11 0 11
302 11 SSize_t i, n = av_len(g) + 1;
303 23 ▶ for (i = 0; i < n; i++) av_push(out, newSVsv(*av_fetch(g, i, 0)));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) av_push(out, newSVsv(*av_fetch(g, i, 0))); 12 11 23
304 11 SvREFCNT_dec((SV *)g);
305 }
306 }
307 112 ▶ if (o) av_push(out, pmcp_oauth_guard(aTHX_ app, opts, (HV *)SvRV(o)));
Branch: 1 branch
BranchTrueFalseTotal
if (o) av_push(out, pmcp_oauth_guard(aTHX_ app, opts, (HV *)SvRV(o))); 13 99 112
308 107 return newRV_noinc((SV *)out);
309 }
310  
311 /* A guard's refusal, made readable by a browser client: its challenge header
312 * exposed when the request came with an Origin. */
313 21 static void pmcp_expose_refusal(pTHX_ SV *out, int cors)
314 {
315 21 ▶ if (!cors || !pmcp_is_array(out) || av_len((AV *)SvRV(out)) < 1) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!cors || !pmcp_is_array(out) || av_len((AV *)SvRV(out)) < 1) return; 1 20 21
if (!cors || !pmcp_is_array(out) || av_len((AV *)SvRV(out)) < 1) return; 1 0 1
if (!cors || !pmcp_is_array(out) || av_len((AV *)SvRV(out)) < 1) return; 0 1 1
316 {
317 1 SV **hp = av_fetch((AV *)SvRV(out), 1, 0);
318 1 ▶ if (hp && pmcp_is_array(*hp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (hp && pmcp_is_array(*hp)) { 1 0 1
if (hp && pmcp_is_array(*hp)) { 1 0 1
319 1 av_push((AV *)SvRV(*hp), newSVpvs("Access-Control-Expose-Headers"));
320 1 av_push((AV *)SvRV(*hp), newSVpvs(PMCP_EXPOSE));
321 }
322 }
323 }
324  
325 /* ---- principal ------------------------------------------------------------------------ */
326  
327 /* `principal`, once a request is let in: what it returns is $c->mcp_user, and
328 * through the stash copy it reaches every tool call. A die is a 500, as a die
329 * in a guard is. */
330 17 static void pmcp_principal_run(pTHX_ SV *c, SV *principal)
331 {
332 SV *u, *stash;
333 17 ▶ if (!principal) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!principal) return; 0 17 17
334 17 u = pmcp_call_sv(aTHX_ principal, &c, 1, G_SCALAR, NULL);
335 17 sv_2mortal(u);
336 17 stash = sv_2mortal(pmcp_call_method(aTHX_ c, "stash", NULL, 0, G_SCALAR, NULL));
337 17 ▶ if (pmcp_is_hash(stash)) (void)hv_stores((HV *)SvRV(stash), "mcp.user", newSVsv(u));
Branch: 1 branch
BranchTrueFalseTotal
if (pmcp_is_hash(stash)) (void)hv_stores((HV *)SvRV(stash), "mcp.user", newSVsv(u)); 17 0 17
338 }
339  
340 /* ---- the RFC 9728 document -------------------------------------------------------------- */
341  
342 /* GET on both well-known paths. Public by design, so any origin may read it.
343 * cap = [the frozen bytes] */
344 5 XS_INTERNAL(pmcp_prm_cb)
345 {
346 5 dXSARGS;
347 5 SV *bytes = pmcp_cap(aTHX_ cv, 0);
348 5 AV *t = newAV(), *h = newAV(), *b = newAV();
349 PERL_UNUSED_VAR(items);
350 5 av_push(h, newSVpvs("Content-Type"));
351 5 av_push(h, newSVpvs("application/json"));
352 5 av_push(h, newSVpvs("Cache-Control"));
353 5 av_push(h, newSVpvs("public, max-age=3600"));
354 5 av_push(h, newSVpvs("Access-Control-Allow-Origin"));
355 5 av_push(h, newSVpvs("*"));
356 5 av_push(b, newSVsv(bytes));
357 5 av_push(t, newSViv(200));
358 5 av_push(t, newRV_noinc((SV *)h));
359 5 av_push(t, newRV_noinc((SV *)b));
360 5 SP = PL_stack_base + ax - 1;
361 5 ST(0) = sv_2mortal(newRV_noinc((SV *)t));
362 5 XSRETURN(1);
363 }
364  
365 107 static void pmcp_prm_install(pTHX_ SV *app, HV *opts)
366 {
367 107 HV *o = pmcp_is_hash(pmcp_hget(aTHX_ opts, "oauth"))
368 107 ▶ ? (HV *)SvRV(pmcp_hget(aTHX_ opts, "oauth")) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(pmcp_hget(aTHX_ opts, "oauth")) : NULL; 8 99 107
369 HV *doc;
370 107 SV *v, *bytes, *err = NULL;
371 AV *as;
372 int k;
373 107 ▶ if (!o) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!o) return; 99 8 107
374 8 doc = newHV();
375 8 (void)hv_stores(doc, "resource", newSVsv(pmcp_resource_url(aTHX_ app, opts)));
376 8 as = newAV();
377 8 av_push(as, newSVsv(pmcp_hget(aTHX_ o, "issuer")));
378 8 (void)hv_stores(doc, "authorization_servers", newRV_noinc((SV *)as));
379 8 (void)hv_stores(doc, "scopes_supported",
380 newSVsv(pmcp_list_or_one(aTHX_ pmcp_hget(aTHX_ o, "scopes"))));
381 {
382 8 AV *bm = newAV();
383 8 av_push(bm, newSVpvs("header"));
384 8 (void)hv_stores(doc, "bearer_methods_supported", newRV_noinc((SV *)bm));
385 }
386 8 ▶ if ((v = pmcp_hget(aTHX_ opts, "title"))) (void)hv_stores(doc, "resource_name", pmcp_str(aTHX_ v));
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ opts, "title"))) (void)hv_stores(doc, "resource_name", pmcp_str(aTHX_ v)); 1 7 8
387 8 ▶ if ((v = pmcp_hget(aTHX_ opts, "website")))
Branch: 1 branch
BranchTrueFalseTotal
if ((v = pmcp_hget(aTHX_ opts, "website"))) 1 7 8
388 1 (void)hv_stores(doc, "resource_documentation", pmcp_str(aTHX_ v));
389 8 bytes = pmcp_json_trapped(aTHX_ sv_2mortal(newRV_noinc((SV *)doc)), &err);
390 8 ▶ if (!bytes) pmcp_croak_sv(aTHX_ sv_2mortal(err));
Branch: 1 branch
BranchTrueFalseTotal
if (!bytes) pmcp_croak_sv(aTHX_ sv_2mortal(err)); 0 8 8
391 8 sv_2mortal(bytes);
392 24 ▶ for (k = 0; k < 2; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < 2; k++) { 16 8 24
393 16 AV *cap = newAV();
394 SV *argv[3], *r;
395 16 SV *path = sv_2mortal(newSVpvs(PMCP_PRM_PATH));
396 16 ▶ if (k) sv_catsv(path, pmcp_hget(aTHX_ opts, "path"));
Branch: 1 branch
BranchTrueFalseTotal
if (k) sv_catsv(path, pmcp_hget(aTHX_ opts, "path")); 8 8 16
397 16 av_push(cap, newSVsv(bytes));
398 16 argv[0] = sv_2mortal(newSVpvs("GET"));
399 16 argv[1] = path;
400 16 argv[2] = sv_2mortal(pmcp_closure(aTHX_ pmcp_prm_cb, cap));
401 16 r = pmcp_call_method(aTHX_ app, "route", argv, 3, G_SCALAR, NULL);
402 16 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 16 0 16
403 }
404 }
405  
406 #endif /* PMCP_AUTH_H */
« include/pmcp/pmcp_compat.h ↑ Summary xs/mcp.xs »