include/psaml/psaml_reg.h

filter: Statementstmt 96.1% 101 / 105 Branchbran 69.0% 58 / 84 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.1% 159 / 189 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/psaml/psaml_response.h ↑ Summary include/psaml/psaml_idp.h »
1 #ifndef PSAML_REG_H
2 #define PSAML_REG_H
3  
4 /* The registrar surface: how this plugin reaches Punk.
5 *
6 * Punk is reached by ordinary method dispatch, not through a C table.
7 * pk_abi.h is an observer interface with no install_kw, so there is
8 * nothing to consume from Punk through ExtUtils::Depends and the
9 * Makefile.PL does not load it. This is the same choice Punk-Feed and
10 * Punk-Challenge made, for the same reason.
11 *
12 * NO STRUCTS ON THE APP. Every piece of state this plugin keeps - the
13 * option hash, the providers, the ACS URL, the mount - is a key on the
14 * application HV, exactly as punk_sitemap.h and pfeed_*.h keep theirs.
15 * A struct hung off the app would need a free hook, would need to
16 * survive a fork, and would be invisible to anything that dumps the
17 * application; three problems bought for nothing.
18 *
19 * The only aggregates in this dist are the closure capture in
20 * psaml_clos.h and the per-document frx_doc *, which lives exactly as
21 * long as one verification. */
22  
23 #include "psaml_compat.h"
24 #include "psaml_clos.h"
25  
26 /* The keys this plugin owns on the application hash. Namespaced, because
27 * the application hash belongs to the application and every plugin is a
28 * guest on it. */
29 #define PSAML_K_OPTS "punk_saml.opts"
30 #define PSAML_K_IDPS "punk_saml.idps"
31 #define PSAML_K_IDPORDER "punk_saml.idp_order"
32 #define PSAML_K_LOGIN "punk_saml.login"
33 #define PSAML_K_ACS "punk_saml.acs"
34 #define PSAML_K_MOUNT "punk_saml.mount"
35 #define PSAML_K_ENTITY "punk_saml.entity_id"
36 #define PSAML_K_INSTALLED "punk_saml.installed"
37  
38 /* The application hash behind $app. Croaks rather than returning NULL:
39 * every caller is inside a keyword body, and a keyword body with no
40 * application is a bug in this dist, not a condition to handle. */
41 1370 PERL_STATIC_INLINE HV *psaml_app_hv(pTHX_ SV *app) {
42 1370 ▶ if (!app || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!app || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVHV) 1370 0 1370
if (!app || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVHV) 1370 0 1370
if (!app || !SvROK(app) || SvTYPE(SvRV(app)) != SVt_PVHV) 0 1370 1370
43 0 croak("Punk::Plugin::SAML: expected the application, got something else");
44 1370 return (HV *)SvRV(app);
45 }
46  
47 /* Fetch one of this plugin's keys, borrowed, or NULL. */
48 1067 PERL_STATIC_INLINE SV *psaml_app_get(pTHX_ SV *app, const char *key) {
49 1067 HV *hv = psaml_app_hv(aTHX_ app);
50 1067 SV **e = hv_fetch(hv, key, (I32)strlen(key), 0);
51 1067 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 865 202 1067
return (e && *e && SvOK(*e)) ? *e : NULL; 865 0 865
return (e && *e && SvOK(*e)) ? *e : NULL; 865 0 865
52 }
53  
54 /* Store one, taking ownership of val. */
55 276 PERL_STATIC_INLINE void psaml_app_set(pTHX_ SV *app, const char *key, SV *val) {
56 276 HV *hv = psaml_app_hv(aTHX_ app);
57 276 ▶ if (!hv_store(hv, key, (I32)strlen(key), val, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_store(hv, key, (I32)strlen(key), val, 0)) 0 276 276
58 0 SvREFCNT_dec(val);
59 276 }
60  
61 /* This plugin's key as a hashref, created on first use. Returns the RV,
62 * borrowed. */
63 39 PERL_STATIC_INLINE SV *psaml_app_hash(pTHX_ SV *app, const char *key) {
64 39 SV *v = psaml_app_get(aTHX_ app, key);
65 39 ▶ if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return v;
Branch: 3 branches
BranchTrueFalseTotal
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return v; 3 36 39
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return v; 3 0 3
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return v; 3 0 3
66 36 v = newRV_noinc((SV *)newHV());
67 36 psaml_app_set(aTHX_ app, key, v);
68 36 return v;
69 }
70  
71 /* The same for an arrayref. */
72 39 PERL_STATIC_INLINE SV *psaml_app_array(pTHX_ SV *app, const char *key) {
73 39 SV *v = psaml_app_get(aTHX_ app, key);
74 39 ▶ if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return v;
Branch: 3 branches
BranchTrueFalseTotal
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return v; 3 36 39
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return v; 3 0 3
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return v; 3 0 3
75 36 v = newRV_noinc((SV *)newAV());
76 36 psaml_app_set(aTHX_ app, key, v);
77 36 return v;
78 }
79  
80 /* Call a method on $app and return the single result as a mortal, or
81 * NULL when the call returned nothing.
82 *
83 * Returns +1 and NOT mortal: the caller owns it and frees it. Most
84 * callers here want exactly that, because they call for the side effect
85 * and drop the result with SvREFCNT_dec, and a mortal handed to
86 * SvREFCNT_dec is freed twice - once by the caller and once when the
87 * temps stack unwinds. A caller that wants a mortal says sv_2mortal at
88 * its own call site.
89 *
90 * The value is incremented BEFORE FREETMPS, not after: a value the callee
91 * mortalised is freed by that FREETMPS, so the reference has to be taken
92 * while the frame is still standing. This is the lifetime bug Crypt::JWS
93 * hit with a resolver-returned key. */
94 1047 PERL_STATIC_INLINE SV *psaml_call(pTHX_ SV *invocant, const char *method,
95 SV **args, int nargs) {
96 1047 dSP;
97 int count, i;
98 1047 SV *out = NULL;
99 1047 ENTER; SAVETMPS;
100 1047 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 1047 1047
101 1047 ▶ XPUSHs(invocant);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(invocant); 0 1047 1047
102 3126 ▶ for (i = 0; i < nargs; i++) XPUSHs(args[i]);
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) XPUSHs(args[i]); 0 2079 2079
for (i = 0; i < nargs; i++) XPUSHs(args[i]); 2079 1047 3126
103 1047 PUTBACK;
104 1047 count = call_method(method, G_SCALAR);
105 1047 SPAGAIN;
106 1047 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 1047 0 1047
107 1047 SV *r = POPs;
108 1047 ▶ if (SvOK(r)) out = SvREFCNT_inc_simple_NN(r);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(r)) out = SvREFCNT_inc_simple_NN(r); 931 116 1047
109 }
110 1047 PUTBACK;
111 1047 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 1047 0 1047
112 1047 return out; /* +1, caller owns; may be NULL */
113 }
114  
115 /* Call a coderef, the same shape as psaml_call above.
116 *
117 * on_login and on_error both go through this rather than each opening
118 * their own ENTER/PUSHMARK/call_sv inline. One shape, tested once: the
119 * inline version of this in the ACS handed the callback a glob instead
120 * of the context, and the failure surfaced as Punk trying to JSON-encode
121 * a GLOB reference three frames away from the cause. */
122 142 PERL_STATIC_INLINE SV *psaml_call_cv(pTHX_ SV *code, SV **args, int nargs) {
123 142 dSP;
124 int count, i;
125 142 SV *out = NULL;
126 142 ENTER; SAVETMPS;
127 142 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 142 142
128 535 ▶ for (i = 0; i < nargs; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef);
Branch: 3 branches
BranchTrueFalseTotal
for (i = 0; i < nargs; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); 0 393 393
for (i = 0; i < nargs; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); 393 0 393
for (i = 0; i < nargs; i++) XPUSHs(args[i] ? args[i] : &PL_sv_undef); 393 142 535
129 142 PUTBACK;
130 142 count = call_sv(code, G_SCALAR);
131 141 SPAGAIN;
132 141 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 141 0 141
133 141 SV *r = POPs;
134 141 ▶ if (SvOK(r)) out = SvREFCNT_inc_simple_NN(r);
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(r)) out = SvREFCNT_inc_simple_NN(r); 120 21 141
135 }
136 141 PUTBACK;
137 141 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 141 0 141
138 141 return out; /* +1, caller owns; may be NULL */
139 }
140  
141 /* The same, on the application object. */
142 #define psaml_app_call(app, method, args, n) \
143 psaml_call(aTHX_ (app), (method), (args), (n))
144  
145  
146 /* ---- the owner ------------------------------------------------------- */
147  
148 /* PSAML_WHO is in psaml_compat.h, where every header can see it. It is
149 * install_kw's third argument and the prefix of every croak: Punk makes a
150 * second install by the same owner a no-op, which is what lets `import`
151 * and `register` both ask and only the first do anything. */
152  
153 /* ---- calling into Punk ----------------------------------------------- */
154  
155 /* Handles both an object and a bare package name: `import` runs with the
156 * application's package before any object exists, and register runs with
157 * the object. */
158 163 PERL_STATIC_INLINE int psaml_can(pTHX_ SV *sv, const char *method) {
159 HV *stash;
160 163 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 163 0 163
if (!sv || !SvOK(sv)) return 0; 0 163 163
161 163 ▶ stash = SvROK(sv) ? SvSTASH(SvRV(sv)) : gv_stashsv(sv, 0);
Branch: 1 branch
BranchTrueFalseTotal
stash = SvROK(sv) ? SvSTASH(SvRV(sv)) : gv_stashsv(sv, 0); 113 50 163
162 163 ▶ return stash && gv_fetchmethod_autoload(stash, method, FALSE) != NULL;
Branch: 2 branches
BranchTrueFalseTotal
return stash && gv_fetchmethod_autoload(stash, method, FALSE) != NULL; 163 0 163
return stash && gv_fetchmethod_autoload(stash, method, FALSE) != NULL; 162 1 163
163 }
164  
165 /* ---- option validation ------------------------------------------------ */
166  
167 2 PERL_STATIC_INLINE SV *psaml_name_list(pTHX_ const char *const *known) {
168 2 SV *out = sv_2mortal(newSVpvs(""));
169 int i;
170 27 ▶ for (i = 0; known[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; known[i]; i++) { 25 2 27
171 25 ▶ if (i) sv_catpvs(out, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (i) sv_catpvs(out, ", "); 23 2 25
172 25 sv_catpv(out, known[i]);
173 }
174 2 return out;
175 }
176  
177 /* An unknown key croaks naming the noun, the key and what was available.
178 * The alternative is a typo that configures nothing and changes no
179 * behaviour, which is how `require_signd` becomes a site that checks
180 * nothing and nobody finds out until an audit. */
181 127 PERL_STATIC_INLINE void psaml_check_opts(pTHX_ const char *noun, HV *opts,
182 const char *const *known) {
183 HE *he;
184 127 ▶ if (!opts) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!opts) return; 0 127 127
185 127 hv_iterinit(opts);
186 329 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 204 125 329
187 STRLEN kl;
188 204 ▶ const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, kl); 0 204 204
189 204 int i, ok = 0;
190 608 ▶ for (i = 0; known[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; known[i]; i++) 606 2 608
191 606 ▶ if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; } 234 372 606
if (strlen(known[i]) == kl && memEQ(known[i], k, kl)) { ok = 1; break; } 202 32 234
192 204 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 2 202 204
193 4 croak("%s: unknown %s '%.*s' (known: %s)", PSAML_WHO, noun,
194 (int)kl, k, SvPV_nolen(psaml_name_list(aTHX_ known)));
195 }
196 }
197  
198 /* ---- installing ------------------------------------------------------- */
199  
200 178 PERL_STATIC_INLINE void psaml_keyword(pTHX_ SV *app, const char *name,
201 XSUBADDR_t body, AV *cap) {
202 SV *argv[3];
203 178 argv[0] = sv_2mortal(newSVpv(name, 0));
204 178 argv[1] = sv_2mortal(psaml_closure(aTHX_ body, cap));
205 178 argv[2] = sv_2mortal(newSVpvs(PSAML_WHO));
206 178 SvREFCNT_dec(psaml_call(aTHX_ app, "install_kw", argv, 3));
207 178 }
208  
209 80 PERL_STATIC_INLINE void psaml_helper(pTHX_ SV *app, const char *name,
210 XSUBADDR_t body, AV *cap) {
211 SV *argv[2];
212 80 argv[0] = sv_2mortal(newSVpv(name, 0));
213 80 argv[1] = sv_2mortal(psaml_closure(aTHX_ body, cap));
214 80 SvREFCNT_dec(psaml_call(aTHX_ app, "helper", argv, 2));
215 80 }
216  
217 /* A route. Written here and first CALLED in phase 8, which mounts the
218 * four; nothing in phase 4 mounts anything.
219 *
220 * `sitemap => 0` on all of them: Punk::Plugin::Sitemap lists every GET
221 * route with no capture and no guard, and /saml/metadata and
222 * /saml/login are both. A login URL in a sitemap is a crawler starting
223 * an authentication flow, on a schedule, for as long as the site
224 * exists. */
225 108 PERL_STATIC_INLINE void psaml_route(pTHX_ SV *app, const char *method,
226 SV *path, XSUBADDR_t body, AV *cap) {
227 SV *argv[5];
228 108 HV *o = newHV();
229 108 (void)hv_stores(o, "sitemap", newSViv(0));
230 108 argv[0] = sv_2mortal(newSVpv(method, 0));
231 108 argv[1] = path;
232 108 argv[2] = sv_2mortal(psaml_closure(aTHX_ body, cap));
233 108 argv[3] = &PL_sv_undef;
234 108 argv[4] = sv_2mortal(newRV_noinc((SV *)o));
235 108 SvREFCNT_dec(psaml_call(aTHX_ app, "route", argv, 5));
236 108 }
237  
238 /* The to_app seam. on_compile runs once, in registration order, after
239 * every keyword has recorded and before anything is compiled, which is
240 * the only moment at which `host` has certainly been declared and every
241 * saml_idp has certainly been seen. Both matter here: the ACS URL
242 * derives from `host`, and `saml_login` has to be able to complain that
243 * no provider was declared without caring which line came first.
244 *
245 * The `can` guard is kept although the floor is well past 0.30, so the
246 * failure is a croak naming the version rather than a method-not-found
247 * from inside a closure. */
248 40 PERL_STATIC_INLINE void psaml_at_compile(pTHX_ SV *app, XSUBADDR_t body,
249 AV *cap) {
250 SV *argv[2];
251 40 ▶ if (!psaml_can(aTHX_ app, "on_compile")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!psaml_can(aTHX_ app, "on_compile")) { 0 40 40
252 0 SvREFCNT_dec((SV *)cap);
253 0 croak("%s needs Punk 0.30 or newer for on_compile", PSAML_WHO);
254 }
255 40 argv[0] = sv_2mortal(psaml_closure(aTHX_ body, cap));
256 40 argv[1] = sv_2mortal(newSVpvs(PSAML_WHO));
257 40 SvREFCNT_dec(psaml_call(aTHX_ app, "on_compile", argv, 2));
258 40 }
259  
260 #endif /* PSAML_REG_H */
« include/psaml/psaml_response.h ↑ Summary include/psaml/psaml_idp.h »