include/punk/punk_auth.h

filter: Statementstmt 96.4% 266 / 278 (2 uncoverable) Branchbran 66.5% 255 / 406 (32 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 78.6% 521 / 684 (34 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_compile.h ↑ Summary include/punk/punk_ratelimit.h »
1 /* punk_auth.h - the authentication battery's request path, in C.
2 *
3 * The `auth` keyword freezes a config on the app; auth_guard() returns a
4 * guard coderef for `under`. The guard's common case - "is anyone signed
5 * in" - runs entirely here: one session load, one hash fetch. Role and
6 * verified checks need the user row, so they fall through to
7 * Punk::Auth::_guard_check, which loads it through the model (and the
8 * await seam, so both database backends work).
9 *
10 * Denial negotiates: a client whose Accept asks for text/html is redirected
11 * to the login page with ?to=<path> (relative return-to); anything else gets
12 * a 401 in the house error shape. on_denied overrides with '403', '404' (the
13 * queue_admin do-not-confirm-it-exists option) or a coderef.
14 *
15 * Include after punk_session.h (ps_load, ps_cfg_str, ps_stash), punk_csrf.h,
16 * punk_accept.h (the Accept parser) and punk_static.h (punk_closure).
17 */
18  
19 #ifndef PUNK_AUTH_H
20 #define PUNK_AUTH_H
21  
22 /* the app's frozen auth config, from a context - the pcf_cfg shape */
23 92 static HV *pauth_cfg(pTHX_ SV *c) {
24 92 SV *app = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_APP);
25 SV **s;
26 /*
27 # uncoverable branch true
28 # uncoverable condition left count:1 class:ignore_covered_err
29 # uncoverable condition left count:2 class:ignore_covered_err
30 # uncoverable condition left count:3 class:ignore_covered_err */
31 92 ▶ if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 92 0 92
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 92 0 92
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; 0 92 92
32 92 s = hv_fetchs((HV *)SvRV(app), K_AUTH, 0);
33 86 ▶ return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 86 0 86
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 86 0 86
return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 86 0 86
34 178 ▶ ? (HV *)SvRV(*s) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(*s) : NULL; 86 6 92
35 }
36  
37 /* Is this a user id? Defined, not empty and not "0": an id a backend handed
38 * back empty must not read as somebody being signed in. */
39 117 static int pauth_id_ok(pTHX_ SV *id) {
40 STRLEN l;
41 const char *p;
42 /*
43 # uncoverable branch true
44 # uncoverable condition left count:1 class:ignore_covered_err
45 # uncoverable condition left count:2 class:ignore_covered_err */
46 117 ▶ if (!(id && SvOK(id))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(id && SvOK(id))) return 0; 117 0 117
if (!(id && SvOK(id))) return 0; 0 117 117
47 117 p = SvPV_const(id, l);
48 117 ▶ return l > 0 && !(l == 1 && *p == '0');
Branch: 3 branches
BranchTrueFalseTotal
return l > 0 && !(l == 1 && *p == '0'); 113 4 117
return l > 0 && !(l == 1 && *p == '0'); 96 17 113
return l > 0 && !(l == 1 && *p == '0'); 93 3 96
49 }
50  
51 /* A `verified` column is true unless it is false to Perl or spells false the
52 * way a database driver does: PostgreSQL's 'f' is a true string. */
53 12 static int pauth_truthy(pTHX_ SV *v) {
54 STRLEN l;
55 const char *p;
56 /*
57 # uncoverable branch true
58 # uncoverable condition left count:1 class:ignore_covered_err
59 # uncoverable condition left count:2 class:ignore_covered_err */
60 12 ▶ if (!(v && SvOK(v))) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!(v && SvOK(v))) return 0; 12 0 12
if (!(v && SvOK(v))) return 0; 0 12 12
61 /*
62 # uncoverable branch true
63 # uncoverable condition left count:1 class:ignore_covered_err */
64 12 ▶ if (SvROK(v)) return SvTRUE(v) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) return SvTRUE(v) ? 1 : 0; 0 12 12
65 12 p = SvPV_const(v, l);
66 12 ▶ if (!l || (l == 1 && *p == '0')) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!l || (l == 1 && *p == '0')) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 12 0 12
if (!l || (l == 1 && *p == '0')) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 6 6 12
if (!l || (l == 1 && *p == '0')) return 0; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 5 6
67 11 ▶ if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; /* # uncoverable condition left count:5 class:ignore_covered_err */
Branch: 5 branches
BranchTrueFalseTotal
if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; /* # uncoverable condition left count:5 class:ignore_covered_err */ 5 6 11
if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; /* # uncoverable condition left count:5 class:ignore_covered_err */ 4 1 5
if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; /* # uncoverable condition left count:5 class:ignore_covered_err */ 3 1 4
if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; /* # uncoverable condition left count:5 class:ignore_covered_err */ 2 1 3
if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; /* # uncoverable condition left count:5 class:ignore_covered_err */ 0 2 2
68 8 ▶ if (l == 5 && foldEQ(p, "false", 5)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (l == 5 && foldEQ(p, "false", 5)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 6 8
if (l == 5 && foldEQ(p, "false", 5)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
69 6 ▶ if (l == 2 && foldEQ(p, "no", 2)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (l == 2 && foldEQ(p, "no", 2)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 5 6
if (l == 2 && foldEQ(p, "no", 2)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
70 5 ▶ if (l == 3 && foldEQ(p, "off", 3)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (l == 3 && foldEQ(p, "off", 3)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 4 5
if (l == 3 && foldEQ(p, "off", 3)) return 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
71 4 return SvTRUE(v) ? 1 : 0;
72 }
73  
74 /* the signed-in user id from the session, or NULL. Returns +1. */
75 98 static SV *pauth_session_id(pTHX_ SV *c, HV *cfg) {
76 STRLEN kl;
77 98 const char *key = ps_cfg_str(aTHX_ cfg, "session_key", "user_id", &kl);
78 98 SV *sess = ps_load(aTHX_ c);
79 98 SV *out = NULL;
80 /*
81 # uncoverable branch false
82 # uncoverable condition right count:1 class:ignore_covered_err
83 # uncoverable condition right count:2 class:ignore_covered_err
84 # uncoverable condition right count:3 class:ignore_covered_err */
85 98 ▶ if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { 98 0 98
if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { 98 0 98
if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { 98 0 98
86 98 SV **e = hv_fetch((HV *)SvRV(sess), key, (I32)kl, 0);
87 98 ▶ if (e && *e && pauth_id_ok(aTHX_ *e)) out = newSVsv(*e); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && pauth_id_ok(aTHX_ *e)) out = newSVsv(*e); /* # uncoverable condition right count:2 class:ignore_covered_err */ 81 17 98
if (e && *e && pauth_id_ok(aTHX_ *e)) out = newSVsv(*e); /* # uncoverable condition right count:2 class:ignore_covered_err */ 81 0 81
if (e && *e && pauth_id_ok(aTHX_ *e)) out = newSVsv(*e); /* # uncoverable condition right count:2 class:ignore_covered_err */ 77 4 81
88 }
89 /*
90 # uncoverable branch false
91 # uncoverable condition right count:1 class:ignore_covered_err */
92 98 ▶ if (sess) SvREFCNT_dec(sess);
Branch: 1 branch
BranchTrueFalseTotal
if (sess) SvREFCNT_dec(sess); 98 0 98
93 98 return out;
94 }
95  
96 /* percent-encode a path for the ?to= value - unreserved chars and '/' pass.
97 * The encoder itself is punk_url.h's, which url_for uses for a *splat: one
98 * table, one set of tests, two callers that want the same answer. */
99 6 static SV *pauth_escape(pTHX_ const char *s, STRLEN len) {
100 6 return pk_pct_encode(aTHX_ s, len, 1);
101 }
102  
103 /* a small finished triplet: status + type + body (+1) */
104 22 static SV *pauth_triplet(pTHX_ IV status, const char *type,
105 const char *body, STRLEN blen) {
106 22 AV *hdr = newAV(), *b = newAV(), *resp = newAV();
107 22 av_push(hdr, newSVpvs("Content-Type"));
108 22 av_push(hdr, newSVpv(type, 0));
109 22 av_push(hdr, newSVpvs("Content-Length"));
110 22 av_push(hdr, newSViv((IV)blen));
111 22 av_push(b, newSVpvn(body, blen));
112 22 av_push(resp, newSViv(status));
113 22 av_push(resp, newRV_noinc((SV *)hdr));
114 22 av_push(resp, newRV_noinc((SV *)b));
115 22 return newRV_noinc((SV *)resp);
116 }
117  
118 #define PAUTH_401_BODY "{\"errors\":[{\"message\":\"Unauthorized\"}]}"
119 #define PAUTH_403_BODY "{\"errors\":[{\"message\":\"Forbidden\"}]}"
120 #define PAUTH_404_BODY "{\"errors\":[{\"message\":\"Not Found\"}]}"
121  
122 /* The denial response for a failed guard (+1). opts may be NULL. */
123 29 static SV *pauth_denial(pTHX_ SV *c, HV *cfg, HV *opts) {
124 29 ▶ SV **od = opts ? hv_fetchs(opts, "on_denied", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **od = opts ? hv_fetchs(opts, "on_denied", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 29 0 29
125  
126 /*
127 # uncoverable condition right count:2 class:ignore_covered_err
128 # uncoverable condition right count:4 class:ignore_covered_err */
129 29 ▶ if (od && *od && SvROK(*od) && SvTYPE(SvRV(*od)) == SVt_PVCV) {
Branch: 4 branches
BranchTrueFalseTotal
if (od && *od && SvROK(*od) && SvTYPE(SvRV(*od)) == SVt_PVCV) { 4 25 29
if (od && *od && SvROK(*od) && SvTYPE(SvRV(*od)) == SVt_PVCV) { 4 0 4
if (od && *od && SvROK(*od) && SvTYPE(SvRV(*od)) == SVt_PVCV) { 1 3 4
if (od && *od && SvROK(*od) && SvTYPE(SvRV(*od)) == SVt_PVCV) { 1 0 1
130 1 dSP; int count; SV *r = NULL;
131 1 ENTER; SAVETMPS;
132 1 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 1 1
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; 0 1 1
133 1 count = call_sv(*od, G_SCALAR);
134 1 SPAGAIN;
135 /*
136 # uncoverable branch false
137 # uncoverable condition right count:1 class:ignore_covered_err
138 # uncoverable condition right count:2 class:ignore_covered_err */
139 1 ▶ if (count > 0) { SV *v = POPs; if (SvROK(v)) r = SvREFCNT_inc(v); }
Branch: 2 branches
BranchTrueFalseTotal
if (count > 0) { SV *v = POPs; if (SvROK(v)) r = SvREFCNT_inc(v); } 1 0 1
if (count > 0) { SV *v = POPs; if (SvROK(v)) r = SvREFCNT_inc(v); } 1 0 1
140 1 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 1 0 1
141 /*
142 # uncoverable branch false
143 # uncoverable condition right count:1 class:ignore_covered_err */
144 1 ▶ if (r) return r;
Branch: 1 branch
BranchTrueFalseTotal
if (r) return r; 1 0 1
145 /* a non-reference from on_denied falls through to the default */
146 }
147 /*
148 # uncoverable condition right count:2 class:ignore_covered_err
149 # uncoverable condition right count:3 class:ignore_covered_err
150 # uncoverable condition right count:4 class:ignore_covered_err */
151 28 ▶ else if (od && *od && SvOK(*od) && !SvROK(*od)) {
Branch: 4 branches
BranchTrueFalseTotal
else if (od && *od && SvOK(*od) && !SvROK(*od)) { 3 25 28
else if (od && *od && SvOK(*od) && !SvROK(*od)) { 3 0 3
else if (od && *od && SvOK(*od) && !SvROK(*od)) { 3 0 3
else if (od && *od && SvOK(*od) && !SvROK(*od)) { 3 0 3
152 3 STRLEN ol; const char *o = SvPV_const(*od, ol);
153 /*
154 # uncoverable branch true
155 # uncoverable condition left count:1 class:ignore_covered_err
156 # uncoverable condition right count:1 class:ignore_covered_err
157 # uncoverable condition left count:2 class:ignore_covered_err */
158 3 ▶ if (ol == 3 && memEQ(o, "403", 3))
Branch: 2 branches
BranchTrueFalseTotal
if (ol == 3 && memEQ(o, "403", 3)) 3 0 3
if (ol == 3 && memEQ(o, "403", 3)) 0 3 3
159 3 return pauth_triplet(aTHX_ 403, "application/json", /* # uncoverable statement class:ignore_covered_err */
160 PAUTH_403_BODY, sizeof(PAUTH_403_BODY) - 1);
161 /*
162 # uncoverable branch false
163 # uncoverable condition right count:1 class:ignore_covered_err
164 # uncoverable condition right count:2 class:ignore_covered_err */
165 3 ▶ if (ol == 3 && memEQ(o, "404", 3))
Branch: 2 branches
BranchTrueFalseTotal
if (ol == 3 && memEQ(o, "404", 3)) 3 0 3
if (ol == 3 && memEQ(o, "404", 3)) 3 0 3
166 3 return pauth_triplet(aTHX_ 404, "application/json",
167 PAUTH_404_BODY, sizeof(PAUTH_404_BODY) - 1);
168 }
169  
170 { /* negotiate: an Accept that wants text/html gets the login redirect */
171 25 SV *env = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_ENV);
172 25 ▶ HV *eh = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
HV *eh = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) 25 0 25
HV *eh = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) 25 0 25
173 50 ▶ ? (HV *)SvRV(env) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(env) : NULL; 25 0 25
174 25 ▶ SV **ac = eh ? hv_fetchs(eh, "HTTP_ACCEPT", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **ac = eh ? hv_fetchs(eh, "HTTP_ACCEPT", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 25 0 25
175 25 int wants_html = 0;
176 /*
177 # uncoverable condition right count:2 class:ignore_covered_err
178 # uncoverable condition right count:3 class:ignore_covered_err
179 # uncoverable condition right count:4 class:ignore_covered_err */
180 25 ▶ if (ac && *ac && SvOK(*ac) && SvCUR(*ac)) {
Branch: 4 branches
BranchTrueFalseTotal
if (ac && *ac && SvOK(*ac) && SvCUR(*ac)) { 19 6 25
if (ac && *ac && SvOK(*ac) && SvCUR(*ac)) { 19 0 19
if (ac && *ac && SvOK(*ac) && SvCUR(*ac)) { 19 0 19
if (ac && *ac && SvOK(*ac) && SvCUR(*ac)) { 19 0 19
181 pa_range ranges[PA_RANGE_MAX];
182 19 STRLEN al; const char *a = SvPV_const(*ac, al);
183 19 int nr = pa_parse(a, al, ranges, PA_RANGE_MAX);
184 19 int q = 0, order = 0;
185 19 int spec = pa_match(aTHX_ ranges, nr, "text", 4, "html", 4,
186 &q, &order);
187 19 ▶ wants_html = spec > 0 && q > 0; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
wants_html = spec > 0 && q > 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 6 13 19
wants_html = spec > 0 && q > 0; /* # uncoverable condition right count:2 class:ignore_covered_err */ 6 0 6
188 }
189 25 ▶ if (wants_html) {
Branch: 1 branch
BranchTrueFalseTotal
if (wants_html) { 6 19 25
190 STRLEN ll;
191 6 const char *login = ps_cfg_str(aTHX_ cfg, "login_path",
192 "/login", &ll);
193 6 SV *loc = newSVpvn(login, ll);
194 6 ▶ SV **pi = eh ? hv_fetchs(eh, "PATH_INFO", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
SV **pi = eh ? hv_fetchs(eh, "PATH_INFO", 0) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 6 0 6
195 6 AV *hdr = newAV(), *b = newAV(), *resp = newAV();
196 /*
197 # uncoverable branch false
198 # uncoverable condition right count:1 class:ignore_covered_err
199 # uncoverable condition right count:2 class:ignore_covered_err
200 # uncoverable condition right count:3 class:ignore_covered_err
201 # uncoverable condition right count:4 class:ignore_covered_err
202 # uncoverable condition right count:5 class:ignore_covered_err
203 # uncoverable condition left count:6 class:ignore_covered_err
204 # uncoverable condition right count:6 class:ignore_covered_err */
205 6 ▶ if (pi && *pi && SvOK(*pi) && SvCUR(*pi)
Branch: 4 branches
BranchTrueFalseTotal
if (pi && *pi && SvOK(*pi) && SvCUR(*pi) 6 0 6
if (pi && *pi && SvOK(*pi) && SvCUR(*pi) 6 0 6
if (pi && *pi && SvOK(*pi) && SvCUR(*pi) 6 0 6
if (pi && *pi && SvOK(*pi) && SvCUR(*pi) 6 0 6
206 6 ▶ && !(SvCUR(*pi) == ll && memEQ(SvPVX(*pi), login, ll))) {
Branch: 2 branches
BranchTrueFalseTotal
&& !(SvCUR(*pi) == ll && memEQ(SvPVX(*pi), login, ll))) { 0 6 6
&& !(SvCUR(*pi) == ll && memEQ(SvPVX(*pi), login, ll))) { 0 0 0
207 6 STRLEN pl; const char *p = SvPV_const(*pi, pl);
208 6 SV *esc = pauth_escape(aTHX_ p, pl);
209 6 sv_catpvs(loc, "?to=");
210 6 sv_catsv(loc, esc);
211 6 SvREFCNT_dec(esc);
212 }
213 6 av_push(hdr, newSVpvs("Location"));
214 6 av_push(hdr, loc);
215 6 av_push(hdr, newSVpvs("Content-Length"));
216 6 av_push(hdr, newSViv(0));
217 6 av_push(b, newSVpvs(""));
218 6 av_push(resp, newSViv(302));
219 6 av_push(resp, newRV_noinc((SV *)hdr));
220 6 av_push(resp, newRV_noinc((SV *)b));
221 6 return newRV_noinc((SV *)resp);
222 }
223 }
224 19 return pauth_triplet(aTHX_ 401, "application/json",
225 PAUTH_401_BODY, sizeof(PAUTH_401_BODY) - 1);
226 }
227  
228 /* The await seam, in C: a plain value passes through; an object with ->get
229 * is a future from a nonblocking model backend - a Punk::Future goes through
230 * $c->await (the loop-aware path), anything else through its own ->get.
231 * Returns +1. */
232 69 static SV *pauth_await(pTHX_ SV *c, SV *v) {
233 /*
234 # uncoverable condition left count:1 class:ignore_covered_err
235 # uncoverable condition left count:4 class:ignore_covered_err */
236 69 ▶ if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get")))
Branch: 4 branches
BranchTrueFalseTotal
if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) 69 0 69
if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) 62 7 69
if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) 2 60 62
if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) 0 2 2
237 67 ▶ return newSVsv(v ? v : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return newSVsv(v ? v : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */ 67 0 67
238 /*
239 # uncoverable branch true
240 # uncoverable condition left count:1 class:ignore_covered_err */
241 2 ▶ if (sv_derived_from(v, "Punk::Future")) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(v, "Punk::Future")) { 0 2 2
242 SV *argv[1], *r;
243 0 argv[0] = v; /* # uncoverable statement */
244 0 r = pcx_call_meth(aTHX_ c, "await", argv, 1, 1); /* # uncoverable statement */
245 /*
246 # uncoverable statement
247 # uncoverable condition left count:1
248 # uncoverable condition right count:1 */
249 0 ▶ return r ? r : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
return r ? r : newSV(0); 0 0 0
250 }
251 {
252 2 SV *r = pcx_call_meth(aTHX_ v, "get", NULL, 0, 1);
253 2 ▶ return r ? r : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return r ? r : newSV(0); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 2 2
254 }
255 }
256  
257 /* one field name out of the auth config's fields map (borrowed pointer) */
258 74 static const char *pauth_field(pTHX_ HV *cfg, const char *name,
259 const char *def, STRLEN *lp) {
260 74 SV **f = hv_fetchs(cfg, "fields", 0);
261 /*
262 # uncoverable branch false
263 # uncoverable condition right count:1 class:ignore_covered_err
264 # uncoverable condition right count:2 class:ignore_covered_err
265 # uncoverable condition right count:3 class:ignore_covered_err
266 # uncoverable condition right count:4 class:ignore_covered_err */
267 74 ▶ if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) {
Branch: 4 branches
BranchTrueFalseTotal
if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 74 0 74
if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 74 0 74
if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 74 0 74
if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { 74 0 74
268 74 SV **e = hv_fetch((HV *)SvRV(*f), name, (I32)strlen(name), 0);
269 /*
270 # uncoverable branch false
271 # uncoverable condition right count:1 class:ignore_covered_err
272 # uncoverable condition right count:2 class:ignore_covered_err
273 # uncoverable condition right count:3 class:ignore_covered_err */
274 148 ▶ if (e && *e && SvOK(*e)) return SvPV_const(*e, *lp);
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) return SvPV_const(*e, *lp); 74 0 74
if (e && *e && SvOK(*e)) return SvPV_const(*e, *lp); 74 0 74
if (e && *e && SvOK(*e)) return SvPV_const(*e, *lp); 74 0 74
275 }
276 0 *lp = strlen(def); /* # uncoverable statement */
277 0 return def; /* # uncoverable statement */
278 }
279  
280 /* the model instance behind a config option ('model' / 'token_model'),
281 * croaking with the fix when the option was never given. Returns +1. */
282 53 static SV *pauth_model(pTHX_ SV *c, HV *cfg, const char *opt,
283 const char *what) {
284 53 SV **mn = hv_fetch(cfg, opt, (I32)strlen(opt), 0);
285 SV *argv[1], *m;
286 /*
287 # uncoverable condition left count:2 class:ignore_covered_err
288 # uncoverable condition left count:3 class:ignore_covered_err */
289 53 ▶ if (!(mn && *mn && SvOK(*mn)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(mn && *mn && SvOK(*mn))) 52 1 53
if (!(mn && *mn && SvOK(*mn))) 52 0 52
if (!(mn && *mn && SvOK(*mn))) 0 52 52
290 1 croak("Punk: %s needs `auth %s => ...`", what, opt);
291 52 argv[0] = *mn;
292 52 m = pcx_call_meth(aTHX_ c, "model", argv, 1, 1);
293 /*
294 # uncoverable branch true
295 # uncoverable condition left count:1 class:ignore_covered_err */
296 52 ▶ if (!m)
Branch: 1 branch
BranchTrueFalseTotal
if (!m) 0 52 52
297 0 croak("Punk: %s could not build the %s model", what, opt); /* # uncoverable statement */
298 52 return m;
299 }
300  
301 /* the rows arrayref out of a search result (borrowed), or NULL */
302 17 static AV *pauth_rows(pTHX_ SV *res) {
303 SV **r;
304 /*
305 # uncoverable branch true
306 # uncoverable condition left count:1 class:ignore_covered_err
307 # uncoverable condition left count:2 class:ignore_covered_err
308 # uncoverable condition left count:3 class:ignore_covered_err */
309 17 ▶ if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) return NULL; 17 0 17
if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) return NULL; 17 0 17
if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) return NULL; 0 17 17
310 17 r = hv_fetchs((HV *)SvRV(res), "rows", 0);
311 17 ▶ return (r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
return (r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVAV) 17 0 17
return (r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVAV) 17 0 17
return (r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVAV) 17 0 17
312 34 ▶ ? (AV *)SvRV(*r) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*r) : NULL; 17 0 17
313 }
314  
315 /* the stash's punk.auth slot, autovivified (borrowed) */
316 37 static HV *pauth_memo(pTHX_ SV *c) {
317 37 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c));
318 37 SV **slot = hv_fetchs(stash, "punk.auth", 0);
319 HV *pa;
320 /*
321 # uncoverable condition right count:2 class:ignore_covered_err
322 # uncoverable condition right count:3 class:ignore_covered_err
323 # uncoverable condition right count:4 class:ignore_covered_err */
324 37 ▶ if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) 1 36 37
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) 1 0 1
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) 1 0 1
if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) 1 0 1
325 1 return (HV *)SvRV(*slot);
326 36 pa = newHV();
327 36 (void)hv_stores(stash, "punk.auth", newRV_noinc((SV *)pa));
328 36 return pa;
329 }
330  
331 /* The signed-in user's row, loaded once per request through the configured
332 * model and memoized in the stash - the memo records "loaded, and it was
333 * undef" as an undef entry, so a stale session costs one lookup, not one per
334 * call. Returns +1 or NULL. */
335 37 static SV *pauth_current_user(pTHX_ SV *c, HV *cfg) {
336 37 HV *pa = pauth_memo(aTHX_ c);
337 37 SV **u = hv_fetchs(pa, "user", 0);
338 37 SV *id, *user = NULL;
339 37 ▶ if (u && *u) return SvOK(*u) ? newSVsv(*u) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (u && *u) return SvOK(*u) ? newSVsv(*u) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 36 37
if (u && *u) return SvOK(*u) ? newSVsv(*u) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
if (u && *u) return SvOK(*u) ? newSVsv(*u) : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
340  
341 36 id = pauth_session_id(aTHX_ c, cfg);
342 36 ▶ if (id) {
Branch: 1 branch
BranchTrueFalseTotal
if (id) { 35 1 36
343 35 SV *m = pauth_model(aTHX_ c, cfg, "model", "current_user");
344 STRLEN idl;
345 35 const char *idf = pauth_field(aTHX_ cfg, "id", "id", &idl);
346 SV *argv[2], *raw, *got;
347 35 argv[0] = sv_2mortal(newSVpvn(idf, idl));
348 35 argv[1] = id;
349 35 raw = pcx_call_meth(aTHX_ m, "get", argv, 2, 1);
350 35 SvREFCNT_dec(m);
351 35 SvREFCNT_dec(id);
352 35 ▶ got = pauth_await(aTHX_ c, raw ? raw : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
got = pauth_await(aTHX_ c, raw ? raw : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */ 35 0 35
353 /*
354 # uncoverable branch false
355 # uncoverable condition right count:1 class:ignore_covered_err */
356 35 ▶ if (raw) SvREFCNT_dec(raw);
Branch: 1 branch
BranchTrueFalseTotal
if (raw) SvREFCNT_dec(raw); 35 0 35
357 /*
358 # uncoverable branch false
359 # uncoverable condition right count:1 class:ignore_covered_err
360 # uncoverable condition right count:2 class:ignore_covered_err
361 # uncoverable condition right count:3 class:ignore_covered_err */
362 35 ▶ if (got && SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV) user = got;
Branch: 3 branches
BranchTrueFalseTotal
if (got && SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV) user = got; 35 0 35
if (got && SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV) user = got; 35 0 35
if (got && SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV) user = got; 35 0 35
363 /*
364 # uncoverable statement
365 # uncoverable branch true
366 # uncoverable branch false
367 # uncoverable condition left count:1
368 # uncoverable condition right count:1 */
369 0 ▶ else if (got) SvREFCNT_dec(got);
Branch: 1 branch
BranchTrueFalseTotal
else if (got) SvREFCNT_dec(got); 0 0 0
370 }
371 36 ▶ (void)hv_stores(pa, "user", user ? newSVsv(user) : newSV(0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(pa, "user", user ? newSVsv(user) : newSV(0)); 35 1 36
372 36 return user;
373 }
374  
375 /* The role/verified half of a guard, reached only when the guard has
376 * options. The roles hook may return one name, a list, or an arrayref - a
377 * user can hold several roles. A required role in the rank ladder means
378 * "this or better"; one outside it is orthogonal and matches exactly.
379 * Returns a denial response (+1) or NULL to pass. */
380 26 static SV *pauth_check(pTHX_ SV *c, HV *cfg, HV *opts) {
381 26 SV *user = pauth_current_user(aTHX_ c, cfg);
382 SV **od;
383 /*
384 # uncoverable branch true
385 # uncoverable condition left count:1 class:ignore_covered_err */
386 26 ▶ if (!user) return pauth_denial(aTHX_ c, cfg, opts);
Branch: 1 branch
BranchTrueFalseTotal
if (!user) return pauth_denial(aTHX_ c, cfg, opts); 0 26 26
387  
388 26 od = hv_fetchs(opts, "verified", 0);
389 /*
390 # uncoverable condition right count:2 class:ignore_covered_err
391 # uncoverable condition right count:3 class:ignore_covered_err */
392 26 ▶ if (od && *od && SvTRUE(*od)) {
Branch: 3 branches
BranchTrueFalseTotal
if (od && *od && SvTRUE(*od)) { 12 14 26
if (od && *od && SvTRUE(*od)) { 12 0 12
if (od && *od && SvTRUE(*od)) { 12 0 12
393 STRLEN vl;
394 12 const char *vf = pauth_field(aTHX_ cfg, "verified", "verified", &vl);
395 12 SV **e = hv_fetch((HV *)SvRV(user), vf, (I32)vl, 0);
396 /*
397 # uncoverable condition left count:1 class:ignore_covered_err
398 # uncoverable condition left count:2 class:ignore_covered_err */
399 12 ▶ if (!(e && *e && pauth_truthy(aTHX_ *e))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && pauth_truthy(aTHX_ *e))) { 12 0 12
if (!(e && *e && pauth_truthy(aTHX_ *e))) { 12 0 12
if (!(e && *e && pauth_truthy(aTHX_ *e))) { 8 4 12
400 8 SvREFCNT_dec(user);
401 8 return pauth_denial(aTHX_ c, cfg, opts);
402 }
403 }
404  
405 18 od = hv_fetchs(opts, "role", 0);
406 /*
407 # uncoverable condition right count:2 class:ignore_covered_err
408 # uncoverable condition right count:3 class:ignore_covered_err */
409 18 ▶ if (od && *od && SvOK(*od)) {
Branch: 3 branches
BranchTrueFalseTotal
if (od && *od && SvOK(*od)) { 14 4 18
if (od && *od && SvOK(*od)) { 14 0 14
if (od && *od && SvOK(*od)) { 14 0 14
410 14 SV **rl = hv_fetchs(cfg, "roles", 0);
411 14 SV **rk = hv_fetchs(cfg, "rank", 0);
412 6 ▶ AV *rank = (rk && *rk && SvROK(*rk)
Branch: 2 branches
BranchTrueFalseTotal
AV *rank = (rk && *rk && SvROK(*rk) 6 0 6
AV *rank = (rk && *rk && SvROK(*rk) 6 0 6
413 6 ▶ && SvTYPE(SvRV(*rk)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(*rk)) == SVt_PVAV) 6 0 6
414 20 ▶ ? (AV *)SvRV(*rk) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (AV *)SvRV(*rk) : NULL; 6 8 14
415 static const char *const DEFAULT_RANK[] =
416 { "member", "admin", "owner" };
417 14 AV *have = (AV *)sv_2mortal((SV *)newAV());
418 STRLEN nl;
419 14 const char *need = SvPV_const(*od, nl);
420 SSize_t i, hn;
421 14 IV need_idx = -1;
422 14 int ok = 0;
423  
424 /*
425 # uncoverable condition left count:2 class:ignore_covered_err
426 # uncoverable condition left count:3 class:ignore_covered_err
427 # uncoverable condition left count:4 class:ignore_covered_err */
428 14 ▶ if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { 13 1 14
if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { 13 0 13
if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { 13 0 13
if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { 0 13 13
429 1 SvREFCNT_dec(user);
430 1 croak("Punk: auth_guard role checks need "
431 "`auth roles => sub { ... }`");
432 }
433 { /* the hook, in list context */
434 13 dSP; int count, j;
435 13 ENTER; SAVETMPS;
436 13 ▶ PUSHMARK(SP); EXTEND(SP, 2);
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 2); 0 13 13
PUSHMARK(SP); EXTEND(SP, 2); 0 13 13
437 13 PUSHs(c); PUSHs(user); PUTBACK;
438 13 count = call_sv(*rl, G_ARRAY);
439 13 SPAGAIN;
440 28 ▶ for (j = 0; j < count; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < count; j++) { 15 13 28
441 15 SV *v = POPs; /* never inside a multi-eval macro */
442 15 (void)av_store(have, count - 1 - j, newSVsv(v));
443 }
444 13 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 13 0 13
445 }
446 13 ▶ if (av_len(have) == 0) { /* one return that is an arrayref: flatten */
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(have) == 0) { /* one return that is an arrayref: flatten */ 11 2 13
447 11 SV **h0 = av_fetch(have, 0, 0);
448 /*
449 # uncoverable condition right count:1 class:ignore_covered_err
450 # uncoverable condition right count:2 class:ignore_covered_err
451 # uncoverable condition right count:4 class:ignore_covered_err */
452 11 ▶ if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) {
Branch: 4 branches
BranchTrueFalseTotal
if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { 11 0 11
if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { 11 0 11
if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { 6 5 11
if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { 6 0 6
453 6 AV *inner = (AV *)SvRV(*h0);
454 6 AV *flat = (AV *)sv_2mortal((SV *)newAV());
455 6 SSize_t k, n2 = av_len(inner) + 1;
456 13 ▶ for (k = 0; k < n2; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n2; k++) { 7 6 13
457 7 SV **e = av_fetch(inner, k, 0);
458 /*
459 # uncoverable branch false
460 # uncoverable condition right count:1 class:ignore_covered_err
461 # uncoverable condition right count:2 class:ignore_covered_err */
462 7 ▶ if (e && *e) av_push(flat, newSVsv(*e));
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) av_push(flat, newSVsv(*e)); 7 0 7
if (e && *e) av_push(flat, newSVsv(*e)); 7 0 7
463 }
464 6 have = flat;
465 }
466 }
467  
468 { /* where does the required role sit on the ladder? */
469 6 SSize_t rn = rank ? av_len(rank) + 1
470 19 ▶ : (SSize_t)(sizeof(DEFAULT_RANK)
Branch: 1 branch
BranchTrueFalseTotal
: (SSize_t)(sizeof(DEFAULT_RANK) 6 7 13
471 / sizeof(*DEFAULT_RANK));
472 35 ▶ for (i = 0; i < rn; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rn; i++) { 32 3 35
473 STRLEN xl; const char *x;
474 32 ▶ if (rank) {
Branch: 1 branch
BranchTrueFalseTotal
if (rank) { 15 17 32
475 15 SV **e = av_fetch(rank, i, 0);
476 /*
477 # uncoverable branch true
478 # uncoverable condition left count:1 class:ignore_covered_err
479 # uncoverable condition left count:2 class:ignore_covered_err
480 # uncoverable condition left count:3 class:ignore_covered_err */
481 15 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 15 0 15
if (!(e && *e && SvOK(*e))) continue; 15 0 15
if (!(e && *e && SvOK(*e))) continue; 0 15 15
482 30 x = SvPV_const(*e, xl);
483 }
484 17 else { x = DEFAULT_RANK[i]; xl = strlen(x); }
485 32 ▶ if (xl == nl && memEQ(x, need, nl)) { need_idx = (IV)i; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (xl == nl && memEQ(x, need, nl)) { need_idx = (IV)i; break; } 19 13 32
if (xl == nl && memEQ(x, need, nl)) { need_idx = (IV)i; break; } 10 9 19
486 }
487 }
488 13 hn = av_len(have) + 1;
489 29 ▶ for (i = 0; i < hn && !ok; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < hn && !ok; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 13 29
for (i = 0; i < hn && !ok; i++) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 16 0 16
490 16 SV **hp = av_fetch(have, i, 0);
491 STRLEN hl; const char *h;
492 /*
493 # uncoverable branch true
494 # uncoverable condition left count:1 class:ignore_covered_err
495 # uncoverable condition left count:2 class:ignore_covered_err
496 # uncoverable condition left count:3 class:ignore_covered_err */
497 16 ▶ if (!(hp && *hp && SvOK(*hp))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(hp && *hp && SvOK(*hp))) continue; 16 0 16
if (!(hp && *hp && SvOK(*hp))) continue; 16 0 16
if (!(hp && *hp && SvOK(*hp))) continue; 0 16 16
498 16 h = SvPV_const(*hp, hl);
499 16 ▶ if (need_idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (need_idx < 0) { 4 12 16
500 /* orthogonal: exact membership */
501 4 ▶ if (hl == nl && memEQ(h, need, nl)) ok = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (hl == nl && memEQ(h, need, nl)) ok = 1; 3 1 4
if (hl == nl && memEQ(h, need, nl)) ok = 1; 1 2 3
502 }
503 else {
504 /* ladder: any held role at or above the requirement */
505 6 SSize_t rn = rank ? av_len(rank) + 1
506 18 ▶ : (SSize_t)(sizeof(DEFAULT_RANK)
Branch: 1 branch
BranchTrueFalseTotal
: (SSize_t)(sizeof(DEFAULT_RANK) 6 6 12
507 / sizeof(*DEFAULT_RANK));
508 SSize_t j;
509 25 ▶ for (j = need_idx; j < rn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = need_idx; j < rn; j++) { 18 7 25
510 STRLEN xl; const char *x;
511 18 ▶ if (rank) {
Branch: 1 branch
BranchTrueFalseTotal
if (rank) { 8 10 18
512 8 SV **e = av_fetch(rank, j, 0);
513 /*
514 # uncoverable branch true
515 # uncoverable condition left count:1 class:ignore_covered_err
516 # uncoverable condition left count:2 class:ignore_covered_err
517 # uncoverable condition left count:3 class:ignore_covered_err */
518 8 ▶ if (!(e && *e && SvOK(*e))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(e && *e && SvOK(*e))) continue; 8 0 8
if (!(e && *e && SvOK(*e))) continue; 8 0 8
if (!(e && *e && SvOK(*e))) continue; 0 8 8
519 16 x = SvPV_const(*e, xl);
520 }
521 10 else { x = DEFAULT_RANK[j]; xl = strlen(x); }
522 18 ▶ if (xl == hl && memEQ(x, h, hl)) { ok = 1; break; }
Branch: 2 branches
BranchTrueFalseTotal
if (xl == hl && memEQ(x, h, hl)) { ok = 1; break; } 11 7 18
if (xl == hl && memEQ(x, h, hl)) { ok = 1; break; } 5 6 11
523 }
524 }
525 }
526 13 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 7 6 13
527 7 SvREFCNT_dec(user);
528 7 return pauth_denial(aTHX_ c, cfg, opts);
529 }
530 { /* record what the guard learned */
531 6 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c));
532 6 SV **a = hv_fetchs(stash, "auth", 0);
533 HV *ah;
534 6 AV *copy = newAV();
535 6 SSize_t k, n2 = av_len(have) + 1;
536 15 ▶ for (k = 0; k < n2; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n2; k++) { 9 6 15
537 9 SV **e = av_fetch(have, k, 0);
538 /*
539 # uncoverable branch false
540 # uncoverable condition right count:1 class:ignore_covered_err
541 # uncoverable condition right count:2 class:ignore_covered_err
542 # uncoverable condition right count:3 class:ignore_covered_err */
543 9 ▶ if (e && *e && SvOK(*e)) av_push(copy, newSVsv(*e));
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && SvOK(*e)) av_push(copy, newSVsv(*e)); 9 0 9
if (e && *e && SvOK(*e)) av_push(copy, newSVsv(*e)); 9 0 9
if (e && *e && SvOK(*e)) av_push(copy, newSVsv(*e)); 9 0 9
544 }
545 /*
546 # uncoverable branch false
547 # uncoverable condition right count:1 class:ignore_covered_err
548 # uncoverable condition right count:2 class:ignore_covered_err
549 # uncoverable condition right count:3 class:ignore_covered_err
550 # uncoverable condition right count:4 class:ignore_covered_err */
551 6 ▶ if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 6 0 6
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 6 0 6
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 6 0 6
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 6 0 6
552 6 ah = (HV *)SvRV(*a);
553 else {
554 0 ah = newHV(); /* # uncoverable statement */
555 0 (void)hv_stores(stash, "auth", newRV_noinc((SV *)ah)); /* # uncoverable statement */
556 }
557 6 (void)hv_stores(ah, "roles", newRV_noinc((SV *)copy));
558 }
559 }
560  
561 { /* the loaded user rides along for the handler */
562 10 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c));
563 10 SV **a = hv_fetchs(stash, "auth", 0);
564 HV *ah;
565 /*
566 # uncoverable branch false
567 # uncoverable condition right count:1 class:ignore_covered_err
568 # uncoverable condition right count:2 class:ignore_covered_err
569 # uncoverable condition right count:3 class:ignore_covered_err
570 # uncoverable condition right count:4 class:ignore_covered_err */
571 10 ▶ if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 10 0 10
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 10 0 10
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 10 0 10
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 10 0 10
572 10 ah = (HV *)SvRV(*a);
573 else {
574 0 ah = newHV(); /* # uncoverable statement */
575 0 (void)hv_stores(stash, "auth", newRV_noinc((SV *)ah)); /* # uncoverable statement */
576 }
577 10 (void)hv_stores(ah, "user", newSVsv(user));
578 }
579 10 SvREFCNT_dec(user);
580 10 return NULL;
581 }
582  
583 /* record the signed-in id in the stash's auth slot (the Open::API checker
584 * convention: authenticated facts live under stash->{auth}) */
585 32 static void pauth_stash_id(pTHX_ SV *c, SV *id) {
586 32 HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c));
587 32 SV **a = hv_fetchs(stash, "auth", 0);
588 HV *ah;
589 /*
590 # uncoverable branch true
591 # uncoverable condition left count:1 class:ignore_covered_err
592 # uncoverable condition left count:2 class:ignore_covered_err
593 # uncoverable condition right count:2 class:ignore_covered_err
594 # uncoverable condition left count:3 class:ignore_covered_err
595 # uncoverable condition right count:3 class:ignore_covered_err
596 # uncoverable condition left count:4 class:ignore_covered_err
597 # uncoverable condition right count:4 class:ignore_covered_err */
598 32 ▶ if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 0 32 32
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 0 0 0
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 0 0 0
if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) 0 0 0
599 0 ah = (HV *)SvRV(*a); /* # uncoverable statement */
600 else {
601 32 ah = newHV();
602 32 (void)hv_stores(stash, "auth", newRV_noinc((SV *)ah));
603 }
604 32 (void)hv_stores(ah, "user_id", newSVsv(id));
605 32 }
606  
607 /* The guard closure: capture [ $app, \%opts ]. Body: no session id is a
608 * denial; a bare guard passes on the id alone; role/verified options load
609 * the user through the Perl half. */
610 XS_INTERNAL(pauth_guard_cb);
611 47 XS_INTERNAL(pauth_guard_cb) {
612 47 dXSARGS;
613 47 AV *cap = punk_clos_cap(aTHX_ cv);
614 SV *app_sv, *opts_sv, *c, *id;
615 47 HV *cfg = NULL, *opts = NULL;
616 /*
617 # uncoverable branch true class:ignore_covered_err
618 # uncoverable condition left count:1 class:ignore_covered_err
619 # uncoverable condition left count:2 class:ignore_covered_err */
620 47 ▶ if (!cap || items < 1) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || items < 1) XSRETURN_EMPTY; 47 0 47
if (!cap || items < 1) XSRETURN_EMPTY; 0 47 47
621 47 app_sv = *av_fetch(cap, 0, 0);
622 47 opts_sv = *av_fetch(cap, 1, 0);
623 47 c = ST(0);
624  
625 /*
626 # uncoverable branch false class:ignore_covered_err
627 # uncoverable condition right count:1 class:ignore_covered_err
628 # uncoverable condition right count:2 class:ignore_covered_err
629 # uncoverable condition right count:3 class:ignore_covered_err */
630 47 ▶ if (app_sv && SvROK(app_sv) && SvTYPE(SvRV(app_sv)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (app_sv && SvROK(app_sv) && SvTYPE(SvRV(app_sv)) == SVt_PVHV) { 47 0 47
if (app_sv && SvROK(app_sv) && SvTYPE(SvRV(app_sv)) == SVt_PVHV) { 47 0 47
if (app_sv && SvROK(app_sv) && SvTYPE(SvRV(app_sv)) == SVt_PVHV) { 47 0 47
631 47 SV **s = hv_fetchs((HV *)SvRV(app_sv), K_AUTH, 0);
632 /*
633 # uncoverable condition right count:2 class:ignore_covered_err
634 # uncoverable condition right count:3 class:ignore_covered_err
635 # uncoverable condition right count:4 class:ignore_covered_err */
636 47 ▶ if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 46 1 47
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 46 0 46
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 46 0 46
if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) 46 0 46
637 46 cfg = (HV *)SvRV(*s);
638 }
639 47 ▶ if (!cfg)
Branch: 1 branch
BranchTrueFalseTotal
if (!cfg) 1 46 47
640 1 croak("Punk: auth_guard needs the auth keyword (add `auth ...`)");
641 /*
642 # uncoverable branch false class:ignore_covered_err
643 # uncoverable condition right count:1 class:ignore_covered_err
644 # uncoverable condition right count:2 class:ignore_covered_err
645 # uncoverable condition right count:3 class:ignore_covered_err */
646 46 ▶ if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 46 0 46
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 46 0 46
if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) 46 0 46
647 46 opts = (HV *)SvRV(opts_sv);
648  
649 46 id = pauth_session_id(aTHX_ c, cfg);
650 46 ▶ if (!id) {
Branch: 1 branch
BranchTrueFalseTotal
if (!id) { 14 32 46
651 14 ST(0) = sv_2mortal(pauth_denial(aTHX_ c, cfg, opts));
652 14 XSRETURN(1);
653 }
654 32 pauth_stash_id(aTHX_ c, id);
655 32 SvREFCNT_dec(id);
656  
657 32 ▶ if (opts && HvUSEDKEYS(opts)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (opts && HvUSEDKEYS(opts)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 32 0 32
if (opts && HvUSEDKEYS(opts)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 32 32
if (opts && HvUSEDKEYS(opts)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 26 6 32
658 26 SV *r = pauth_check(aTHX_ c, cfg, opts);
659 25 ▶ if (r) { ST(0) = sv_2mortal(r); XSRETURN(1); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) { ST(0) = sv_2mortal(r); XSRETURN(1); } 15 10 25
660 }
661 16 XSRETURN_EMPTY;
662 }
663  
664 #endif /* PUNK_AUTH_H */
« include/punk/punk_compile.h ↑ Summary include/punk/punk_ratelimit.h »