46 const char b64Table[] = {
47 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66,
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, 62, 66, 62, 66, 63, 52, 53, 54, 55, 56, 57,
50 58, 59, 60, 61, 66, 66, 66, 66, 66, 66, 66, 0, 1, 2, 3, 4, 5, 6,
51 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
52 25, 66, 66, 66, 66, 63, 66, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
53 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 66, 66, 66,
54 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 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,
73 size_t DecodeBytesNeeded(
size_t num_decode) {
74 return 3 + (num_decode / 4) * 3;
81int DecodeUrl(
const char *decode,
size_t num_decode,
char *out,
size_t &num_out)
84 if ((decode + num_decode) < decode || (out + num_out) < out)
87 if (num_out < DecodeBytesNeeded(num_decode))
90 const char *end = decode + num_decode;
91 const char *out_start = out;
97 while (decode < end) {
109 *(out++) = (buf >> 16) & 0xff;
110 *(out++) = (buf >> 8) & 0xff;
111 *(out++) = buf & 0xff;
119 *(out++) = (buf >> 10) & 0xff;
120 *(out++) = (buf >> 2) & 0xff;
123 *(out++) = (buf >> 4) & 0xff;
127 num_out = (out - out_start);
140 size_t inBytes, outBytes;
142 char *key, *outData, inData[1024];
146 if (!strncmp(b64data,
"Bearer%20", 9)) b64data += 9;
152 if (!(dot = index(b64data,
'.')))
return false;
157 inBytes = dot - b64data;
158 if (inBytes >= (
int)
sizeof(inData))
return false;
159 memcpy(inData, b64data, inBytes);
164 outBytes = DecodeBytesNeeded(inBytes);
165 outData = (
char *)alloca(outBytes);
169 if (DecodeUrl(inData, inBytes, outData, outBytes))
return false;
174 if (outBytes <= 0 || *outData !=
'{' || outData[outBytes-1] !=
'}')
179 if (!(key = strstr(outData,
"\"typ\"")))
return false;
184 while(*key ==
' ') key++;
185 if (*key !=
':')
return false;
190 while(*key ==
' ') key++;
191 return strncmp(key,
"\"JWT\"", 5) == 0;