-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathccapi_jwt.h
More file actions
32 lines (29 loc) · 910 Bytes
/
ccapi_jwt.h
File metadata and controls
32 lines (29 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once
#include "ccapi_cpp/ccapi_hmac.h"
#include "ccapi_cpp/ccapi_logger.h"
#include "ccapi_cpp/ccapi_macro.h"
#include "ccapi_cpp/ccapi_util_private.h"
namespace ccapi {
/**
* This class is used for handling jwt tokens.
*/
class Jwt {
public:
static std::string generate(const Hmac::ShaVersion shaVersion, const std::string& secret, const std::string& payload) {
std::string output;
switch (shaVersion) {
case Hmac::ShaVersion::SHA256:
output += UtilAlgorithm::base64UrlEncode("{\"alg\":\"HS256\",\"typ\":\"JWT\"}");
break;
default:
CCAPI_LOGGER_FATAL(CCAPI_UNSUPPORTED_VALUE);
}
output += ".";
output += UtilAlgorithm::base64UrlEncode(payload);
std::string signature = Hmac::hmac(shaVersion, secret, output);
output += ".";
output += UtilAlgorithm::base64UrlEncode(signature);
return output;
}
};
} // namespace ccapi