47 const char b64Table[] = {
48 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
49 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
50 66, 66, 66, 66, 66, 66, 66, 62, 66, 62, 66, 63, 52, 53, 54, 55, 56, 57,
51 58, 59, 60, 61, 66, 66, 66, 66, 66, 66, 66, 0, 1, 2, 3, 4, 5, 6,
52 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
53 25, 66, 66, 66, 66, 63, 66, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
54 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 66, 66, 66,
55 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
56 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
57 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
58 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
59 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
60 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
61 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
74 size_t DecodeBytesNeeded(
size_t num_decode) {
75 return 3 + (num_decode / 4) * 3;
82 int DecodeUrl(
const char *decode,
size_t num_decode,
char *out,
size_t &num_out)
85 if (num_decode > std::numeric_limits<size_t>::max() - (
size_t)decode)
88 if (num_out > std::numeric_limits<size_t>::max() - (
size_t)out)
91 if (num_out < DecodeBytesNeeded(num_decode))
94 const char *end = decode + num_decode;
95 const char *out_start = out;
101 while (decode < end) {
113 *(out++) = (buf >> 16) & 0xff;
114 *(out++) = (buf >> 8) & 0xff;
115 *(out++) = buf & 0xff;
123 *(out++) = (buf >> 10) & 0xff;
124 *(out++) = (buf >> 2) & 0xff;
127 *(out++) = (buf >> 4) & 0xff;
131 num_out = (out - out_start);
144 size_t inBytes, outBytes;
146 char *key, *outData, inData[1024];
150 if (!strncmp(b64data,
"Bearer%20", 9)) b64data += 9;
156 if (!(dot = index(b64data,
'.')))
return false;
161 inBytes = dot - b64data;
162 if (inBytes >= (
int)
sizeof(inData))
return false;
163 memcpy(inData, b64data, inBytes);
168 outBytes = DecodeBytesNeeded(inBytes);
169 outData = (
char *)alloca(outBytes);
173 if (DecodeUrl(inData, inBytes, outData, outBytes))
return false;
178 if (outBytes <= 0 || *outData !=
'{' || outData[outBytes-1] !=
'}')
183 if (!(key = strstr(outData,
"\"typ\"")))
return false;
188 while(*key ==
' ') key++;
189 if (*key !=
':')
return false;
194 while(*key ==
' ') key++;
195 return strncmp(key,
"\"JWT\"", 5) == 0;