libjwt-1.13.1
jwt.h
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Ben Collins <ben@cyphre.com>
2  This file is part of the JWT C Library
3 
4  This Source Code Form is subject to the terms of the Mozilla Public
5  License, v. 2.0. If a copy of the MPL was not distributed with this
6  file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 
13 #ifndef JWT_H
14 #define JWT_H
15 
16 #include <stdio.h>
17 #include <time.h>
18 
19 #ifdef _MSC_VER
20 
21  #define DEPRECATED(func) __declspec(deprecated) func
22 
23  #define alloca _alloca
24  #define strcasecmp _stricmp
25  #define strdup _strdup
26 
27  #ifdef JWT_DLL_CONFIG
28  #ifdef JWT_BUILD_SHARED_LIBRARY
29  #define JWT_EXPORT __declspec(dllexport)
30  #else
31  #define JWT_EXPORT __declspec(dllimport)
32  #endif
33  #else
34  #define JWT_EXPORT
35  #endif
36 
37 #else
38 
39  #define DEPRECATED(func) func __attribute__ ((deprecated))
40  #define JWT_EXPORT
41 
42 #endif
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
49 typedef struct jwt jwt_t;
50 
52 typedef struct jwt_valid jwt_valid_t;
53 
55 typedef enum jwt_alg {
67 } jwt_alg_t;
68 
69 #define JWT_ALG_INVAL JWT_ALG_TERM
70 
72 #define JWT_VALIDATION_SUCCESS 0x0000
73 #define JWT_VALIDATION_ERROR 0x0001 /* General failures */
74 #define JWT_VALIDATION_ALG_MISMATCH 0x0002
75 #define JWT_VALIDATION_EXPIRED 0x0004
76 #define JWT_VALIDATION_TOO_NEW 0x0008
77 #define JWT_VALIDATION_ISS_MISMATCH 0x0010
78 #define JWT_VALIDATION_SUB_MISMATCH 0x0020
79 #define JWT_VALIDATION_AUD_MISMATCH 0x0040
80 #define JWT_VALIDATION_GRANT_MISSING 0x0080
81 #define JWT_VALIDATION_GRANT_MISMATCH 0x0100
82 
84 typedef void *(*jwt_malloc_t)(size_t);
85 typedef void *(*jwt_realloc_t)(void *, size_t);
86 typedef void (*jwt_free_t)(void *);
87 
89 typedef struct {
90  const unsigned char *jwt_key;
92 } jwt_key_t;
93 
95 typedef int (*jwt_key_p_t)(const jwt_t *, jwt_key_t *);
96 
123 JWT_EXPORT int jwt_new(jwt_t **jwt);
124 
149 JWT_EXPORT int jwt_decode(jwt_t **jwt, const char *token,
150  const unsigned char *key, int key_len);
151 
168 JWT_EXPORT int jwt_decode_2(jwt_t **jwt, const char *token, jwt_key_p_t key_provider);
169 
179 JWT_EXPORT void jwt_free(jwt_t *jwt);
180 
190 JWT_EXPORT jwt_t *jwt_dup(jwt_t *jwt);
191 
217 JWT_EXPORT const char *jwt_get_grant(jwt_t *jwt, const char *grant);
218 
235 JWT_EXPORT long jwt_get_grant_int(jwt_t *jwt, const char *grant);
236 
253 JWT_EXPORT int jwt_get_grant_bool(jwt_t *jwt, const char *grant);
254 
268 JWT_EXPORT char *jwt_get_grants_json(jwt_t *jwt, const char *grant);
269 
288 JWT_EXPORT int jwt_add_grant(jwt_t *jwt, const char *grant, const char *val);
289 
307 JWT_EXPORT int jwt_add_grant_int(jwt_t *jwt, const char *grant, long val);
308 
326 JWT_EXPORT int jwt_add_grant_bool(jwt_t *jwt, const char *grant, int val);
327 
340 JWT_EXPORT int jwt_add_grants_json(jwt_t *jwt, const char *json);
341 
354 JWT_EXPORT int jwt_del_grants(jwt_t *jwt, const char *grant);
355 
367 DEPRECATED(JWT_EXPORT int jwt_del_grant(jwt_t *jwt, const char *grant));
368 
394 JWT_EXPORT const char *jwt_get_header(jwt_t *jwt, const char *header);
395 
412 JWT_EXPORT long jwt_get_header_int(jwt_t *jwt, const char *header);
413 
430 JWT_EXPORT int jwt_get_header_bool(jwt_t *jwt, const char *header);
431 
445 JWT_EXPORT char *jwt_get_headers_json(jwt_t *jwt, const char *header);
446 
465 JWT_EXPORT int jwt_add_header(jwt_t *jwt, const char *header, const char *val);
466 
484 JWT_EXPORT int jwt_add_header_int(jwt_t *jwt, const char *header, long val);
485 
503 JWT_EXPORT int jwt_add_header_bool(jwt_t *jwt, const char *header, int val);
504 
517 JWT_EXPORT int jwt_add_headers_json(jwt_t *jwt, const char *json);
518 
531 JWT_EXPORT int jwt_del_headers(jwt_t *jwt, const char *header);
532 
562 JWT_EXPORT int jwt_dump_fp(jwt_t *jwt, FILE *fp, int pretty);
563 
582 JWT_EXPORT char *jwt_dump_str(jwt_t *jwt, int pretty);
583 
595 JWT_EXPORT int jwt_encode_fp(jwt_t *jwt, FILE *fp);
596 
608 JWT_EXPORT char *jwt_encode_str(jwt_t *jwt);
609 
616 JWT_EXPORT void jwt_free_str(char *str);
617 
645 JWT_EXPORT int jwt_set_alg(jwt_t *jwt, jwt_alg_t alg, const unsigned char *key, int len);
646 
655 JWT_EXPORT jwt_alg_t jwt_get_alg(const jwt_t *jwt);
656 
666 JWT_EXPORT const char *jwt_alg_str(jwt_alg_t alg);
667 
679 JWT_EXPORT jwt_alg_t jwt_str_alg(const char *alg);
680 
713 JWT_EXPORT int jwt_set_alloc(jwt_malloc_t pmalloc, jwt_realloc_t prealloc, jwt_free_t pfree);
714 
722 JWT_EXPORT void jwt_get_alloc(jwt_malloc_t *pmalloc, jwt_realloc_t *prealloc, jwt_free_t *pfree);
723 
750 JWT_EXPORT unsigned int jwt_validate(jwt_t *jwt, jwt_valid_t *jwt_valid);
751 
764 JWT_EXPORT int jwt_valid_new(jwt_valid_t **jwt_valid, jwt_alg_t alg);
765 
775 JWT_EXPORT void jwt_valid_free(jwt_valid_t *jwt_valid);
776 
787 JWT_EXPORT unsigned int jwt_valid_get_status(jwt_valid_t *jwt_valid);
788 
802 JWT_EXPORT int jwt_valid_add_grant(jwt_valid_t *jwt_valid, const char *grant, const char *val);
803 
820 JWT_EXPORT const char *jwt_valid_get_grant(jwt_valid_t *jwt_valid, const char *grant);
821 
834 JWT_EXPORT int jwt_valid_add_grant_int(jwt_valid_t *jwt_valid, const char *grant, long val);
835 
852 JWT_EXPORT long jwt_valid_get_grant_int(jwt_valid_t *jwt_valid, const char *grant);
853 
871 JWT_EXPORT int jwt_valid_add_grant_bool(jwt_valid_t *jwt_valid, const char *grant, int val);
872 
889 JWT_EXPORT int jwt_valid_get_grant_bool(jwt_valid_t *jwt_valid, const char *grant);
890 
901 JWT_EXPORT int jwt_valid_add_grants_json(jwt_valid_t *jwt_valid, const char *json);
902 
915 JWT_EXPORT char* jwt_valid_get_grants_json(jwt_valid_t *jwt_valid, const char *grant);
916 
929 JWT_EXPORT int jwt_valid_del_grants(jwt_valid_t *jwt, const char *grant);
930 
941 JWT_EXPORT int jwt_valid_set_now(jwt_valid_t *jwt_valid, const time_t now);
942 
956 JWT_EXPORT int jwt_valid_set_headers(jwt_valid_t *jwt_valid, int hdr);
957 
960 #ifdef __cplusplus
961 }
962 #endif
963 
964 #endif /* JWT_H */
char * jwt_get_headers_json(jwt_t *jwt, const char *header)
Return the value of a header as JSON encoded object string.
int jwt_add_grant_int(jwt_t *jwt, const char *grant, long val)
Add a new integer grant to this JWT object.
#define JWT_EXPORT
Definition: jwt.h:40
void jwt_valid_free(jwt_valid_t *jwt_valid)
Free a JWT validation object and any other resources it is using.
jwt_alg_t jwt_str_alg(const char *alg)
Convert alg string to type.
void jwt_free_str(char *str)
Free a string returned from the library.
const char * jwt_get_grant(jwt_t *jwt, const char *grant)
Return the value of a string grant.
const unsigned char * jwt_key
Definition: jwt.h:90
void jwt_get_alloc(jwt_malloc_t *pmalloc, jwt_realloc_t *prealloc, jwt_free_t *pfree)
Get functions used for allocating and freeing memory.
void *(* jwt_malloc_t)(size_t)
JWT Memory allocation overrides.
Definition: jwt.h:84
int jwt_valid_add_grants_json(jwt_valid_t *jwt_valid, const char *json)
Add required grants from a JSON encoded object string.
long jwt_get_header_int(jwt_t *jwt, const char *header)
Return the value of an integer header.
int jwt_add_header_bool(jwt_t *jwt, const char *header, int val)
Add a new boolean header to this JWT object.
enum jwt_alg jwt_alg_t
JWT algorithm types.
int jwt_get_header_bool(jwt_t *jwt, const char *header)
Return the value of an boolean header.
int(* jwt_key_p_t)(const jwt_t *, jwt_key_t *)
Key provider - inspects the JWT to obtain the key used to verify the signature.
Definition: jwt.h:95
int jwt_del_grant(jwt_t *jwt, const char *grant)
int jwt_add_headers_json(jwt_t *jwt, const char *json)
Add headers from a JSON encoded object string.
#define DEPRECATED(func)
Definition: jwt.h:39
jwt_alg_t jwt_get_alg(const jwt_t *jwt)
Get the jwt_alg_t set for this JWT object.
int jwt_valid_del_grants(jwt_valid_t *jwt, const char *grant)
Delete a grant from this JWT object.
int jwt_del_grants(jwt_t *jwt, const char *grant)
Delete a grant from this JWT object.
int jwt_valid_set_headers(jwt_valid_t *jwt_valid, int hdr)
Set validation for replicated claims in headers.
void *(* jwt_realloc_t)(void *, size_t)
Definition: jwt.h:85
int jwt_valid_new(jwt_valid_t **jwt_valid, jwt_alg_t alg)
Allocate a new, JWT validation object.
int jwt_encode_fp(jwt_t *jwt, FILE *fp)
Fully encode a JWT object and write it to FILE.
unsigned int jwt_validate(jwt_t *jwt, jwt_valid_t *jwt_valid)
Validate a JWT object with a validation object.
int jwt_key_len
Definition: jwt.h:91
jwt_alg
JWT algorithm types.
Definition: jwt.h:55
int jwt_valid_add_grant_bool(jwt_valid_t *jwt_valid, const char *grant, int val)
Add a new boolean required grant to this JWT validation object.
void(* jwt_free_t)(void *)
Definition: jwt.h:86
const char * jwt_get_header(jwt_t *jwt, const char *header)
Return the value of a string header.
int jwt_add_header_int(jwt_t *jwt, const char *header, long val)
Add a new integer header to this JWT object.
int jwt_valid_add_grant_int(jwt_valid_t *jwt_valid, const char *grant, long val)
Add a new integer grant requirement to this JWT validation object.
char * jwt_encode_str(jwt_t *jwt)
Fully encode a JWT object and return as a string.
int jwt_valid_add_grant(jwt_valid_t *jwt_valid, const char *grant, const char *val)
Add a new string grant requirement to this JWT validation object.
int jwt_add_grant_bool(jwt_t *jwt, const char *grant, int val)
Add a new boolean grant to this JWT object.
int jwt_set_alg(jwt_t *jwt, jwt_alg_t alg, const unsigned char *key, int len)
Set an algorithm from jwt_alg_t for this JWT object.
const char * jwt_alg_str(jwt_alg_t alg)
Convert alg type to it&#39;s string representation.
Structure used by key provider to return a key.
Definition: jwt.h:89
char * jwt_dump_str(jwt_t *jwt, int pretty)
Return plain text representation as a string.
int jwt_valid_set_now(jwt_valid_t *jwt_valid, const time_t now)
Set the time for which expires and not-before claims should be evaluated.
struct jwt_valid jwt_valid_t
Opaque JWT validation object.
Definition: jwt.h:52
long jwt_get_grant_int(jwt_t *jwt, const char *grant)
Return the value of an integer grant.
int jwt_dump_fp(jwt_t *jwt, FILE *fp, int pretty)
Output plain text representation to a FILE pointer.
int jwt_get_grant_bool(jwt_t *jwt, const char *grant)
Return the value of an boolean grant.
int jwt_set_alloc(jwt_malloc_t pmalloc, jwt_realloc_t prealloc, jwt_free_t pfree)
Set functions to be used for allocating and freeing memory.
int jwt_new(jwt_t **jwt)
Allocate a new, empty, JWT object.
const char * jwt_valid_get_grant(jwt_valid_t *jwt_valid, const char *grant)
Return the value of a string required grant.
jwt_t * jwt_dup(jwt_t *jwt)
Duplicate an existing JWT object.
long jwt_valid_get_grant_int(jwt_valid_t *jwt_valid, const char *grant)
Return the value of an integer required grant.
struct jwt jwt_t
Opaque JWT object.
Definition: jwt.h:49
int jwt_add_grants_json(jwt_t *jwt, const char *json)
Add grants from a JSON encoded object string.
int jwt_valid_get_grant_bool(jwt_valid_t *jwt_valid, const char *grant)
Return the value of an boolean required grant.
char * jwt_valid_get_grants_json(jwt_valid_t *jwt_valid, const char *grant)
Return the value of a grant as JSON encoded object string.
int jwt_add_grant(jwt_t *jwt, const char *grant, const char *val)
Add a new string grant to this JWT object.
int jwt_decode(jwt_t **jwt, const char *token, const unsigned char *key, int key_len)
Verify an existing JWT and allocate a new JWT object from it.
int jwt_decode_2(jwt_t **jwt, const char *token, jwt_key_p_t key_provider)
Like jwt_decode(), but the key will be obtained via the key provider.
int jwt_add_header(jwt_t *jwt, const char *header, const char *val)
Add a new string header to this JWT object.
void jwt_free(jwt_t *jwt)
Free a JWT object and any other resources it is using.
unsigned int jwt_valid_get_status(jwt_valid_t *jwt_valid)
Return the status string for the validation object.
int jwt_del_headers(jwt_t *jwt, const char *header)
Delete a header from this JWT object.
char * jwt_get_grants_json(jwt_t *jwt, const char *grant)
Return the value of a grant as JSON encoded object string.