gRPC  0.6.0
 All Classes Namespaces Functions Variables Enumerations Properties Pages
credentials.h
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_INTERNAL_CORE_SECURITY_CREDENTIALS_H
35 #define GRPC_INTERNAL_CORE_SECURITY_CREDENTIALS_H
36 
37 #include "src/core/transport/stream_op.h"
38 #include <grpc/grpc.h>
39 #include <grpc/grpc_security.h>
40 #include <grpc/support/sync.h>
41 
42 #include "src/core/security/security_connector.h"
43 
45 
46 /* --- Constants. --- */
47 
48 typedef enum {
49  GRPC_CREDENTIALS_OK = 0,
50  GRPC_CREDENTIALS_ERROR
51 } grpc_credentials_status;
52 
53 #define GRPC_CREDENTIALS_TYPE_SSL "Ssl"
54 #define GRPC_CREDENTIALS_TYPE_OAUTH2 "Oauth2"
55 #define GRPC_CREDENTIALS_TYPE_JWT "Jwt"
56 #define GRPC_CREDENTIALS_TYPE_IAM "Iam"
57 #define GRPC_CREDENTIALS_TYPE_COMPOSITE "Composite"
58 #define GRPC_CREDENTIALS_TYPE_FAKE_TRANSPORT_SECURITY "FakeTransportSecurity"
59 
60 #define GRPC_AUTHORIZATION_METADATA_KEY "Authorization"
61 #define GRPC_IAM_AUTHORIZATION_TOKEN_METADATA_KEY \
62  "x-goog-iam-authorization-token"
63 #define GRPC_IAM_AUTHORITY_SELECTOR_METADATA_KEY "x-goog-iam-authority-selector"
64 
65 #define GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY "gcloud"
66 #define GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE \
67  "application_default_credentials.json"
68 
69 #define GRPC_SECURE_TOKEN_REFRESH_THRESHOLD_SECS 60
70 
71 #define GRPC_COMPUTE_ENGINE_METADATA_HOST "metadata"
72 #define GRPC_COMPUTE_ENGINE_METADATA_TOKEN_PATH \
73  "/computeMetadata/v1/instance/service-accounts/default/token"
74 
75 #define GRPC_GOOGLE_OAUTH2_SERVICE_HOST "www.googleapis.com"
76 #define GRPC_GOOGLE_OAUTH2_SERVICE_TOKEN_PATH "/oauth2/v3/token"
77 
78 #define GRPC_SERVICE_ACCOUNT_POST_BODY_PREFIX \
79  "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&" \
80  "assertion="
81 
82 #define GRPC_REFRESH_TOKEN_POST_BODY_FORMAT_STRING \
83  "client_id=%s&client_secret=%s&refresh_token=%s&grant_type=refresh_token"
84 
85 /* --- grpc_credentials. --- */
86 
87 /* It is the caller's responsibility to gpr_free the result if not NULL. */
88 char *grpc_get_well_known_google_credentials_file_path(void);
89 
90 typedef void (*grpc_credentials_metadata_cb)(void *user_data,
91  grpc_mdelem **md_elems,
92  size_t num_md,
93  grpc_credentials_status status);
94 
95 typedef struct {
96  void (*destroy)(grpc_credentials *c);
97  int (*has_request_metadata)(const grpc_credentials *c);
98  int (*has_request_metadata_only)(const grpc_credentials *c);
99  grpc_mdctx *(*get_metadata_context)(grpc_credentials *c);
100  void (*get_request_metadata)(grpc_credentials *c,
101  const char *service_url,
102  grpc_credentials_metadata_cb cb,
103  void *user_data);
104  grpc_security_status (*create_security_connector)(
105  grpc_credentials *c, const char *target, const grpc_channel_args *args,
106  grpc_credentials *request_metadata_creds,
108 
110 
112  const grpc_credentials_vtable *vtable;
113  const char *type;
114  gpr_refcount refcount;
115 };
116 
117 grpc_credentials *grpc_credentials_ref(grpc_credentials *creds);
118 void grpc_credentials_unref(grpc_credentials *creds);
119 int grpc_credentials_has_request_metadata(grpc_credentials *creds);
120 int grpc_credentials_has_request_metadata_only(grpc_credentials *creds);
121 void grpc_credentials_get_request_metadata(grpc_credentials *creds,
122  const char *service_url,
123  grpc_credentials_metadata_cb cb,
124  void *user_data);
125 
126 /* Gets the mdctx from the credentials and increase the refcount if it exists,
127  otherwise, create a new one. */
128 grpc_mdctx *grpc_credentials_get_or_create_metadata_context(
129  grpc_credentials *creds);
130 
131 /* Creates a security connector for the channel. May also create new channel
132  args for the channel to be used in place of the passed in const args if
133  returned non NULL. In that case the caller is responsible for destroying
134  new_args after channel creation. */
135 grpc_security_status grpc_credentials_create_security_connector(
136  grpc_credentials *creds, const char *target, const grpc_channel_args *args,
137  grpc_credentials *request_metadata_creds,
139 
140 typedef struct {
141  grpc_credentials **creds_array;
142  size_t num_creds;
144 
145 const grpc_credentials_array *grpc_composite_credentials_get_credentials(
146  grpc_credentials *composite_creds);
147 
148 /* Returns creds if creds is of the specified type or the inner creds of the
149  specified type (if found), if the creds is of type COMPOSITE.
150  If composite_creds is not NULL, *composite_creds will point to creds if of
151  type COMPOSITE in case of success. */
152 grpc_credentials *grpc_credentials_contains_type(
153  grpc_credentials *creds, const char *type,
154  grpc_credentials **composite_creds);
155 
156 /* Exposed for testing only. */
157 grpc_credentials_status
158  grpc_oauth2_token_fetcher_credentials_parse_server_response(
159  const struct grpc_httpcli_response *response, grpc_mdctx *ctx,
160  grpc_mdelem **token_elem, gpr_timespec *token_lifetime);
161 
162 /* Simulates an oauth2 token fetch with the specified value for testing. */
163 grpc_credentials *grpc_fake_oauth2_credentials_create(
164  const char *token_md_value, int is_async);
165 
166 /* --- grpc_server_credentials. --- */
167 
168 typedef struct {
169  void (*destroy)(grpc_server_credentials *c);
170  grpc_security_status (*create_security_connector)(
173 
175  const grpc_server_credentials_vtable *vtable;
176  const char *type;
177 };
178 
179 grpc_security_status grpc_server_credentials_create_security_connector(
181 
182 #endif /* GRPC_INTERNAL_CORE_SECURITY_CREDENTIALS_H */
Definition: credentials.h:95
Definition: credentials.h:168
Definition: sync_generic.h:49
Definition: grpc.h:101
Definition: credentials.h:174
Definition: credentials.h:111
Definition: credentials.h:140
Definition: metadata.c:83
Definition: metadata.h:78
Definition: security_connector.h:75
Definition: security_connector.h:121
Definition: time.h:48
Definition: httpcli.h:68