| 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 |
88 |
| static HV *pauth_cfg(pTHX_ SV *c) { |
| 24 |
88 |
| SV *app = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_APP); |
| 25 |
|
| SV **s; |
| 26 |
88 |
▶ | if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
88 |
0 |
88 |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
88 |
0 |
88 |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return NULL; |
0 |
88 |
88 |
|
| 27 |
88 |
| s = hv_fetchs((HV *)SvRV(app), K_AUTH, 0); |
| 28 |
82 |
▶ | return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
82 |
0 |
82 |
| return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
82 |
0 |
82 |
| return (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
82 |
0 |
82 |
|
| 29 |
170 |
▶ | ? (HV *)SvRV(*s) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(*s) : NULL; |
82 |
6 |
88 |
|
| 30 |
|
| } |
| 31 |
|
| |
| 32 |
|
| /* Is this a user id? Defined, not empty and not "0": an id a backend handed |
| 33 |
|
| * back empty must not read as somebody being signed in. */ |
| 34 |
99 |
| static int pauth_id_ok(pTHX_ SV *id) { |
| 35 |
|
| STRLEN l; |
| 36 |
|
| const char *p; |
| 37 |
99 |
▶ | if (!(id && SvOK(id))) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(id && SvOK(id))) return 0; |
99 |
0 |
99 |
| if (!(id && SvOK(id))) return 0; |
0 |
99 |
99 |
|
| 38 |
99 |
| p = SvPV_const(id, l); |
| 39 |
99 |
▶ | return l > 0 && !(l == 1 && *p == '0'); |
Branch: 3 branches
| Branch | True | False | Total |
| return l > 0 && !(l == 1 && *p == '0'); |
95 |
4 |
99 |
| return l > 0 && !(l == 1 && *p == '0'); |
78 |
17 |
95 |
| return l > 0 && !(l == 1 && *p == '0'); |
75 |
3 |
78 |
|
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
|
| /* A `verified` column is true unless it is false to Perl or spells false the |
| 43 |
|
| * way a database driver does: PostgreSQL's 'f' is a true string. */ |
| 44 |
12 |
| static int pauth_truthy(pTHX_ SV *v) { |
| 45 |
|
| STRLEN l; |
| 46 |
|
| const char *p; |
| 47 |
12 |
▶ | if (!(v && SvOK(v))) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(v && SvOK(v))) return 0; |
12 |
0 |
12 |
| if (!(v && SvOK(v))) return 0; |
0 |
12 |
12 |
|
| 48 |
12 |
▶ | if (SvROK(v)) return SvTRUE(v) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(v)) return SvTRUE(v) ? 1 : 0; |
0 |
12 |
12 |
|
| 49 |
12 |
| p = SvPV_const(v, l); |
| 50 |
12 |
▶ | if (!l || (l == 1 && *p == '0')) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!l || (l == 1 && *p == '0')) return 0; |
12 |
0 |
12 |
| if (!l || (l == 1 && *p == '0')) return 0; |
6 |
6 |
12 |
| if (!l || (l == 1 && *p == '0')) return 0; |
1 |
5 |
6 |
|
| 51 |
11 |
▶ | if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; |
Branch: 5 branches
| Branch | True | False | Total |
| if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; |
5 |
6 |
11 |
| if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; |
4 |
1 |
5 |
| if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; |
3 |
1 |
4 |
| if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; |
2 |
1 |
3 |
| if (l == 1 && (*p == 'f' || *p == 'F' || *p == 'n' || *p == 'N')) return 0; |
0 |
2 |
2 |
|
| 52 |
8 |
▶ | if (l == 5 && foldEQ(p, "false", 5)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l == 5 && foldEQ(p, "false", 5)) return 0; |
2 |
6 |
8 |
| if (l == 5 && foldEQ(p, "false", 5)) return 0; |
2 |
0 |
2 |
|
| 53 |
6 |
▶ | if (l == 2 && foldEQ(p, "no", 2)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l == 2 && foldEQ(p, "no", 2)) return 0; |
1 |
5 |
6 |
| if (l == 2 && foldEQ(p, "no", 2)) return 0; |
1 |
0 |
1 |
|
| 54 |
5 |
▶ | if (l == 3 && foldEQ(p, "off", 3)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (l == 3 && foldEQ(p, "off", 3)) return 0; |
1 |
4 |
5 |
| if (l == 3 && foldEQ(p, "off", 3)) return 0; |
1 |
0 |
1 |
|
| 55 |
4 |
| return SvTRUE(v) ? 1 : 0; |
| 56 |
|
| } |
| 57 |
|
| |
| 58 |
|
| /* the signed-in user id from the session, or NULL. Returns +1. */ |
| 59 |
84 |
| static SV *pauth_session_id(pTHX_ SV *c, HV *cfg) { |
| 60 |
|
| STRLEN kl; |
| 61 |
84 |
| const char *key = ps_cfg_str(aTHX_ cfg, "session_key", "user_id", &kl); |
| 62 |
84 |
| SV *sess = ps_load(aTHX_ c); |
| 63 |
84 |
| SV *out = NULL; |
| 64 |
84 |
▶ | if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { |
84 |
0 |
84 |
| if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { |
84 |
0 |
84 |
| if (sess && SvROK(sess) && SvTYPE(SvRV(sess)) == SVt_PVHV) { |
84 |
0 |
84 |
|
| 65 |
84 |
| SV **e = hv_fetch((HV *)SvRV(sess), key, (I32)kl, 0); |
| 66 |
84 |
▶ | if (e && *e && pauth_id_ok(aTHX_ *e)) out = newSVsv(*e); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && pauth_id_ok(aTHX_ *e)) out = newSVsv(*e); |
67 |
17 |
84 |
| if (e && *e && pauth_id_ok(aTHX_ *e)) out = newSVsv(*e); |
67 |
0 |
67 |
| if (e && *e && pauth_id_ok(aTHX_ *e)) out = newSVsv(*e); |
63 |
4 |
67 |
|
| 67 |
|
| } |
| 68 |
84 |
▶ | if (sess) SvREFCNT_dec(sess); |
Branch: 1 branch
| Branch | True | False | Total |
| if (sess) SvREFCNT_dec(sess); |
84 |
0 |
84 |
|
| 69 |
84 |
| return out; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| /* percent-encode a path for the ?to= value - unreserved chars and '/' pass. |
| 73 |
|
| * The encoder itself is punk_url.h's, which url_for uses for a *splat: one |
| 74 |
|
| * table, one set of tests, two callers that want the same answer. */ |
| 75 |
6 |
| static SV *pauth_escape(pTHX_ const char *s, STRLEN len) { |
| 76 |
6 |
| return pk_pct_encode(aTHX_ s, len, 1); |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| /* a small finished triplet: status + type + body (+1) */ |
| 80 |
19 |
| static SV *pauth_triplet(pTHX_ IV status, const char *type, |
| 81 |
|
| const char *body, STRLEN blen) { |
| 82 |
19 |
| AV *hdr = newAV(), *b = newAV(), *resp = newAV(); |
| 83 |
19 |
| av_push(hdr, newSVpvs("Content-Type")); |
| 84 |
19 |
| av_push(hdr, newSVpv(type, 0)); |
| 85 |
19 |
| av_push(hdr, newSVpvs("Content-Length")); |
| 86 |
19 |
| av_push(hdr, newSViv((IV)blen)); |
| 87 |
19 |
| av_push(b, newSVpvn(body, blen)); |
| 88 |
19 |
| av_push(resp, newSViv(status)); |
| 89 |
19 |
| av_push(resp, newRV_noinc((SV *)hdr)); |
| 90 |
19 |
| av_push(resp, newRV_noinc((SV *)b)); |
| 91 |
19 |
| return newRV_noinc((SV *)resp); |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| #define PAUTH_401_BODY "{\"errors\":[{\"message\":\"Unauthorized\"}]}" |
| 95 |
|
| #define PAUTH_403_BODY "{\"errors\":[{\"message\":\"Forbidden\"}]}" |
| 96 |
|
| #define PAUTH_404_BODY "{\"errors\":[{\"message\":\"Not Found\"}]}" |
| 97 |
|
| |
| 98 |
|
| /* The denial response for a failed guard (+1). opts may be NULL. */ |
| 99 |
26 |
| static SV *pauth_denial(pTHX_ SV *c, HV *cfg, HV *opts) { |
| 100 |
26 |
▶ | SV **od = opts ? hv_fetchs(opts, "on_denied", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **od = opts ? hv_fetchs(opts, "on_denied", 0) : NULL; |
26 |
0 |
26 |
|
| 101 |
|
| |
| 102 |
26 |
▶ | if (od && *od && SvROK(*od) && SvTYPE(SvRV(*od)) == SVt_PVCV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (od && *od && SvROK(*od) && SvTYPE(SvRV(*od)) == SVt_PVCV) { |
4 |
22 |
26 |
| 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 |
|
| 103 |
1 |
| dSP; int count; SV *r = NULL; |
| 104 |
1 |
| ENTER; SAVETMPS; |
| 105 |
1 |
▶ | PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
1 |
1 |
| PUSHMARK(SP); EXTEND(SP, 1); PUSHs(c); PUTBACK; |
0 |
1 |
1 |
|
| 106 |
1 |
| count = call_sv(*od, G_SCALAR); |
| 107 |
1 |
| SPAGAIN; |
| 108 |
1 |
▶ | if (count > 0) { SV *v = POPs; if (SvROK(v)) r = SvREFCNT_inc(v); } |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 109 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 110 |
1 |
▶ | if (r) return r; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) return r; |
1 |
0 |
1 |
|
| 111 |
|
| /* a non-reference from on_denied falls through to the default */ |
| 112 |
|
| } |
| 113 |
25 |
▶ | else if (od && *od && SvOK(*od) && !SvROK(*od)) { |
Branch: 4 branches
| Branch | True | False | Total |
| else if (od && *od && SvOK(*od) && !SvROK(*od)) { |
3 |
22 |
25 |
| 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 |
|
| 114 |
3 |
| STRLEN ol; const char *o = SvPV_const(*od, ol); |
| 115 |
3 |
▶ | if (ol == 3 && memEQ(o, "403", 3)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ol == 3 && memEQ(o, "403", 3)) |
3 |
0 |
3 |
| if (ol == 3 && memEQ(o, "403", 3)) |
0 |
3 |
3 |
|
| 116 |
3 |
| return pauth_triplet(aTHX_ 403, "application/json", |
| 117 |
|
| PAUTH_403_BODY, sizeof(PAUTH_403_BODY) - 1); |
| 118 |
3 |
▶ | if (ol == 3 && memEQ(o, "404", 3)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (ol == 3 && memEQ(o, "404", 3)) |
3 |
0 |
3 |
| if (ol == 3 && memEQ(o, "404", 3)) |
3 |
0 |
3 |
|
| 119 |
3 |
| return pauth_triplet(aTHX_ 404, "application/json", |
| 120 |
|
| PAUTH_404_BODY, sizeof(PAUTH_404_BODY) - 1); |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
|
| { /* negotiate: an Accept that wants text/html gets the login redirect */ |
| 124 |
22 |
| SV *env = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_ENV); |
| 125 |
22 |
▶ | HV *eh = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| HV *eh = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) |
22 |
0 |
22 |
| HV *eh = (env && SvROK(env) && SvTYPE(SvRV(env)) == SVt_PVHV) |
22 |
0 |
22 |
|
| 126 |
44 |
▶ | ? (HV *)SvRV(env) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (HV *)SvRV(env) : NULL; |
22 |
0 |
22 |
|
| 127 |
22 |
▶ | SV **ac = eh ? hv_fetchs(eh, "HTTP_ACCEPT", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **ac = eh ? hv_fetchs(eh, "HTTP_ACCEPT", 0) : NULL; |
22 |
0 |
22 |
|
| 128 |
22 |
| int wants_html = 0; |
| 129 |
22 |
▶ | if (ac && *ac && SvOK(*ac) && SvCUR(*ac)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (ac && *ac && SvOK(*ac) && SvCUR(*ac)) { |
19 |
3 |
22 |
| 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 |
|
| 130 |
|
| pa_range ranges[PA_RANGE_MAX]; |
| 131 |
19 |
| STRLEN al; const char *a = SvPV_const(*ac, al); |
| 132 |
19 |
| int nr = pa_parse(a, al, ranges, PA_RANGE_MAX); |
| 133 |
19 |
| int q = 0, order = 0; |
| 134 |
19 |
| int spec = pa_match(aTHX_ ranges, nr, "text", 4, "html", 4, |
| 135 |
|
| &q, &order); |
| 136 |
19 |
▶ | wants_html = spec > 0 && q > 0; |
Branch: 2 branches
| Branch | True | False | Total |
| wants_html = spec > 0 && q > 0; |
6 |
13 |
19 |
| wants_html = spec > 0 && q > 0; |
6 |
0 |
6 |
|
| 137 |
|
| } |
| 138 |
22 |
▶ | if (wants_html) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wants_html) { |
6 |
16 |
22 |
|
| 139 |
|
| STRLEN ll; |
| 140 |
6 |
| const char *login = ps_cfg_str(aTHX_ cfg, "login_path", |
| 141 |
|
| "/login", &ll); |
| 142 |
6 |
| SV *loc = newSVpvn(login, ll); |
| 143 |
6 |
▶ | SV **pi = eh ? hv_fetchs(eh, "PATH_INFO", 0) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| SV **pi = eh ? hv_fetchs(eh, "PATH_INFO", 0) : NULL; |
6 |
0 |
6 |
|
| 144 |
6 |
| AV *hdr = newAV(), *b = newAV(), *resp = newAV(); |
| 145 |
6 |
▶ | if (pi && *pi && SvOK(*pi) && SvCUR(*pi) |
Branch: 4 branches
| Branch | True | False | Total |
| 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 |
|
| 146 |
6 |
▶ | && !(SvCUR(*pi) == ll && memEQ(SvPVX(*pi), login, ll))) { |
Branch: 2 branches
| Branch | True | False | Total |
| && !(SvCUR(*pi) == ll && memEQ(SvPVX(*pi), login, ll))) { |
0 |
6 |
6 |
| && !(SvCUR(*pi) == ll && memEQ(SvPVX(*pi), login, ll))) { |
0 |
0 |
0 |
|
| 147 |
6 |
| STRLEN pl; const char *p = SvPV_const(*pi, pl); |
| 148 |
6 |
| SV *esc = pauth_escape(aTHX_ p, pl); |
| 149 |
6 |
| sv_catpvs(loc, "?to="); |
| 150 |
6 |
| sv_catsv(loc, esc); |
| 151 |
6 |
| SvREFCNT_dec(esc); |
| 152 |
|
| } |
| 153 |
6 |
| av_push(hdr, newSVpvs("Location")); |
| 154 |
6 |
| av_push(hdr, loc); |
| 155 |
6 |
| av_push(hdr, newSVpvs("Content-Length")); |
| 156 |
6 |
| av_push(hdr, newSViv(0)); |
| 157 |
6 |
| av_push(b, newSVpvs("")); |
| 158 |
6 |
| av_push(resp, newSViv(302)); |
| 159 |
6 |
| av_push(resp, newRV_noinc((SV *)hdr)); |
| 160 |
6 |
| av_push(resp, newRV_noinc((SV *)b)); |
| 161 |
6 |
| return newRV_noinc((SV *)resp); |
| 162 |
|
| } |
| 163 |
|
| } |
| 164 |
16 |
| return pauth_triplet(aTHX_ 401, "application/json", |
| 165 |
|
| PAUTH_401_BODY, sizeof(PAUTH_401_BODY) - 1); |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
|
| /* The await seam, in C: a plain value passes through; an object with ->get |
| 169 |
|
| * is a future from a nonblocking model backend - a Punk::Future goes through |
| 170 |
|
| * $c->await (the loop-aware path), anything else through its own ->get. |
| 171 |
|
| * Returns +1. */ |
| 172 |
62 |
| static SV *pauth_await(pTHX_ SV *c, SV *v) { |
| 173 |
62 |
▶ | if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) |
62 |
0 |
62 |
| if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) |
55 |
7 |
62 |
| if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) |
2 |
53 |
55 |
| if (!(v && SvROK(v) && SvOBJECT(SvRV(v)) && pcx_can(aTHX_ v, "get"))) |
0 |
2 |
2 |
|
| 174 |
60 |
▶ | return newSVsv(v ? v : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| return newSVsv(v ? v : &PL_sv_undef); |
60 |
0 |
60 |
|
| 175 |
2 |
▶ | if (sv_derived_from(v, "Punk::Future")) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (sv_derived_from(v, "Punk::Future")) { |
0 |
2 |
2 |
|
| 176 |
|
| SV *argv[1], *r; |
| 177 |
0 |
| argv[0] = v; |
| 178 |
0 |
| r = pcx_call_meth(aTHX_ c, "await", argv, 1, 1); |
| 179 |
0 |
▶ | return r ? r : newSV(0); |
Branch: 1 branch
| Branch | True | False | Total |
| return r ? r : newSV(0); |
0 |
0 |
0 |
|
| 180 |
|
| } |
| 181 |
|
| { |
| 182 |
2 |
| SV *r = pcx_call_meth(aTHX_ v, "get", NULL, 0, 1); |
| 183 |
2 |
▶ | return r ? r : newSV(0); |
Branch: 1 branch
| Branch | True | False | Total |
| return r ? r : newSV(0); |
0 |
2 |
2 |
|
| 184 |
|
| } |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
|
| /* one field name out of the auth config's fields map (borrowed pointer) */ |
| 188 |
63 |
| static const char *pauth_field(pTHX_ HV *cfg, const char *name, |
| 189 |
|
| const char *def, STRLEN *lp) { |
| 190 |
63 |
| SV **f = hv_fetchs(cfg, "fields", 0); |
| 191 |
63 |
▶ | if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { |
63 |
0 |
63 |
| if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { |
63 |
0 |
63 |
| if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { |
63 |
0 |
63 |
| if (f && *f && SvROK(*f) && SvTYPE(SvRV(*f)) == SVt_PVHV) { |
63 |
0 |
63 |
|
| 192 |
63 |
| SV **e = hv_fetch((HV *)SvRV(*f), name, (I32)strlen(name), 0); |
| 193 |
126 |
▶ | if (e && *e && SvOK(*e)) return SvPV_const(*e, *lp); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) return SvPV_const(*e, *lp); |
63 |
0 |
63 |
| if (e && *e && SvOK(*e)) return SvPV_const(*e, *lp); |
63 |
0 |
63 |
| if (e && *e && SvOK(*e)) return SvPV_const(*e, *lp); |
63 |
0 |
63 |
|
| 194 |
|
| } |
| 195 |
0 |
| *lp = strlen(def); |
| 196 |
0 |
| return def; |
| 197 |
|
| } |
| 198 |
|
| |
| 199 |
|
| /* the model instance behind a config option ('model' / 'token_model'), |
| 200 |
|
| * croaking with the fix when the option was never given. Returns +1. */ |
| 201 |
46 |
| static SV *pauth_model(pTHX_ SV *c, HV *cfg, const char *opt, |
| 202 |
|
| const char *what) { |
| 203 |
46 |
| SV **mn = hv_fetch(cfg, opt, (I32)strlen(opt), 0); |
| 204 |
|
| SV *argv[1], *m; |
| 205 |
46 |
▶ | if (!(mn && *mn && SvOK(*mn))) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(mn && *mn && SvOK(*mn))) |
45 |
1 |
46 |
| if (!(mn && *mn && SvOK(*mn))) |
45 |
0 |
45 |
| if (!(mn && *mn && SvOK(*mn))) |
0 |
45 |
45 |
|
| 206 |
1 |
| croak("Punk: %s needs `auth %s => ...`", what, opt); |
| 207 |
45 |
| argv[0] = *mn; |
| 208 |
45 |
| m = pcx_call_meth(aTHX_ c, "model", argv, 1, 1); |
| 209 |
45 |
▶ | if (!m) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m) |
0 |
45 |
45 |
|
| 210 |
0 |
| croak("Punk: %s could not build the %s model", what, opt); |
| 211 |
45 |
| return m; |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
|
| /* the rows arrayref out of a search result (borrowed), or NULL */ |
| 215 |
17 |
| static AV *pauth_rows(pTHX_ SV *res) { |
| 216 |
|
| SV **r; |
| 217 |
17 |
▶ | if (!(res && SvROK(res) && SvTYPE(SvRV(res)) == SVt_PVHV)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 218 |
17 |
| r = hv_fetchs((HV *)SvRV(res), "rows", 0); |
| 219 |
17 |
▶ | return (r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVAV) |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 220 |
34 |
▶ | ? (AV *)SvRV(*r) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(*r) : NULL; |
17 |
0 |
17 |
|
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
|
| /* the stash's punk.auth slot, autovivified (borrowed) */ |
| 224 |
30 |
| static HV *pauth_memo(pTHX_ SV *c) { |
| 225 |
30 |
| HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c)); |
| 226 |
30 |
| SV **slot = hv_fetchs(stash, "punk.auth", 0); |
| 227 |
|
| HV *pa; |
| 228 |
30 |
▶ | if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVHV) |
1 |
29 |
30 |
| 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 |
|
| 229 |
1 |
| return (HV *)SvRV(*slot); |
| 230 |
29 |
| pa = newHV(); |
| 231 |
29 |
| (void)hv_stores(stash, "punk.auth", newRV_noinc((SV *)pa)); |
| 232 |
29 |
| return pa; |
| 233 |
|
| } |
| 234 |
|
| |
| 235 |
|
| /* The signed-in user's row, loaded once per request through the configured |
| 236 |
|
| * model and memoized in the stash - the memo records "loaded, and it was |
| 237 |
|
| * undef" as an undef entry, so a stale session costs one lookup, not one per |
| 238 |
|
| * call. Returns +1 or NULL. */ |
| 239 |
30 |
| static SV *pauth_current_user(pTHX_ SV *c, HV *cfg) { |
| 240 |
30 |
| HV *pa = pauth_memo(aTHX_ c); |
| 241 |
30 |
| SV **u = hv_fetchs(pa, "user", 0); |
| 242 |
30 |
| SV *id, *user = NULL; |
| 243 |
30 |
▶ | if (u && *u) return SvOK(*u) ? newSVsv(*u) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (u && *u) return SvOK(*u) ? newSVsv(*u) : NULL; |
1 |
29 |
30 |
| if (u && *u) return SvOK(*u) ? newSVsv(*u) : NULL; |
1 |
0 |
1 |
| if (u && *u) return SvOK(*u) ? newSVsv(*u) : NULL; |
1 |
0 |
1 |
|
| 244 |
|
| |
| 245 |
29 |
| id = pauth_session_id(aTHX_ c, cfg); |
| 246 |
29 |
▶ | if (id) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (id) { |
28 |
1 |
29 |
|
| 247 |
28 |
| SV *m = pauth_model(aTHX_ c, cfg, "model", "current_user"); |
| 248 |
|
| STRLEN idl; |
| 249 |
28 |
| const char *idf = pauth_field(aTHX_ cfg, "id", "id", &idl); |
| 250 |
|
| SV *argv[2], *raw, *got; |
| 251 |
28 |
| argv[0] = sv_2mortal(newSVpvn(idf, idl)); |
| 252 |
28 |
| argv[1] = id; |
| 253 |
28 |
| raw = pcx_call_meth(aTHX_ m, "get", argv, 2, 1); |
| 254 |
28 |
| SvREFCNT_dec(m); |
| 255 |
28 |
| SvREFCNT_dec(id); |
| 256 |
28 |
▶ | got = pauth_await(aTHX_ c, raw ? raw : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| got = pauth_await(aTHX_ c, raw ? raw : &PL_sv_undef); |
28 |
0 |
28 |
|
| 257 |
28 |
▶ | if (raw) SvREFCNT_dec(raw); |
Branch: 1 branch
| Branch | True | False | Total |
| if (raw) SvREFCNT_dec(raw); |
28 |
0 |
28 |
|
| 258 |
28 |
▶ | if (got && SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV) user = got; |
Branch: 3 branches
| Branch | True | False | Total |
| if (got && SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV) user = got; |
28 |
0 |
28 |
| if (got && SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV) user = got; |
28 |
0 |
28 |
| if (got && SvROK(got) && SvTYPE(SvRV(got)) == SVt_PVHV) user = got; |
28 |
0 |
28 |
|
| 259 |
0 |
▶ | else if (got) SvREFCNT_dec(got); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (got) SvREFCNT_dec(got); |
0 |
0 |
0 |
|
| 260 |
|
| } |
| 261 |
29 |
▶ | (void)hv_stores(pa, "user", user ? newSVsv(user) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(pa, "user", user ? newSVsv(user) : newSV(0)); |
28 |
1 |
29 |
|
| 262 |
29 |
| return user; |
| 263 |
|
| } |
| 264 |
|
| |
| 265 |
|
| /* The role/verified half of a guard, reached only when the guard has |
| 266 |
|
| * options. The roles hook may return one name, a list, or an arrayref - a |
| 267 |
|
| * user can hold several roles. A required role in the rank ladder means |
| 268 |
|
| * "this or better"; one outside it is orthogonal and matches exactly. |
| 269 |
|
| * Returns a denial response (+1) or NULL to pass. */ |
| 270 |
19 |
| static SV *pauth_check(pTHX_ SV *c, HV *cfg, HV *opts) { |
| 271 |
19 |
| SV *user = pauth_current_user(aTHX_ c, cfg); |
| 272 |
|
| SV **od; |
| 273 |
19 |
▶ | if (!user) return pauth_denial(aTHX_ c, cfg, opts); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!user) return pauth_denial(aTHX_ c, cfg, opts); |
0 |
19 |
19 |
|
| 274 |
|
| |
| 275 |
19 |
| od = hv_fetchs(opts, "verified", 0); |
| 276 |
19 |
▶ | if (od && *od && SvTRUE(*od)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (od && *od && SvTRUE(*od)) { |
12 |
7 |
19 |
| if (od && *od && SvTRUE(*od)) { |
12 |
0 |
12 |
| if (od && *od && SvTRUE(*od)) { |
12 |
0 |
12 |
|
| 277 |
|
| STRLEN vl; |
| 278 |
12 |
| const char *vf = pauth_field(aTHX_ cfg, "verified", "verified", &vl); |
| 279 |
12 |
| SV **e = hv_fetch((HV *)SvRV(user), vf, (I32)vl, 0); |
| 280 |
12 |
▶ | if (!(e && *e && pauth_truthy(aTHX_ *e))) { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 281 |
8 |
| SvREFCNT_dec(user); |
| 282 |
8 |
| return pauth_denial(aTHX_ c, cfg, opts); |
| 283 |
|
| } |
| 284 |
|
| } |
| 285 |
|
| |
| 286 |
11 |
| od = hv_fetchs(opts, "role", 0); |
| 287 |
11 |
▶ | if (od && *od && SvOK(*od)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (od && *od && SvOK(*od)) { |
7 |
4 |
11 |
| if (od && *od && SvOK(*od)) { |
7 |
0 |
7 |
| if (od && *od && SvOK(*od)) { |
7 |
0 |
7 |
|
| 288 |
7 |
| SV **rl = hv_fetchs(cfg, "roles", 0); |
| 289 |
7 |
| SV **rk = hv_fetchs(cfg, "rank", 0); |
| 290 |
0 |
▶ | AV *rank = (rk && *rk && SvROK(*rk) |
Branch: 2 branches
| Branch | True | False | Total |
| AV *rank = (rk && *rk && SvROK(*rk) |
0 |
0 |
0 |
| AV *rank = (rk && *rk && SvROK(*rk) |
0 |
0 |
0 |
|
| 291 |
0 |
▶ | && SvTYPE(SvRV(*rk)) == SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(*rk)) == SVt_PVAV) |
0 |
0 |
0 |
|
| 292 |
7 |
▶ | ? (AV *)SvRV(*rk) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(*rk) : NULL; |
0 |
7 |
7 |
|
| 293 |
|
| static const char *const DEFAULT_RANK[] = |
| 294 |
|
| { "member", "admin", "owner" }; |
| 295 |
7 |
| AV *have = (AV *)sv_2mortal((SV *)newAV()); |
| 296 |
|
| STRLEN nl; |
| 297 |
7 |
| const char *need = SvPV_const(*od, nl); |
| 298 |
|
| SSize_t i, hn; |
| 299 |
7 |
| IV need_idx = -1; |
| 300 |
7 |
| int ok = 0; |
| 301 |
|
| |
| 302 |
7 |
▶ | if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { |
7 |
0 |
7 |
| if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { |
7 |
0 |
7 |
| if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { |
7 |
0 |
7 |
| if (!(rl && *rl && SvROK(*rl) && SvTYPE(SvRV(*rl)) == SVt_PVCV)) { |
0 |
7 |
7 |
|
| 303 |
0 |
| SvREFCNT_dec(user); |
| 304 |
0 |
| croak("Punk: auth_guard role checks need " |
| 305 |
|
| "`auth roles => sub { ... }`"); |
| 306 |
|
| } |
| 307 |
|
| { /* the hook, in list context */ |
| 308 |
7 |
| dSP; int count, j; |
| 309 |
7 |
| ENTER; SAVETMPS; |
| 310 |
7 |
▶ | PUSHMARK(SP); EXTEND(SP, 2); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
7 |
7 |
| PUSHMARK(SP); EXTEND(SP, 2); |
0 |
7 |
7 |
|
| 311 |
7 |
| PUSHs(c); PUSHs(user); PUTBACK; |
| 312 |
7 |
| count = call_sv(*rl, G_ARRAY); |
| 313 |
7 |
| SPAGAIN; |
| 314 |
16 |
▶ | for (j = 0; j < count; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < count; j++) { |
9 |
7 |
16 |
|
| 315 |
9 |
| SV *v = POPs; /* never inside a multi-eval macro */ |
| 316 |
9 |
| (void)av_store(have, count - 1 - j, newSVsv(v)); |
| 317 |
|
| } |
| 318 |
7 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
7 |
0 |
7 |
|
| 319 |
|
| } |
| 320 |
7 |
▶ | if (av_len(have) == 0) { /* one return that is an arrayref: flatten */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(have) == 0) { /* one return that is an arrayref: flatten */ |
5 |
2 |
7 |
|
| 321 |
5 |
| SV **h0 = av_fetch(have, 0, 0); |
| 322 |
5 |
▶ | if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { |
5 |
0 |
5 |
| if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { |
5 |
0 |
5 |
| if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { |
0 |
5 |
5 |
| if (h0 && *h0 && SvROK(*h0) && SvTYPE(SvRV(*h0)) == SVt_PVAV) { |
0 |
0 |
0 |
|
| 323 |
0 |
| AV *inner = (AV *)SvRV(*h0); |
| 324 |
0 |
| AV *flat = (AV *)sv_2mortal((SV *)newAV()); |
| 325 |
0 |
| SSize_t k, n2 = av_len(inner) + 1; |
| 326 |
0 |
▶ | for (k = 0; k < n2; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < n2; k++) { |
0 |
0 |
0 |
|
| 327 |
0 |
| SV **e = av_fetch(inner, k, 0); |
| 328 |
0 |
▶ | if (e && *e) av_push(flat, newSVsv(*e)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && *e) av_push(flat, newSVsv(*e)); |
0 |
0 |
0 |
| if (e && *e) av_push(flat, newSVsv(*e)); |
0 |
0 |
0 |
|
| 329 |
|
| } |
| 330 |
0 |
| have = flat; |
| 331 |
|
| } |
| 332 |
|
| } |
| 333 |
|
| |
| 334 |
|
| { /* where does the required role sit on the ladder? */ |
| 335 |
0 |
| SSize_t rn = rank ? av_len(rank) + 1 |
| 336 |
7 |
▶ | : (SSize_t)(sizeof(DEFAULT_RANK) |
Branch: 1 branch
| Branch | True | False | Total |
| : (SSize_t)(sizeof(DEFAULT_RANK) |
0 |
7 |
7 |
|
| 337 |
|
| / sizeof(*DEFAULT_RANK)); |
| 338 |
19 |
▶ | for (i = 0; i < rn; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < rn; i++) { |
17 |
2 |
19 |
|
| 339 |
|
| STRLEN xl; const char *x; |
| 340 |
17 |
▶ | if (rank) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rank) { |
0 |
17 |
17 |
|
| 341 |
0 |
| SV **e = av_fetch(rank, i, 0); |
| 342 |
0 |
▶ | if (!(e && *e && SvOK(*e))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(e && *e && SvOK(*e))) continue; |
0 |
0 |
0 |
| if (!(e && *e && SvOK(*e))) continue; |
0 |
0 |
0 |
| if (!(e && *e && SvOK(*e))) continue; |
0 |
0 |
0 |
|
| 343 |
0 |
| x = SvPV_const(*e, xl); |
| 344 |
|
| } |
| 345 |
17 |
| else { x = DEFAULT_RANK[i]; xl = strlen(x); } |
| 346 |
17 |
▶ | if (xl == nl && memEQ(x, need, nl)) { need_idx = (IV)i; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (xl == nl && memEQ(x, need, nl)) { need_idx = (IV)i; break; } |
10 |
7 |
17 |
| if (xl == nl && memEQ(x, need, nl)) { need_idx = (IV)i; break; } |
5 |
5 |
10 |
|
| 347 |
|
| } |
| 348 |
|
| } |
| 349 |
7 |
| hn = av_len(have) + 1; |
| 350 |
16 |
▶ | for (i = 0; i < hn && !ok; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < hn && !ok; i++) { |
9 |
7 |
16 |
| for (i = 0; i < hn && !ok; i++) { |
9 |
0 |
9 |
|
| 351 |
9 |
| SV **hp = av_fetch(have, i, 0); |
| 352 |
|
| STRLEN hl; const char *h; |
| 353 |
9 |
▶ | if (!(hp && *hp && SvOK(*hp))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(hp && *hp && SvOK(*hp))) continue; |
9 |
0 |
9 |
| if (!(hp && *hp && SvOK(*hp))) continue; |
9 |
0 |
9 |
| if (!(hp && *hp && SvOK(*hp))) continue; |
0 |
9 |
9 |
|
| 354 |
9 |
| h = SvPV_const(*hp, hl); |
| 355 |
9 |
▶ | if (need_idx < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (need_idx < 0) { |
3 |
6 |
9 |
|
| 356 |
|
| /* orthogonal: exact membership */ |
| 357 |
3 |
▶ | if (hl == nl && memEQ(h, need, nl)) ok = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (hl == nl && memEQ(h, need, nl)) ok = 1; |
2 |
1 |
3 |
| if (hl == nl && memEQ(h, need, nl)) ok = 1; |
1 |
1 |
2 |
|
| 358 |
|
| } |
| 359 |
|
| else { |
| 360 |
|
| /* ladder: any held role at or above the requirement */ |
| 361 |
0 |
| SSize_t rn = rank ? av_len(rank) + 1 |
| 362 |
6 |
▶ | : (SSize_t)(sizeof(DEFAULT_RANK) |
Branch: 1 branch
| Branch | True | False | Total |
| : (SSize_t)(sizeof(DEFAULT_RANK) |
0 |
6 |
6 |
|
| 363 |
|
| / sizeof(*DEFAULT_RANK)); |
| 364 |
|
| SSize_t j; |
| 365 |
14 |
▶ | for (j = need_idx; j < rn; j++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = need_idx; j < rn; j++) { |
10 |
4 |
14 |
|
| 366 |
|
| STRLEN xl; const char *x; |
| 367 |
10 |
▶ | if (rank) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rank) { |
0 |
10 |
10 |
|
| 368 |
0 |
| SV **e = av_fetch(rank, j, 0); |
| 369 |
0 |
▶ | if (!(e && *e && SvOK(*e))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(e && *e && SvOK(*e))) continue; |
0 |
0 |
0 |
| if (!(e && *e && SvOK(*e))) continue; |
0 |
0 |
0 |
| if (!(e && *e && SvOK(*e))) continue; |
0 |
0 |
0 |
|
| 370 |
0 |
| x = SvPV_const(*e, xl); |
| 371 |
|
| } |
| 372 |
10 |
| else { x = DEFAULT_RANK[j]; xl = strlen(x); } |
| 373 |
10 |
▶ | if (xl == hl && memEQ(x, h, hl)) { ok = 1; break; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (xl == hl && memEQ(x, h, hl)) { ok = 1; break; } |
6 |
4 |
10 |
| if (xl == hl && memEQ(x, h, hl)) { ok = 1; break; } |
2 |
4 |
6 |
|
| 374 |
|
| } |
| 375 |
|
| } |
| 376 |
|
| } |
| 377 |
7 |
▶ | if (!ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) { |
4 |
3 |
7 |
|
| 378 |
4 |
| SvREFCNT_dec(user); |
| 379 |
4 |
| return pauth_denial(aTHX_ c, cfg, opts); |
| 380 |
|
| } |
| 381 |
|
| { /* record what the guard learned */ |
| 382 |
3 |
| HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c)); |
| 383 |
3 |
| SV **a = hv_fetchs(stash, "auth", 0); |
| 384 |
|
| HV *ah; |
| 385 |
3 |
| AV *copy = newAV(); |
| 386 |
3 |
| SSize_t k, n2 = av_len(have) + 1; |
| 387 |
7 |
▶ | for (k = 0; k < n2; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < n2; k++) { |
4 |
3 |
7 |
|
| 388 |
4 |
| SV **e = av_fetch(have, k, 0); |
| 389 |
4 |
▶ | if (e && *e && SvOK(*e)) av_push(copy, newSVsv(*e)); |
Branch: 3 branches
| Branch | True | False | Total |
| if (e && *e && SvOK(*e)) av_push(copy, newSVsv(*e)); |
4 |
0 |
4 |
| if (e && *e && SvOK(*e)) av_push(copy, newSVsv(*e)); |
4 |
0 |
4 |
| if (e && *e && SvOK(*e)) av_push(copy, newSVsv(*e)); |
4 |
0 |
4 |
|
| 390 |
|
| } |
| 391 |
3 |
▶ | if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
3 |
0 |
3 |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
3 |
0 |
3 |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
3 |
0 |
3 |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
3 |
0 |
3 |
|
| 392 |
3 |
| ah = (HV *)SvRV(*a); |
| 393 |
|
| else { |
| 394 |
0 |
| ah = newHV(); |
| 395 |
0 |
| (void)hv_stores(stash, "auth", newRV_noinc((SV *)ah)); |
| 396 |
|
| } |
| 397 |
3 |
| (void)hv_stores(ah, "roles", newRV_noinc((SV *)copy)); |
| 398 |
|
| } |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
|
| { /* the loaded user rides along for the handler */ |
| 402 |
7 |
| HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c)); |
| 403 |
7 |
| SV **a = hv_fetchs(stash, "auth", 0); |
| 404 |
|
| HV *ah; |
| 405 |
7 |
▶ | if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
7 |
0 |
7 |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
7 |
0 |
7 |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
7 |
0 |
7 |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
7 |
0 |
7 |
|
| 406 |
7 |
| ah = (HV *)SvRV(*a); |
| 407 |
|
| else { |
| 408 |
0 |
| ah = newHV(); |
| 409 |
0 |
| (void)hv_stores(stash, "auth", newRV_noinc((SV *)ah)); |
| 410 |
|
| } |
| 411 |
7 |
| (void)hv_stores(ah, "user", newSVsv(user)); |
| 412 |
|
| } |
| 413 |
7 |
| SvREFCNT_dec(user); |
| 414 |
7 |
| return NULL; |
| 415 |
|
| } |
| 416 |
|
| |
| 417 |
|
| /* record the signed-in id in the stash's auth slot (the Open::API checker |
| 418 |
|
| * convention: authenticated facts live under stash->{auth}) */ |
| 419 |
25 |
| static void pauth_stash_id(pTHX_ SV *c, SV *id) { |
| 420 |
25 |
| HV *stash = ps_stash(aTHX_ pcx_av(aTHX_ c)); |
| 421 |
25 |
| SV **a = hv_fetchs(stash, "auth", 0); |
| 422 |
|
| HV *ah; |
| 423 |
25 |
▶ | if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (a && *a && SvROK(*a) && SvTYPE(SvRV(*a)) == SVt_PVHV) |
0 |
25 |
25 |
| 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 |
|
| 424 |
0 |
| ah = (HV *)SvRV(*a); |
| 425 |
|
| else { |
| 426 |
25 |
| ah = newHV(); |
| 427 |
25 |
| (void)hv_stores(stash, "auth", newRV_noinc((SV *)ah)); |
| 428 |
|
| } |
| 429 |
25 |
| (void)hv_stores(ah, "user_id", newSVsv(id)); |
| 430 |
25 |
| } |
| 431 |
|
| |
| 432 |
|
| /* The guard closure: capture [ $app, \%opts ]. Body: no session id is a |
| 433 |
|
| * denial; a bare guard passes on the id alone; role/verified options load |
| 434 |
|
| * the user through the Perl half. */ |
| 435 |
|
| XS_INTERNAL(pauth_guard_cb); |
| 436 |
39 |
| XS_INTERNAL(pauth_guard_cb) { |
| 437 |
39 |
| dXSARGS; |
| 438 |
39 |
| AV *cap = punk_clos_cap(aTHX_ cv); |
| 439 |
|
| SV *app_sv, *opts_sv, *c, *id; |
| 440 |
39 |
| HV *cfg = NULL, *opts = NULL; |
| 441 |
39 |
▶ | if (!cap || items < 1) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!cap || items < 1) XSRETURN_EMPTY; |
39 |
0 |
39 |
| if (!cap || items < 1) XSRETURN_EMPTY; |
0 |
39 |
39 |
|
| 442 |
39 |
| app_sv = *av_fetch(cap, 0, 0); |
| 443 |
39 |
| opts_sv = *av_fetch(cap, 1, 0); |
| 444 |
39 |
| c = ST(0); |
| 445 |
|
| |
| 446 |
39 |
▶ | if (app_sv && SvROK(app_sv) && SvTYPE(SvRV(app_sv)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (app_sv && SvROK(app_sv) && SvTYPE(SvRV(app_sv)) == SVt_PVHV) { |
39 |
0 |
39 |
| if (app_sv && SvROK(app_sv) && SvTYPE(SvRV(app_sv)) == SVt_PVHV) { |
39 |
0 |
39 |
| if (app_sv && SvROK(app_sv) && SvTYPE(SvRV(app_sv)) == SVt_PVHV) { |
39 |
0 |
39 |
|
| 447 |
39 |
| SV **s = hv_fetchs((HV *)SvRV(app_sv), K_AUTH, 0); |
| 448 |
39 |
▶ | if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
39 |
0 |
39 |
| if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
39 |
0 |
39 |
| if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
39 |
0 |
39 |
| if (s && *s && SvROK(*s) && SvTYPE(SvRV(*s)) == SVt_PVHV) |
39 |
0 |
39 |
|
| 449 |
39 |
| cfg = (HV *)SvRV(*s); |
| 450 |
|
| } |
| 451 |
39 |
▶ | if (!cfg) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cfg) |
0 |
39 |
39 |
|
| 452 |
0 |
| croak("Punk: auth_guard needs the auth keyword (add `auth ...`)"); |
| 453 |
39 |
▶ | if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) |
39 |
0 |
39 |
| if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) |
39 |
0 |
39 |
| if (opts_sv && SvROK(opts_sv) && SvTYPE(SvRV(opts_sv)) == SVt_PVHV) |
39 |
0 |
39 |
|
| 454 |
39 |
| opts = (HV *)SvRV(opts_sv); |
| 455 |
|
| |
| 456 |
39 |
| id = pauth_session_id(aTHX_ c, cfg); |
| 457 |
39 |
▶ | if (!id) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!id) { |
14 |
25 |
39 |
|
| 458 |
14 |
| ST(0) = sv_2mortal(pauth_denial(aTHX_ c, cfg, opts)); |
| 459 |
14 |
| XSRETURN(1); |
| 460 |
|
| } |
| 461 |
25 |
| pauth_stash_id(aTHX_ c, id); |
| 462 |
25 |
| SvREFCNT_dec(id); |
| 463 |
|
| |
| 464 |
25 |
▶ | if (opts && HvUSEDKEYS(opts)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (opts && HvUSEDKEYS(opts)) { |
25 |
0 |
25 |
| if (opts && HvUSEDKEYS(opts)) { |
0 |
25 |
25 |
| if (opts && HvUSEDKEYS(opts)) { |
19 |
6 |
25 |
|
| 465 |
19 |
| SV *r = pauth_check(aTHX_ c, cfg, opts); |
| 466 |
19 |
▶ | if (r) { ST(0) = sv_2mortal(r); XSRETURN(1); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) { ST(0) = sv_2mortal(r); XSRETURN(1); } |
12 |
7 |
19 |
|
| 467 |
|
| } |
| 468 |
13 |
| XSRETURN_EMPTY; |
| 469 |
|
| } |
| 470 |
|
| |
| 471 |
|
| #endif /* PUNK_AUTH_H */ |