|
| 1 | +// Taken with modifications from https://gist.github.com/niuk/6365b819a86a7e0b92d82328fcf87da5 |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | +#include <stdlib.h> |
| 5 | + |
| 6 | +#include <openssl/ssl.h> |
| 7 | +#include <openssl/err.h> |
| 8 | + |
| 9 | +#define CPPHTTPLIB_OPENSSL_SUPPORT |
| 10 | +#include <httplib.hpp> |
| 11 | + |
| 12 | +#include <json.hpp> |
| 13 | +using json = nlohmann::json; |
| 14 | +namespace duckdb |
| 15 | +{ |
| 16 | + |
| 17 | + char get_base64_char(char byte) { |
| 18 | + if (byte < 26) { |
| 19 | + return 'A' + byte; |
| 20 | + } else if (byte < 52) { |
| 21 | + return 'a' + byte - 26; |
| 22 | + } else if (byte < 62) { |
| 23 | + return '0' + byte - 52; |
| 24 | + } else if (byte == 62) { |
| 25 | + return '-'; |
| 26 | + } else if (byte == 63) { |
| 27 | + return '_'; |
| 28 | + } else { |
| 29 | + fprintf(stderr, "BAD BYTE: %02x\n", byte); |
| 30 | + exit(1); |
| 31 | + return 0; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + // To execute C, please define "int main()" |
| 36 | + void base64encode(char *output, const char *input, size_t input_length) { |
| 37 | + size_t input_index = 0; |
| 38 | + size_t output_index = 0; |
| 39 | + for (; input_index < input_length; ++output_index) { |
| 40 | + switch (output_index % 4) { |
| 41 | + case 0: |
| 42 | + output[output_index] = get_base64_char((0xfc & input[input_index]) >> 2); |
| 43 | + break; |
| 44 | + case 1: |
| 45 | + output[output_index] = get_base64_char(((0x03 & input[input_index]) << 4) | ((0xf0 & input[input_index + 1]) >> 4)); |
| 46 | + ++input_index; |
| 47 | + break; |
| 48 | + case 2: |
| 49 | + output[output_index] = get_base64_char(((0x0f & input[input_index]) << 2) | ((0xc0 & input[input_index + 1]) >> 6)); |
| 50 | + ++input_index; |
| 51 | + break; |
| 52 | + case 3: |
| 53 | + output[output_index] = get_base64_char(0x3f & input[input_index]); |
| 54 | + ++input_index; |
| 55 | + break; |
| 56 | + default: |
| 57 | + exit(1); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + output[output_index] = '\0'; |
| 62 | + } |
| 63 | + |
| 64 | + std::string get_token(const std::string& email, const std::string& private_key) { |
| 65 | + const char *header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"; |
| 66 | + |
| 67 | + const int iat = time(NULL); |
| 68 | + const int exp = iat + 60 * 60 - 1; |
| 69 | + |
| 70 | + char claim_set[1024]; |
| 71 | + |
| 72 | + /* Create jwt claim set */ |
| 73 | + json jwt_claim_set; |
| 74 | + std::time_t t = std::time(NULL); |
| 75 | + jwt_claim_set["iss"] = email; /* service account email address */ |
| 76 | + jwt_claim_set["scope"] = "https://www.googleapis.com/auth/spreadsheets" /* scope of requested access token */; |
| 77 | + jwt_claim_set["aud"] = "https://accounts.google.com/o/oauth2/token"; /* intended target of the assertion for an access token */ |
| 78 | + jwt_claim_set["iat"] = std::to_string(t); /* issued time */ |
| 79 | + // Since we get a new token on every request (and max request time is 3 minutes), |
| 80 | + // set the limit to 10 minutes. (Max that Google allows is 1 hour) |
| 81 | + jwt_claim_set["exp"] = std::to_string(t+600); /* expire time*/ |
| 82 | + |
| 83 | + std::copy(jwt_claim_set.dump().cbegin(), jwt_claim_set.dump().cend(), claim_set); |
| 84 | + |
| 85 | + char header_64[1024]; |
| 86 | + base64encode(header_64, header, strlen(header)); |
| 87 | + |
| 88 | + char claim_set_64[1024]; |
| 89 | + base64encode(claim_set_64, claim_set, strlen(claim_set)); |
| 90 | + |
| 91 | + char input[1024]; |
| 92 | + int input_length = sprintf(input, "%s.%s", header_64, claim_set_64); |
| 93 | + |
| 94 | + unsigned char *digest = SHA256((const unsigned char *)input, input_length, NULL); |
| 95 | + char digest_str[1024]; |
| 96 | + for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) { |
| 97 | + sprintf(digest_str + i * 2, "%02x", digest[i]); |
| 98 | + } |
| 99 | + |
| 100 | + digest_str[SHA256_DIGEST_LENGTH * 2] = '\0'; |
| 101 | + |
| 102 | + // This works, but requires a file. The in-memory version is below. |
| 103 | + // FILE *key_file = fopen("credentials.key.pem", "r"); |
| 104 | + // RSA *rsa = PEM_read_RSAPrivateKey(key_file, NULL, NULL, NULL); |
| 105 | + |
| 106 | + BIO *bufio; |
| 107 | + RSA *rsa; |
| 108 | + |
| 109 | + bufio = BIO_new_mem_buf(private_key.c_str(), -1); |
| 110 | + PEM_read_bio_RSAPrivateKey(bufio, &rsa, 0, NULL); |
| 111 | + |
| 112 | + if (rsa != NULL) { |
| 113 | + unsigned char sigret[4096]; |
| 114 | + unsigned int siglen; |
| 115 | + if (RSA_sign(NID_sha256, digest, SHA256_DIGEST_LENGTH, sigret, &siglen, rsa)) { |
| 116 | + if (RSA_verify(NID_sha256, digest, SHA256_DIGEST_LENGTH, sigret, siglen, rsa)) { |
| 117 | + char signature_64[1024]; |
| 118 | + base64encode(signature_64, (const char *)sigret, siglen); |
| 119 | + |
| 120 | + char jwt[1024]; |
| 121 | + sprintf(jwt, "%s.%s", input, signature_64); |
| 122 | + |
| 123 | + duckdb_httplib_openssl::Client cli("https://oauth2.googleapis.com"); |
| 124 | + duckdb_httplib_openssl::Params params; |
| 125 | + params.emplace("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer"); |
| 126 | + params.emplace("assertion", jwt); |
| 127 | + auto result = cli.Post("/token", params); |
| 128 | + return (result -> body); |
| 129 | + } else { |
| 130 | + printf("Could not verify RSA signature."); |
| 131 | + } |
| 132 | + } else { |
| 133 | + unsigned long err = ERR_get_error(); |
| 134 | + printf("RSA_sign failed: %lu, %s\n", err, ERR_error_string(err, NULL)); |
| 135 | + } |
| 136 | + |
| 137 | + RSA_free(rsa); |
| 138 | + } |
| 139 | + std::string failure_message = "{\"status\":\"failed\"}"; |
| 140 | + return failure_message; |
| 141 | + } |
| 142 | +} |
0 commit comments