#include "JwtTokenHelper.h"
#include "DataIOUtils.h"
#include "Json.h"
#include "JsonMessageWriter.h"
#include <ctype.h>
bool
JwtTokenHelper::IsValid(const BString& value)
{
int countDots = 0;
for (int i = 0; i < value.Length(); i++) {
char ch = value[i];
if ('.' == ch)
countDots++;
else {
if (!_IsBase64(ch))
return false;
}
}
return 2 == countDots;
}
status_t
JwtTokenHelper::ParseClaims(const BString& token, BMessage& message)
{
int firstDot = token.FindFirst('.');
if (firstDot == B_ERROR)
return B_BAD_VALUE;
int secondDot = token.FindFirst('.', firstDot + 1);
if (secondDot == B_ERROR)
return B_BAD_VALUE;
BMemoryIO memoryIo(&(token.String())[firstDot + 1], (secondDot - firstDot) - 1);
Base64DecodingDataIO base64DecodingIo(&memoryIo, '-', '_');
BJsonMessageWriter writer(message);
BJson::Parse(&base64DecodingIo, &writer);
return writer.ErrorStatus();
}
bool
JwtTokenHelper::_IsBase64(char ch)
{
return isalnum(ch)
|| '=' == ch
|| '-' == ch
|| '_' == ch;
}