| 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_HPP |
| 24 |
#define CPP_JWT_ERROR_CODES_HPP |
| 25 |
|
| 26 |
#include <system_error> |
| 27 |
|
| 28 |
namespace jwt { |
| 29 |
/** |
| 30 |
* All the algorithm errors |
| 31 |
*/ |
| 32 |
enum class AlgorithmErrc |
| 33 |
{ |
| 34 |
SigningErr = 1, |
| 35 |
VerificationErr, |
| 36 |
KeyNotFoundErr, |
| 37 |
InvalidKeyErr, |
| 38 |
NoneAlgorithmUsed, // Not an actual error! |
| 39 |
}; |
| 40 |
|
| 41 |
/** |
| 42 |
* Algorithm error conditions |
| 43 |
* TODO: Remove it or use it! |
| 44 |
*/ |
| 45 |
enum class AlgorithmFailureSource |
| 46 |
{ |
| 47 |
}; |
| 48 |
|
| 49 |
/** |
| 50 |
* Decode error conditions |
| 51 |
*/ |
| 52 |
enum class DecodeErrc |
| 53 |
{ |
| 54 |
// No algorithms provided in decode API |
| 55 |
EmptyAlgoList = 1, |
| 56 |
// The JWT signature has incorrect format |
| 57 |
SignatureFormatError, |
| 58 |
// The JSON library failed to parse |
| 59 |
JsonParseError, |
| 60 |
// Algorithm field in header is missing |
| 61 |
AlgHeaderMiss, |
| 62 |
// Type field in header is missing |
| 63 |
TypHeaderMiss, |
| 64 |
// Unexpected type field value |
| 65 |
TypMismatch, |
| 66 |
// Found duplicate claims |
| 67 |
DuplClaims, |
| 68 |
// Key/Secret not passed as decode argument |
| 69 |
KeyNotPresent, |
| 70 |
// Key/secret passed as argument for NONE algorithm. |
| 71 |
// Not a hard error. |
| 72 |
KeyNotRequiredForNoneAlg, |
| 73 |
}; |
| 74 |
|
| 75 |
/** |
| 76 |
* Errors handled during verification process. |
| 77 |
*/ |
| 78 |
enum class VerificationErrc |
| 79 |
{ |
| 80 |
//Algorithms provided does not match with header |
| 81 |
InvalidAlgorithm = 1, |
| 82 |
//Token is expired at the time of decoding |
| 83 |
TokenExpired, |
| 84 |
//The issuer specified does not match with payload |
| 85 |
InvalidIssuer, |
| 86 |
//The subject specified does not match with payload |
| 87 |
InvalidSubject, |
| 88 |
//The field IAT is not present or is of invalid type |
| 89 |
InvalidIAT, |
| 90 |
//Checks for the existence of JTI |
| 91 |
//if validate_jti is passed in decode |
| 92 |
InvalidJTI, |
| 93 |
//The audience specified does not match with payload |
| 94 |
InvalidAudience, |
| 95 |
//Decoded before nbf time |
| 96 |
ImmatureSignature, |
| 97 |
//Signature match error |
| 98 |
InvalidSignature, |
| 99 |
// Invalid value type used for known claims |
| 100 |
TypeConversionError, |
| 101 |
// Algorithm confusion attack detected |
| 102 |
AlgoConfusionAttack, |
| 103 |
}; |
| 104 |
|
| 105 |
/** |
| 106 |
*/ |
| 107 |
std::error_code make_error_code(AlgorithmErrc err); |
| 108 |
|
| 109 |
/** |
| 110 |
*/ |
| 111 |
std::error_code make_error_code(DecodeErrc err); |
| 112 |
|
| 113 |
/** |
| 114 |
*/ |
| 115 |
std::error_code make_error_code(VerificationErrc err); |
| 116 |
|
| 117 |
} // END namespace jwt |
| 118 |
|
| 119 |
|
| 120 |
/** |
| 121 |
* Make the custom enum classes as error code |
| 122 |
* adaptable. |
| 123 |
*/ |
| 124 |
namespace std |
| 125 |
{ |
| 126 |
template <> |
| 127 |
struct is_error_code_enum<jwt::AlgorithmErrc> : true_type {}; |
| 128 |
|
| 129 |
template <> |
| 130 |
struct is_error_code_enum<jwt::DecodeErrc>: true_type {}; |
| 131 |
|
| 132 |
template <> |
| 133 |
struct is_error_code_enum<jwt::VerificationErrc>: true_type {}; |
| 134 |
} |
| 135 |
|
| 136 |
#include "jwt/impl/error_codes.ipp" |
| 137 |
|
| 138 |
#endif |