include/psaml/psaml_error.h

filter: Statementstmt 100% 15 / 15 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.7% 18 / 19 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/psaml/psaml_xml.h ↑ Summary include/psaml/psaml_b64.h »
1 #ifndef PSAML_ERROR_H
2 #define PSAML_ERROR_H
3  
4 /* Every refusal in this dist throws a blessed Punk::SAML::Error with a
5 * `code` and a `message`.
6 *
7 * The code is the interface. The route logs it, the tests assert on it,
8 * the POD lists it, and an operator reading a log at two in the morning
9 * matches on it. A string message is none of those things: it gets
10 * reworded, and every reword breaks somebody.
11 *
12 * The message is for the log and never reaches the browser. A SAML
13 * verifier that tells the far side WHY it refused is telling an attacker
14 * which check to work on next, so the route answers with one status and
15 * one code of its own, and everything below stays in the log.
16 *
17 * Throwing a blessed object from C is croak_sv territory, and the shim
18 * for it is in psaml_compat.h with the reason it is written the way it
19 * is. t/00-load.t throws one on purpose so the shim is exercised on
20 * every perl the suite runs on, rather than first being reached inside a
21 * phase-6 check on a smoker. */
22  
23 #include "psaml_compat.h"
24  
25 /* The codes. Every one of these is documented in Punk::SAML::Error's POD
26 * and asserted somewhere in t/. Add here and there together, never one
27 * without the other.
28 *
29 * They are strings rather than an enum because the code crosses into
30 * Perl, where an integer would have to be translated back, and the
31 * translation table would be a third place to keep in step. */
32 #define PSAML_E_XML_PARSE "xml_parse"
33 #define PSAML_E_XML_SHAPE "xml_shape"
34 #define PSAML_E_BAD_BASE64 "bad_base64"
35 #define PSAML_E_BAD_DATETIME "bad_datetime"
36 #define PSAML_E_NO_SIGNATURE "no_signature"
37 #define PSAML_E_BAD_SIGNATURE "bad_signature"
38 #define PSAML_E_BAD_DIGEST "bad_digest"
39 #define PSAML_E_ALG_REFUSED "alg_refused"
40 #define PSAML_E_UNKNOWN_ISSUER "unknown_issuer"
41 #define PSAML_E_NO_KEY "no_key"
42 #define PSAML_E_EXPIRED "expired"
43 #define PSAML_E_NOT_YET_VALID "not_yet_valid"
44 #define PSAML_E_BAD_AUDIENCE "bad_audience"
45 #define PSAML_E_BAD_DESTINATION "bad_destination"
46 #define PSAML_E_BAD_IN_RESPONSE "bad_in_response_to"
47 #define PSAML_E_REPLAY "replay"
48 #define PSAML_E_UNSOLICITED "unsolicited"
49 #define PSAML_E_ENCRYPTED "encrypted_assertion"
50 #define PSAML_E_STATUS "status"
51 #define PSAML_E_CONFIG "config"
52  
53 /* Builds the exception object: a blessed hashref with code and message.
54 * Returns a mortal, because the only thing done with it is throwing it
55 * and a leak on the throw path is a leak per failed login. */
56 361 PERL_STATIC_INLINE SV *psaml_error_new(pTHX_ const char *code, SV *message) {
57 361 HV *hv = newHV();
58 SV *rv;
59 361 (void)hv_stores(hv, "code", newSVpv(code, 0));
60 361 ▶ (void)hv_stores(hv, "message", message ? message : newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(hv, "message", message ? message : newSVpvs("")); 0 361 361
61 361 rv = sv_2mortal(newRV_noinc((SV *)hv));
62 361 sv_bless(rv, gv_stashpvs("Punk::SAML::Error", GV_ADD));
63 361 return rv;
64 }
65  
66 /* Throws. Never returns. The message is built here rather than by the
67 * caller so that a caller with nothing to add can pass NULL and still
68 * get an object with the right code. */
69 336 static void psaml_throw(pTHX_ const char *code, const char *fmt, ...) {
70 336 SV *msg = newSVpvs("");
71 336 ▶ if (fmt) {
Branch: 1 branch
BranchTrueFalseTotal
if (fmt) { 335 1 336
72 va_list args;
73 335 va_start(args, fmt);
74 335 sv_vcatpvf(msg, fmt, &args);
75 335 va_end(args);
76 }
77 else {
78 1 sv_setpv(msg, code);
79 }
80 336 croak_sv(psaml_error_new(aTHX_ code, msg));
81 }
82  
83 #endif /* PSAML_ERROR_H */
« include/psaml/psaml_xml.h ↑ Summary include/psaml/psaml_b64.h »