1# Copyright 2016 gRPC authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from libc.string cimport memcpy
16
17import pkg_resources
18
19
20cdef grpc_ssl_roots_override_result ssl_roots_override_callback(
21    char **pem_root_certs) nogil:
22  with gil:
23    temporary_pem_root_certs = pkg_resources.resource_string(
24        __name__.rstrip('.cygrpc'), '_credentials/roots.pem')
25    pem_root_certs[0] = <char *>gpr_malloc(len(temporary_pem_root_certs) + 1)
26    memcpy(
27        pem_root_certs[0], <char *>temporary_pem_root_certs,
28        len(temporary_pem_root_certs))
29    pem_root_certs[0][len(temporary_pem_root_certs)] = '\0'
30
31  return GRPC_SSL_ROOTS_OVERRIDE_OK
32
33
34def peer_identities(Call call):
35  cdef grpc_auth_context* auth_context
36  cdef grpc_auth_property_iterator properties
37  cdef const grpc_auth_property* property
38
39  auth_context = grpc_call_auth_context(call.c_call)
40  if auth_context == NULL:
41    return None
42  properties = grpc_auth_context_peer_identity(auth_context)
43  identities = []
44  while True:
45    property = grpc_auth_property_iterator_next(&properties)
46    if property == NULL:
47      break
48    if property.value != NULL:
49      identities.append(<bytes>(property.value))
50  grpc_auth_context_release(auth_context)
51  return identities if identities else None
52
53def peer_identity_key(Call call):
54  cdef grpc_auth_context* auth_context
55  cdef const char* c_key
56  auth_context = grpc_call_auth_context(call.c_call)
57  if auth_context == NULL:
58    return None
59  c_key = grpc_auth_context_peer_identity_property_name(auth_context)
60  if c_key == NULL:
61    key = None
62  else:
63    key = <bytes> grpc_auth_context_peer_identity_property_name(auth_context)
64  grpc_auth_context_release(auth_context)
65  return key
66
67def auth_context(Call call):
68  cdef grpc_auth_context* auth_context
69  cdef grpc_auth_property_iterator properties
70  cdef const grpc_auth_property* property
71
72  auth_context = grpc_call_auth_context(call.c_call)
73  if auth_context == NULL:
74    return {}
75  properties = grpc_auth_context_property_iterator(auth_context)
76  py_auth_context = {}
77  while True:
78    property = grpc_auth_property_iterator_next(&properties)
79    if property == NULL:
80      break
81    if property.name != NULL and property.value != NULL:
82      key = <bytes> property.name
83      if key in py_auth_context:
84        py_auth_context[key].append(<bytes>(property.value))
85      else:
86        py_auth_context[key] = [<bytes> property.value]
87  grpc_auth_context_release(auth_context)
88  return py_auth_context
89
90