3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/security/credentials/credentials.h"
26 #include "src/core/lib/channel/channel_args.h"
27 #include "src/core/lib/gpr/string.h"
28 #include "src/core/lib/http/httpcli.h"
29 #include "src/core/lib/http/parser.h"
30 #include "src/core/lib/iomgr/executor.h"
31 #include "src/core/lib/json/json.h"
32 #include "src/core/lib/surface/api_trace.h"
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/log.h>
36 #include <grpc/support/string_util.h>
37 #include <grpc/support/sync.h>
38 #include <grpc/support/time.h>
42 void grpc_channel_credentials_release(grpc_channel_credentials* creds) {
43 GRPC_API_TRACE("grpc_channel_credentials_release(creds=%p)", 1, (creds));
44 grpc_core::ExecCtx exec_ctx;
45 if (creds) creds->Unref();
48 void grpc_call_credentials_release(grpc_call_credentials* creds) {
49 GRPC_API_TRACE("grpc_call_credentials_release(creds=%p)", 1, (creds));
50 grpc_core::ExecCtx exec_ctx;
51 if (creds) creds->Unref();
54 static void credentials_pointer_arg_destroy(void* p) {
55 static_cast<grpc_channel_credentials*>(p)->Unref();
58 static void* credentials_pointer_arg_copy(void* p) {
59 return static_cast<grpc_channel_credentials*>(p)->Ref().release();
62 static int credentials_pointer_cmp(void* a, void* b) { return GPR_ICMP(a, b); }
64 static const grpc_arg_pointer_vtable credentials_pointer_vtable = {
65 credentials_pointer_arg_copy, credentials_pointer_arg_destroy,
66 credentials_pointer_cmp};
68 grpc_arg grpc_channel_credentials_to_arg(
69 grpc_channel_credentials* credentials) {
70 return grpc_channel_arg_pointer_create(
71 const_cast<char*>(GRPC_ARG_CHANNEL_CREDENTIALS), credentials,
72 &credentials_pointer_vtable);
75 grpc_channel_credentials* grpc_channel_credentials_from_arg(
76 const grpc_arg* arg) {
77 if (strcmp(arg->key, GRPC_ARG_CHANNEL_CREDENTIALS) != 0) return nullptr;
78 if (arg->type != GRPC_ARG_POINTER) {
79 gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
80 GRPC_ARG_CHANNEL_CREDENTIALS);
83 return static_cast<grpc_channel_credentials*>(arg->value.pointer.p);
86 grpc_channel_credentials* grpc_channel_credentials_find_in_args(
87 const grpc_channel_args* args) {
89 if (args == nullptr) return nullptr;
90 for (i = 0; i < args->num_args; i++) {
91 grpc_channel_credentials* credentials =
92 grpc_channel_credentials_from_arg(&args->args[i]);
93 if (credentials != nullptr) return credentials;
98 void grpc_server_credentials_release(grpc_server_credentials* creds) {
99 GRPC_API_TRACE("grpc_server_credentials_release(creds=%p)", 1, (creds));
100 grpc_core::ExecCtx exec_ctx;
101 if (creds) creds->Unref();
104 void grpc_server_credentials::set_auth_metadata_processor(
105 const grpc_auth_metadata_processor& processor) {
107 "grpc_server_credentials_set_auth_metadata_processor("
109 "processor=grpc_auth_metadata_processor { process: %p, state: %p })",
110 3, (this, (void*)(intptr_t)processor.process, processor.state));
112 processor_ = processor;
115 void grpc_server_credentials_set_auth_metadata_processor(
116 grpc_server_credentials* creds, grpc_auth_metadata_processor processor) {
117 GPR_DEBUG_ASSERT(creds != nullptr);
118 creds->set_auth_metadata_processor(processor);
121 static void server_credentials_pointer_arg_destroy(void* p) {
122 static_cast<grpc_server_credentials*>(p)->Unref();
125 static void* server_credentials_pointer_arg_copy(void* p) {
126 return static_cast<grpc_server_credentials*>(p)->Ref().release();
129 static int server_credentials_pointer_cmp(void* a, void* b) {
130 return GPR_ICMP(a, b);
133 static const grpc_arg_pointer_vtable cred_ptr_vtable = {
134 server_credentials_pointer_arg_copy, server_credentials_pointer_arg_destroy,
135 server_credentials_pointer_cmp};
137 grpc_arg grpc_server_credentials_to_arg(grpc_server_credentials* c) {
138 return grpc_channel_arg_pointer_create(
139 const_cast<char*>(GRPC_SERVER_CREDENTIALS_ARG), c, &cred_ptr_vtable);
142 grpc_server_credentials* grpc_server_credentials_from_arg(const grpc_arg* arg) {
143 if (strcmp(arg->key, GRPC_SERVER_CREDENTIALS_ARG) != 0) return nullptr;
144 if (arg->type != GRPC_ARG_POINTER) {
145 gpr_log(GPR_ERROR, "Invalid type %d for arg %s", arg->type,
146 GRPC_SERVER_CREDENTIALS_ARG);
149 return static_cast<grpc_server_credentials*>(arg->value.pointer.p);
152 grpc_server_credentials* grpc_find_server_credentials_in_args(
153 const grpc_channel_args* args) {
155 if (args == nullptr) return nullptr;
156 for (i = 0; i < args->num_args; i++) {
157 grpc_server_credentials* p =
158 grpc_server_credentials_from_arg(&args->args[i]);
159 if (p != nullptr) return p;