Kurlyk
Toggle main menu visibility
Loading...
Searching...
No Matches
Pkce.hpp
Go to the documentation of this file.
1
#pragma once
2
#ifndef _KURLYK_UTILS_PKCE_HPP_INCLUDED
3
#define _KURLYK_UTILS_PKCE_HPP_INCLUDED
4
7
8
#include "
Base64Url.hpp
"
9
#include <hmac_cpp/sha256.hpp>
10
#include <hmac_cpp/hmac_utils.hpp>
11
#include <string>
12
#include <vector>
13
#include <cstdint>
14
#include <algorithm>
15
16
namespace
kurlyk
{
17
namespace
utils {
18
21
struct
PkcePair
{
22
std::string
code_verifier
;
23
std::string
code_challenge
;
24
std::string
code_challenge_method
=
"S256"
;
25
};
26
30
inline
std::string
generate_code_verifier
(std::size_t length = 64) {
31
static
const
char
allowed[] =
32
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~"
;
33
if
(length < 43) length = 43;
34
if
(length > 128) length = 128;
35
36
std::vector<uint8_t> random_bytes = hmac_cpp::random_bytes(length);
37
std::string verifier;
38
verifier.reserve(length);
39
40
const
std::size_t allowed_count =
sizeof
(allowed) - 1;
// exclude null terminator
41
for
(std::size_t i = 0; i < length; ++i) {
42
verifier.push_back(allowed[random_bytes[i] % allowed_count]);
43
}
44
return
verifier;
45
}
46
50
inline
std::string
make_s256_code_challenge
(
const
std::string& verifier) {
51
std::vector<uint8_t> digest = hmac_hash::sha256(
52
reinterpret_cast<
const
uint8_t*
>
(verifier.data()),
53
verifier.size());
54
return
base64url_encode
(digest.data(), digest.size());
55
}
56
59
inline
PkcePair
make_pkce_pair
() {
60
PkcePair
pair;
61
pair.
code_verifier
=
generate_code_verifier
();
62
pair.
code_challenge
=
make_s256_code_challenge
(pair.
code_verifier
);
63
return
pair;
64
}
65
66
}
// namespace utils
67
}
// namespace kurlyk
68
69
#endif
// _KURLYK_UTILS_PKCE_HPP_INCLUDED
Base64Url.hpp
Provides Base64url encoding and decoding (RFC 4648, no padding).
kurlyk::utils::base64url_encode
std::string base64url_encode(const uint8_t *data, std::size_t length)
Encodes a byte buffer using Base64url (RFC 4648) without padding.
Definition
Base64Url.hpp:19
kurlyk::utils::make_s256_code_challenge
std::string make_s256_code_challenge(const std::string &verifier)
Creates an S256 code challenge from a verifier.
Definition
Pkce.hpp:50
kurlyk::utils::make_pkce_pair
PkcePair make_pkce_pair()
Creates a PKCE pair with a freshly generated verifier.
Definition
Pkce.hpp:59
kurlyk::utils::generate_code_verifier
std::string generate_code_verifier(std::size_t length=64)
Generates a cryptographically strong PKCE code verifier.
Definition
Pkce.hpp:30
kurlyk
Primary namespace for the Kurlyk library, encompassing initialization, request management,...
kurlyk::utils::PkcePair
Stores PKCE verifier and challenge values.
Definition
Pkce.hpp:21
kurlyk::utils::PkcePair::code_challenge_method
std::string code_challenge_method
Challenge method, always "S256".
Definition
Pkce.hpp:24
kurlyk::utils::PkcePair::code_verifier
std::string code_verifier
Randomly generated code verifier.
Definition
Pkce.hpp:22
kurlyk::utils::PkcePair::code_challenge
std::string code_challenge
Derived S256 code challenge.
Definition
Pkce.hpp:23
include
kurlyk
utils
Pkce.hpp
Generated by
1.17.0