Debian Codegraph @

include/jwt/impl/error_codes.ipp

raw
1 /*
2 Copyright (c) 2017 Arun Muralidharan
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #ifndef CPP_JWT_ERROR_CODES_IPP
24 #define CPP_JWT_ERROR_CODES_IPP
25
26 namespace jwt {
27 // Anonymous namespace
28 namespace {
29
30 /**
31 */
32 struct AlgorithmErrCategory: std::error_category
33 {
34 const char* name() const noexcept override
35 {
36 return "algorithms";
37 }
38
39 std::string message(int ev) const override
40 {
41 switch (static_cast<AlgorithmErrc>(ev))
42 {
43 case AlgorithmErrc::SigningErr:
44 return "signing failed";
45 case AlgorithmErrc::VerificationErr:
46 return "verification failed";
47 case AlgorithmErrc::KeyNotFoundErr:
48 return "key not provided";
49 case AlgorithmErrc::NoneAlgorithmUsed:
50 return "none algorithm used";
51 case AlgorithmErrc::InvalidKeyErr:
52 return "invalid key";
53 };
54 return "unknown algorithm error";
55 }
56 };
57
58 /**
59 */
60 struct DecodeErrorCategory: std::error_category
61 {
62 const char* name() const noexcept override
63 {
64 return "decode";
65 }
66
67 std::string message(int ev) const override
68 {
69 switch (static_cast<DecodeErrc>(ev))
70 {
71 case DecodeErrc::EmptyAlgoList:
72 return "empty algorithm list";
73 case DecodeErrc::SignatureFormatError:
74 return "signature format is incorrect";
75 case DecodeErrc::AlgHeaderMiss:
76 return "missing algorithm header";
77 case DecodeErrc::TypHeaderMiss:
78 return "missing type header";
79 case DecodeErrc::TypMismatch:
80 return "type mismatch";
81 case DecodeErrc::JsonParseError:
82 return "json parse failed";
83 case DecodeErrc::DuplClaims:
84 return "duplicate claims";
85 case DecodeErrc::KeyNotPresent:
86 return "key not present";
87 case DecodeErrc::KeyNotRequiredForNoneAlg:
88 return "key not required for NONE algorithm";
89 };
90 return "unknown decode error";
91 }
92 };
93
94 /**
95 */
96 struct VerificationErrorCategory: std::error_category
97 {
98 const char* name() const noexcept override
99 {
100 return "verification";
101 }
102
103 std::string message(int ev) const override
104 {
105 switch (static_cast<VerificationErrc>(ev))
106 {
107 case VerificationErrc::InvalidAlgorithm:
108 return "invalid algorithm";
109 case VerificationErrc::TokenExpired:
110 return "token expired";
111 case VerificationErrc::InvalidIssuer:
112 return "invalid issuer";
113 case VerificationErrc::InvalidSubject:
114 return "invalid subject";
115 case VerificationErrc::InvalidAudience:
116 return "invalid audience";
117 case VerificationErrc::InvalidIAT:
118 return "invalid iat";
119 case VerificationErrc::InvalidJTI:
120 return "invalid jti";
121 case VerificationErrc::ImmatureSignature:
122 return "immature signature";
123 case VerificationErrc::InvalidSignature:
124 return "invalid signature";
125 case VerificationErrc::TypeConversionError:
126 return "type conversion error";
127 case VerificationErrc::AlgoConfusionAttack:
128 return "algo confusion attack possibility";
129 };
130 return "unknown verification error";
131 }
132 };
133
134 // Create global object for the error categories
135 const AlgorithmErrCategory theAlgorithmErrCategory {};
136
137 const DecodeErrorCategory theDecodeErrorCategory {};
138
139 const VerificationErrorCategory theVerificationErrorCategory {};
140
141 }
142
143
144 // Create the AlgorithmErrc error code
145 inline std::error_code make_error_code(AlgorithmErrc err)
146 {
147 return { static_cast<int>(err), theAlgorithmErrCategory };
148 }
149
150 inline std::error_code make_error_code(DecodeErrc err)
151 {
152 return { static_cast<int>(err), theDecodeErrorCategory };
153 }
154
155 inline std::error_code make_error_code(VerificationErrc err)
156 {
157 return { static_cast<int>(err), theVerificationErrorCategory };
158 }
159
160 } // END namespace jwt
161
162 #endif