1
// Copyright 2018 Google LLC
2
// Copyright Envoy Project Authors
3
// SPDX-License-Identifier: Apache-2.0
4

            
5
#include "source/common/jwt/status.h"
6

            
7
#include <iostream>
8
#include <map>
9

            
10
namespace Envoy {
11
namespace JwtVerify {
12

            
13
253
std::string getStatusString(Status status) {
14
253
  switch (status) {
15
78
  case Status::Ok:
16
78
    return "OK";
17
42
  case Status::JwtMissed:
18
42
    return "Jwt is missing";
19
2
  case Status::JwtNotYetValid:
20
2
    return "Jwt not yet valid";
21
39
  case Status::JwtExpired:
22
39
    return "Jwt is expired";
23
6
  case Status::JwtBadFormat:
24
6
    return "Jwt is not in the form of Header.Payload.Signature with two dots "
25
6
           "and 3 sections";
26
  case Status::JwtHeaderParseErrorBadBase64:
27
    return "Jwt header is an invalid Base64url encoded";
28
  case Status::JwtHeaderParseErrorBadJson:
29
    return "Jwt header is an invalid JSON";
30
  case Status::JwtHeaderBadAlg:
31
    return "Jwt header [alg] field is required and must be a string";
32
  case Status::JwtHeaderNotImplementedAlg:
33
    return "Jwt header [alg] is not supported";
34
  case Status::JwtHeaderBadKid:
35
    return "Jwt header [kid] field is not a string";
36
  case Status::JwtPayloadParseErrorBadBase64:
37
    return "Jwt payload is an invalid Base64url encoded";
38
  case Status::JwtEd25519SignatureWrongLength:
39
    return "Jwt ED25519 signature is wrong length";
40
  case Status::JwtPayloadParseErrorBadJson:
41
    return "Jwt payload is an invalid JSON";
42
  case Status::JwtPayloadParseErrorIssNotString:
43
    return "Jwt payload [iss] field is not a string";
44
  case Status::JwtPayloadParseErrorSubNotString:
45
    return "Jwt payload [sub] field is not a string";
46
  case Status::JwtPayloadParseErrorIatNotInteger:
47
    return "Jwt payload [iat] field is not an integer";
48
  case Status::JwtPayloadParseErrorIatOutOfRange:
49
    return "Jwt payload [iat] field is not a positive 64 bit integer";
50
  case Status::JwtPayloadParseErrorNbfNotInteger:
51
    return "Jwt payload [nbf] field is not an integer";
52
  case Status::JwtPayloadParseErrorNbfOutOfRange:
53
    return "Jwt payload [nbf] field is not a positive 64 bit integer";
54
  case Status::JwtPayloadParseErrorExpNotInteger:
55
    return "Jwt payload [exp] field is not an integer";
56
  case Status::JwtPayloadParseErrorExpOutOfRange:
57
    return "Jwt payload [exp] field is not a positive 64 bit integer";
58
  case Status::JwtPayloadParseErrorJtiNotString:
59
    return "Jwt payload [jti] field is not a string";
60
  case Status::JwtPayloadParseErrorAudNotString:
61
    return "Jwt payload [aud] field is not a string or string list";
62
  case Status::JwtSignatureParseErrorBadBase64:
63
    return "Jwt signature is an invalid Base64url encoded";
64
8
  case Status::JwtUnknownIssuer:
65
8
    return "Jwt issuer is not configured";
66
15
  case Status::JwtAudienceNotAllowed:
67
15
    return "Audiences in Jwt are not allowed";
68
8
  case Status::JwtVerificationFail:
69
8
    return "Jwt verification fails";
70
  case Status::JwtMultipleTokens:
71
    return "Found multiple Jwt tokens";
72

            
73
1
  case Status::JwksParseError:
74
1
    return "Jwks is an invalid JSON";
75
  case Status::JwksNoKeys:
76
    return "Jwks does not have [keys] field";
77
  case Status::JwksBadKeys:
78
    return "[keys] in Jwks is not an array";
79
2
  case Status::JwksNoValidKeys:
80
2
    return "Jwks doesn't have any valid public key";
81
  case Status::JwksKidAlgMismatch:
82
    return "Jwks doesn't have key to match kid or alg from Jwt";
83
  case Status::JwksRsaParseError:
84
    return "Jwks RSA [n] or [e] field is missing or has a parse error";
85
  case Status::JwksEcCreateKeyFail:
86
    return "Jwks EC create key fail";
87
  case Status::JwksEcXorYBadBase64:
88
    return "Jwks EC [x] or [y] field is an invalid Base64.";
89
  case Status::JwksEcParseError:
90
    return "Jwks EC [x] and [y] fields have a parse error.";
91
  case Status::JwksOctBadBase64:
92
    return "Jwks Oct key is an invalid Base64";
93
  case Status::JwksOKPXBadBase64:
94
    return "Jwks OKP [x] field is an invalid Base64.";
95
  case Status::JwksOKPXWrongLength:
96
    return "Jwks OKP [x] field is wrong length.";
97
50
  case Status::JwksFetchFail:
98
50
    return "Jwks remote fetch is failed";
99

            
100
  case Status::JwksMissingKty:
101
    return "[kty] is missing in [keys]";
102
  case Status::JwksBadKty:
103
    return "[kty] is bad in [keys]";
104
  case Status::JwksNotImplementedKty:
105
    return "[kty] is not supported in [keys]";
106

            
107
  case Status::JwksRSAKeyBadAlg:
108
    return "[alg] is not started with [RS] or [PS] for an RSA key";
109
  case Status::JwksRSAKeyMissingN:
110
    return "[n] field is missing for a RSA key";
111
  case Status::JwksRSAKeyBadN:
112
    return "[n] field is not string for a RSA key";
113
  case Status::JwksRSAKeyMissingE:
114
    return "[e] field is missing for a RSA key";
115
  case Status::JwksRSAKeyBadE:
116
    return "[e] field is not string for a RSA key";
117

            
118
  case Status::JwksECKeyBadAlg:
119
    return "[alg] is not started with [ES] for an EC key";
120
  case Status::JwksECKeyBadCrv:
121
    return "[crv] field is not string for an EC key";
122
  case Status::JwksECKeyAlgOrCrvUnsupported:
123
    return "[crv] or [alg] field is not supported for an EC key";
124
  case Status::JwksECKeyAlgNotCompatibleWithCrv:
125
    return "[crv] field specified is not compatible with [alg] for an EC key";
126
  case Status::JwksECKeyMissingX:
127
    return "[x] field is missing for an EC key";
128
  case Status::JwksECKeyBadX:
129
    return "[x] field is not string for an EC key";
130
  case Status::JwksECKeyMissingY:
131
    return "[y] field is missing for an EC key";
132
  case Status::JwksECKeyBadY:
133
    return "[y] field is not string for an EC key";
134

            
135
  case Status::JwksHMACKeyBadAlg:
136
    return "[alg] does not start with [HS] for an HMAC key";
137
  case Status::JwksHMACKeyMissingK:
138
    return "[k] field is missing for an HMAC key";
139
  case Status::JwksHMACKeyBadK:
140
    return "[k] field is not string for an HMAC key";
141

            
142
  case Status::JwksOKPKeyBadAlg:
143
    return "[alg] is not [EdDSA] for an OKP key";
144
  case Status::JwksOKPKeyMissingCrv:
145
    return "[crv] field is missing for an OKP key";
146
  case Status::JwksOKPKeyBadCrv:
147
    return "[crv] field is not string for an OKP key";
148
  case Status::JwksOKPKeyCrvUnsupported:
149
    return "[crv] field is not supported for an OKP key";
150
  case Status::JwksOKPKeyMissingX:
151
    return "[x] field is missing for an OKP key";
152
  case Status::JwksOKPKeyBadX:
153
    return "[x] field is not string for an OKP key";
154

            
155
  case Status::JwksX509BioWriteError:
156
    return "X509 parse pubkey internal fails: memory allocation";
157
  case Status::JwksX509ParseError:
158
    return "X509 parse pubkey fails";
159
  case Status::JwksX509GetPubkeyError:
160
    return "X509 parse pubkey internal fails: get pubkey";
161

            
162
  case Status::JwksPemNotImplementedKty:
163
    return "PEM Key type is not supported";
164
2
  case Status::JwksPemBadBase64:
165
2
    return "PEM pubkey parse fails";
166
  case Status::JwksPemGetRawEd25519Error:
167
    return "PEM failed to get raw ED25519 key";
168

            
169
  case Status::JwksBioAllocError:
170
    return "Failed to create BIO due to memory allocation failure";
171
253
  };
172
  // Return empty string though switch-case is exhaustive. See issues/91.
173
  return "";
174
253
}
175

            
176
} // namespace JwtVerify
177
} // namespace Envoy