XRootD
XrdSecztn Namespace Reference

Functions

bool isJWT (const char *)
 

Function Documentation

◆ isJWT()

bool XrdSecztn::isJWT ( const char *  b64data)

Definition at line 138 of file XrdSecztn.cc.

139 {
140  size_t inBytes, outBytes;
141  const char *dot;
142  char *key, *outData, inData[1024];
143 
144 // Skip over the header should it exist (sommetime it does sometimes not)
145 //
146  if (!strncmp(b64data, "Bearer%20", 9)) b64data += 9;
147 
148 // We are only interested in the header which must appear first and be
149 // separated by a dot from subsequent tokens. If it does not have the
150 // dot then we assume it's not returnable. Otherwise truncate it at the dot.
151 //
152  if (!(dot = index(b64data, '.'))) return false;
153 
154 // Copy out the token segment we wish to check. The JWT header can never be
155 // more than 1K long and that's being way generous.
156 //
157  inBytes = dot - b64data;
158  if (inBytes >= (int)sizeof(inData)) return false;
159  memcpy(inData, b64data, inBytes);
160  inData[inBytes] = 0;
161 
162 // Allocate a buffer large enough to hold the result. Get it from the stack.
163 //
164  outBytes = DecodeBytesNeeded(inBytes);
165  outData = (char *)alloca(outBytes);
166 
167 // If we can't decode what we have then indicate this is not returnable
168 //
169  if (DecodeUrl(inData, inBytes, outData, outBytes)) return false;
170 
171 // The json object must start/end with a brace and must contain the key:value
172 // of '"typ":"JWT"', other elements may change but not this one.
173 //
174  if (outBytes <= 0 || *outData != '{' || outData[outBytes-1] != '}')
175  return false;
176 
177 // Search for the key
178 //
179  if (!(key = strstr(outData, "\"typ\""))) return false;
180 
181 // Subsequently there should be a colon or spaces but nothing more
182 //
183  key += 5;
184  while(*key == ' ') key++;
185  if (*key != ':') return false;
186 
187 // There may be more spaces but anything else must be the expected value
188 //
189  key++;
190  while(*key == ' ') key++;
191  return strncmp(key, "\"JWT\"", 5) == 0;
192 }