This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
jwt-sign-rs256.c
66 lines (60 loc) · 3.88 KB
/
jwt-sign-rs256.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
*
* Rhonabwy Javascript Object Signing and Encryption (JOSE) library
*
* Example program with a signed JWT using RS256
*
* Copyright 2022 Nicolas Mora <[email protected]>
*
* License MIT
*
* To compile with gcc, use the following command:
* gcc -o jwt-sign-rs256 jwt-sign-rs256.c -lrhonabwy
*
*/
#include <stdio.h>
#include <rhonabwy.h>
const char sign_key[] = "{\"kty\":\"RSA\",\"n\":\"0vx7agoebGcQSuuPiLJXZptN9nndrQmbXEps2aiAFbWhM78LhWx4cbbfAAtVT86zwu1RK7aPFFxuhDR1L6tSoc_BJECPebWKR"\
"XjBZCiFV4n3oknjhMstn64tZ_2W-5JsGY4Hc5n9yBXArwl93lqt7_RN5w6Cf0h4QyQ5v-65YGjQR0_FDW2QvzqY368QQMicAtaSqzs8KJZgnYb9c7d0zgdAZHz"\
"u6qMQvRL5hajrn1n91CbOpbISD08qNLyrdkt-bFTWhAI4vMQFh6WeZu0fM4lFd2NcRwr3XPksINHaQ-G_xBniIqbw0Ls1jF44-csFCur-kEgU8awapJzKnqDKg"\
"w\",\"e\":\"AQAB\",\"d\":\"X4cTteJY_gn4FYPsXB8rdXix5vwsg1FLN5E3EaG6RJoVH-HLLKD9M7dx5oo7GURknchnrRweUkC7hT5fJLM0WbFAKNLWY2v"\
"v7B6NqXSzUvxT0_YSfqijwp3RTzlBaCxWp4doFk5N2o8Gy_nHNKroADIkJ46pRUohsXywbReAdYaMwFs9tv8d_cPVY3i07a3t8MN6TNwm0dSawm9v47UiCl3Sk"\
"5ZiG7xojPLu4sbg1U2jx4I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jwk_t * jwk = NULL;
jwt_t * jwt = NULL;
char * token = NULL;
// Imports RSA private key to sign the token
if (NULL != (jwk = r_jwk_quick_import(R_IMPORT_JSON_STR, sign_key))) {
// Initialize the jwt_t structure
if (RHN_OK == r_jwt_init(&jwt)) {
// Initialize jwt's content
// The payload will have the following content {"aud": "abcd1234", "iat": 1466571360, "iss": "https://example.com"}
// The header will have the following content {"alg": "RS256", "kid": "2011-04-29", "str": "a value", "typ": "JWT"}
if (RHN_OK == r_jwt_set_properties(jwt, RHN_OPT_SIG_ALG, R_JWA_ALG_RS256,
RHN_OPT_HEADER_STR_VALUE, "str", "a value",
RHN_OPT_CLAIM_STR_VALUE, "iss", "https://example.com",
RHN_OPT_CLAIM_STR_VALUE, "aud", "abcd1234",
RHN_OPT_CLAIM_INT_VALUE, "iat", 1466571360,
RHN_OPT_NONE)) {
// Serialize the JWT using the RSA provate key to sign the token
token = r_jwt_serialize_signed(jwt, jwk, 0);
printf("Token: %s\n", token);
// Deallocate the seralized token
r_free(token);
} // else handle r_jwt_set_properties error
} // else handle r_jwt_init error
} // else handle r_jwk_quick_import error
// Deallocate the jwt and the jwk used
r_jwk_free(jwk);
r_jwt_free(jwt);
return 0;
}