| 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_EXCEPTIONS_HPP |
| 24 |
#define CPP_JWT_EXCEPTIONS_HPP |
| 25 |
|
| 26 |
#include <new> |
| 27 |
#include <string> |
| 28 |
|
| 29 |
namespace jwt { |
| 30 |
|
| 31 |
/** |
| 32 |
* Exception for allocation related failures in the |
| 33 |
* OpenSSL C APIs. |
| 34 |
*/ |
| 35 |
class MemoryAllocationException final: public std::bad_alloc |
| 36 |
{ |
| 37 |
public: |
| 38 |
/** |
| 39 |
* Construct MemoryAllocationException from a |
| 40 |
* string literal. |
| 41 |
*/ |
| 42 |
template <size_t N> |
| 43 |
MemoryAllocationException(const char(&msg)[N]) |
| 44 |
: msg_(&msg[0]) |
| 45 |
{ |
| 46 |
} |
| 47 |
|
| 48 |
virtual const char* what() const noexcept override |
| 49 |
{ |
| 50 |
return msg_; |
| 51 |
} |
| 52 |
|
| 53 |
private: |
| 54 |
const char* msg_ = nullptr; |
| 55 |
}; |
| 56 |
|
| 57 |
/** |
| 58 |
* Exception thrown for failures in OpenSSL |
| 59 |
* APIs while signing. |
| 60 |
*/ |
| 61 |
class SigningError : public std::runtime_error |
| 62 |
{ |
| 63 |
public: |
| 64 |
/** |
| 65 |
*/ |
| 66 |
SigningError(std::string msg) |
| 67 |
: std::runtime_error(std::move(msg)) |
| 68 |
{ |
| 69 |
} |
| 70 |
}; |
| 71 |
|
| 72 |
/** |
| 73 |
* Exception thrown for decode related errors. |
| 74 |
*/ |
| 75 |
class DecodeError: public std::runtime_error |
| 76 |
{ |
| 77 |
public: |
| 78 |
/** |
| 79 |
*/ |
| 80 |
DecodeError(std::string msg) |
| 81 |
: std::runtime_error(std::move(msg)) |
| 82 |
{ |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
/** |
| 87 |
* A derived decode error for signature format |
| 88 |
* error. |
| 89 |
*/ |
| 90 |
class SignatureFormatError final : public DecodeError |
| 91 |
{ |
| 92 |
public: |
| 93 |
/** |
| 94 |
*/ |
| 95 |
SignatureFormatError(std::string msg) |
| 96 |
: DecodeError(std::move(msg)) |
| 97 |
{ |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
/** |
| 102 |
* A derived decode error for Key argument not present |
| 103 |
* error. Only thrown if the algorithm set is not NONE. |
| 104 |
*/ |
| 105 |
class KeyNotPresentError final : public DecodeError |
| 106 |
{ |
| 107 |
public: |
| 108 |
/** |
| 109 |
*/ |
| 110 |
KeyNotPresentError(std::string msg) |
| 111 |
: DecodeError(std::move(msg)) |
| 112 |
{ |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* Base class exception for all kinds of verification errors. |
| 119 |
* Verification errors are thrown only when the verify |
| 120 |
* decode parameter is set to true. |
| 121 |
*/ |
| 122 |
class VerificationError : public std::runtime_error |
| 123 |
{ |
| 124 |
public: |
| 125 |
/** |
| 126 |
*/ |
| 127 |
VerificationError(std::string msg) |
| 128 |
: std::runtime_error(std::move(msg)) |
| 129 |
{ |
| 130 |
} |
| 131 |
}; |
| 132 |
|
| 133 |
/** |
| 134 |
* Derived from VerificationError. |
| 135 |
* Thrown when the algorithm decoded in the header |
| 136 |
* is incorrect. |
| 137 |
*/ |
| 138 |
class InvalidAlgorithmError final: public VerificationError |
| 139 |
{ |
| 140 |
public: |
| 141 |
/** |
| 142 |
*/ |
| 143 |
InvalidAlgorithmError(std::string msg) |
| 144 |
: VerificationError(std::move(msg)) |
| 145 |
{ |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
/** |
| 150 |
* Derived from VerificationError. |
| 151 |
* Thrown when the token is expired at the |
| 152 |
* time of decoding. |
| 153 |
*/ |
| 154 |
class TokenExpiredError final: public VerificationError |
| 155 |
{ |
| 156 |
public: |
| 157 |
/** |
| 158 |
*/ |
| 159 |
TokenExpiredError(std::string msg) |
| 160 |
: VerificationError(std::move(msg)) |
| 161 |
{ |
| 162 |
} |
| 163 |
}; |
| 164 |
|
| 165 |
/** |
| 166 |
* Derived from VerificationError. |
| 167 |
* Thrown when the issuer claim does not match |
| 168 |
* with the one provided as part of decode argument. |
| 169 |
*/ |
| 170 |
class InvalidIssuerError final: public VerificationError |
| 171 |
{ |
| 172 |
public: |
| 173 |
/** |
| 174 |
*/ |
| 175 |
InvalidIssuerError(std::string msg) |
| 176 |
: VerificationError(std::move(msg)) |
| 177 |
{ |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
/** |
| 182 |
* Derived from VerificationError. |
| 183 |
* Thrown when the audience claim does not match |
| 184 |
* with the one provided as part of decode argument. |
| 185 |
*/ |
| 186 |
class InvalidAudienceError final: public VerificationError |
| 187 |
{ |
| 188 |
public: |
| 189 |
/** |
| 190 |
*/ |
| 191 |
InvalidAudienceError(std::string msg) |
| 192 |
: VerificationError(std::move(msg)) |
| 193 |
{ |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
/** |
| 198 |
* Derived from VerificationError. |
| 199 |
* Thrown when the subject claim does not match |
| 200 |
* with the one provided as part of decode argument. |
| 201 |
*/ |
| 202 |
class InvalidSubjectError final: public VerificationError |
| 203 |
{ |
| 204 |
public: |
| 205 |
/** |
| 206 |
*/ |
| 207 |
InvalidSubjectError(std::string msg) |
| 208 |
: VerificationError(std::move(msg)) |
| 209 |
{ |
| 210 |
} |
| 211 |
}; |
| 212 |
|
| 213 |
/** |
| 214 |
* Derived from VerificationError. |
| 215 |
* Thrown when verify_iat parameter is passed to |
| 216 |
* decode and IAT is not present. |
| 217 |
*/ |
| 218 |
class InvalidIATError final: public VerificationError |
| 219 |
{ |
| 220 |
public: |
| 221 |
/** |
| 222 |
*/ |
| 223 |
InvalidIATError(std::string msg) |
| 224 |
: VerificationError(std::move(msg)) |
| 225 |
{ |
| 226 |
} |
| 227 |
}; |
| 228 |
|
| 229 |
/** |
| 230 |
* Derived from VerificationError. |
| 231 |
* Thrown when validate_jti is asked for |
| 232 |
* in decode and jti claim is not present. |
| 233 |
*/ |
| 234 |
class InvalidJTIError final: public VerificationError |
| 235 |
{ |
| 236 |
public: |
| 237 |
/** |
| 238 |
*/ |
| 239 |
InvalidJTIError(std::string msg) |
| 240 |
: VerificationError(std::move(msg)) |
| 241 |
{ |
| 242 |
} |
| 243 |
}; |
| 244 |
|
| 245 |
/** |
| 246 |
* Derived from VerificationError. |
| 247 |
* Thrown when the token is decoded at a time before |
| 248 |
* as specified in the `nbf` claim. |
| 249 |
*/ |
| 250 |
class ImmatureSignatureError final: public VerificationError |
| 251 |
{ |
| 252 |
public: |
| 253 |
/** |
| 254 |
*/ |
| 255 |
ImmatureSignatureError(std::string msg) |
| 256 |
: VerificationError(std::move(msg)) |
| 257 |
{ |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
/** |
| 262 |
* Derived from VerificationError. |
| 263 |
* Thrown when the signature does not match in the verification process. |
| 264 |
*/ |
| 265 |
class InvalidSignatureError final: public VerificationError |
| 266 |
{ |
| 267 |
public: |
| 268 |
/** |
| 269 |
*/ |
| 270 |
InvalidSignatureError(std::string msg) |
| 271 |
: VerificationError(std::move(msg)) |
| 272 |
{ |
| 273 |
} |
| 274 |
}; |
| 275 |
|
| 276 |
class InvalidKeyError final: public VerificationError |
| 277 |
{ |
| 278 |
public: |
| 279 |
/** |
| 280 |
*/ |
| 281 |
InvalidKeyError(std::string msg) |
| 282 |
: VerificationError(std::move(msg)) |
| 283 |
{ |
| 284 |
} |
| 285 |
}; |
| 286 |
|
| 287 |
/** |
| 288 |
* Derived from VerificationError. |
| 289 |
* Thrown when there type expectation mismatch |
| 290 |
* while verifying the values of registered claim names. |
| 291 |
*/ |
| 292 |
class TypeConversionError final: public VerificationError |
| 293 |
{ |
| 294 |
public: |
| 295 |
/** |
| 296 |
*/ |
| 297 |
TypeConversionError(std::string msg) |
| 298 |
: VerificationError(std::move(msg)) |
| 299 |
{ |
| 300 |
} |
| 301 |
}; |
| 302 |
|
| 303 |
} // END namespace jwt |
| 304 |
|
| 305 |
#endif |