GRPC Core  9.0.0
jwt_verifier.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_VERIFIER_H
20 #define GRPC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_VERIFIER_H
21 
23 
25 #include "src/core/lib/json/json.h"
26 
27 #include <grpc/slice.h>
28 #include <grpc/support/time.h>
29 
30 /* --- Constants. --- */
31 
32 #define GRPC_OPENID_CONFIG_URL_SUFFIX "/.well-known/openid-configuration"
33 #define GRPC_GOOGLE_SERVICE_ACCOUNTS_EMAIL_DOMAIN "gserviceaccount.com"
34 #define GRPC_GOOGLE_SERVICE_ACCOUNTS_KEY_URL_PREFIX \
35  "www.googleapis.com/robot/v1/metadata/x509"
36 
37 /* --- grpc_jwt_verifier_status. --- */
38 
39 typedef enum {
49 
51 
52 /* --- grpc_jwt_claims. --- */
53 
55 
57 
58 /* Returns the whole JSON tree of the claims. */
59 const grpc_json* grpc_jwt_claims_json(const grpc_jwt_claims* claims);
60 
61 /* Access to registered claims in https://tools.ietf.org/html/rfc7519#page-9 */
62 const char* grpc_jwt_claims_subject(const grpc_jwt_claims* claims);
63 const char* grpc_jwt_claims_issuer(const grpc_jwt_claims* claims);
64 const char* grpc_jwt_claims_id(const grpc_jwt_claims* claims);
65 const char* grpc_jwt_claims_audience(const grpc_jwt_claims* claims);
69 
70 /* --- grpc_jwt_verifier. --- */
71 
73 
74 typedef struct {
75  /* The email domain is the part after the @ sign. */
76  const char* email_domain;
77 
78  /* The key url prefix will be used to get the public key from the issuer:
79  https://<key_url_prefix>/<issuer_email>
80  Therefore the key_url_prefix must NOT contain https://. */
81  const char* key_url_prefix;
83 
84 /* Globals to control the verifier. Not thread-safe. */
87 
88 /* The verifier can be created with some custom mappings to help with key
89  discovery in the case where the issuer is an email address.
90  mappings can be NULL in which case num_mappings MUST be 0.
91  A verifier object has one built-in mapping (unless overridden):
92  GRPC_GOOGLE_SERVICE_ACCOUNTS_EMAIL_DOMAIN ->
93  GRPC_GOOGLE_SERVICE_ACCOUNTS_KEY_URL_PREFIX.*/
96  size_t num_mappings);
97 
98 /*The verifier must not be destroyed if there are still outstanding callbacks.*/
100 
101 /* User provided callback that will be called when the verification of the JWT
102  is done (maybe in another thread).
103  It is the responsibility of the callee to call grpc_jwt_claims_destroy on
104  the claims. */
105 typedef void (*grpc_jwt_verification_done_cb)(void* user_data,
107  grpc_jwt_claims* claims);
108 
109 /* Verifies for the JWT for the given expected audience. */
111  grpc_pollset* pollset, const char* jwt,
112  const char* audience,
114  void* user_data);
115 
116 /* --- TESTING ONLY exposed functions. --- */
117 
119  const grpc_slice& buffer);
121  const char* audience);
122 const char* grpc_jwt_issuer_email_domain(const char* issuer);
123 
124 #endif /* GRPC_CORE_LIB_SECURITY_CREDENTIALS_JWT_JWT_VERIFIER_H */
int64_t grpc_millis
Definition: exec_ctx.h:35
const char * grpc_jwt_issuer_email_domain(const char *issuer)
Definition: jwt_verifier.cc:760
const char * grpc_jwt_claims_subject(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:203
void grpc_jwt_verifier_verify(grpc_jwt_verifier *verifier, grpc_pollset *pollset, const char *jwt, const char *audience, grpc_jwt_verification_done_cb cb, void *user_data)
Definition: jwt_verifier.cc:857
grpc_linked_mdelem status
Definition: lame_client.cc:44
void(* grpc_jwt_verification_done_cb)(void *user_data, grpc_jwt_verifier_status status, grpc_jwt_claims *claims)
Definition: jwt_verifier.h:105
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1]. ...
Definition: slice.h:60
grpc_jwt_claims * grpc_jwt_claims_from_json(grpc_json *json, const grpc_slice &buffer)
Definition: jwt_verifier.cc:239
const char * grpc_jwt_verifier_status_to_string(grpc_jwt_verifier_status status)
Definition: jwt_verifier.cc:48
grpc_jwt_verifier_status
Definition: jwt_verifier.h:39
const char * grpc_jwt_claims_audience(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:218
Definition: jwt_verifier.cc:401
Definition: jwt_verifier.h:40
gpr_timespec grpc_jwt_claims_issued_at(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:223
Definition: jwt_verifier.h:44
void grpc_jwt_verifier_destroy(grpc_jwt_verifier *verifier)
Definition: jwt_verifier.cc:930
Definition: pollset_custom.cc:40
grpc_jwt_verifier_status grpc_jwt_claims_check(const grpc_jwt_claims *claims, const char *audience)
Definition: jwt_verifier.cc:286
Definition: jwt_verifier.h:41
grpc_millis grpc_jwt_verifier_max_delay
Definition: jwt_verifier.cc:394
const char * grpc_jwt_claims_id(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:213
Definition: jwt_verifier.h:47
Definition: jwt_verifier.cc:178
Definition: jwt_verifier.h:45
void grpc_jwt_claims_destroy(grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:192
gpr_timespec grpc_jwt_verifier_clock_skew
Definition: jwt_verifier.cc:391
Definition: json.h:32
Definition: jwt_verifier.h:42
const char * email_domain
Definition: jwt_verifier.h:76
gpr_timespec grpc_jwt_claims_not_before(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:233
Definition: jwt_verifier.h:43
Analogous to struct timespec.
Definition: gpr_types.h:47
const grpc_json * grpc_jwt_claims_json(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:198
const char * grpc_jwt_claims_issuer(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:208
const char * key_url_prefix
Definition: jwt_verifier.h:81
grpc_jwt_verifier * grpc_jwt_verifier_create(const grpc_jwt_verifier_email_domain_key_url_mapping *mappings, size_t num_mappings)
Definition: jwt_verifier.cc:906
Definition: jwt_verifier.h:46
gpr_timespec grpc_jwt_claims_expires_at(const grpc_jwt_claims *claims)
Definition: jwt_verifier.cc:228