コード例 #1
0
ファイル: security_connector.c プロジェクト: DavidCnaup/grpc
grpc_auth_context *tsi_ssl_peer_to_auth_context(const tsi_peer *peer) {
  size_t i;
  grpc_auth_context *ctx = NULL;
  const char *peer_identity_property_name = NULL;

  /* The caller has checked the certificate type property. */
  GPR_ASSERT(peer->property_count >= 1);
  ctx = grpc_auth_context_create(NULL);
  grpc_auth_context_add_cstring_property(
      ctx, GRPC_TRANSPORT_SECURITY_TYPE_PROPERTY_NAME,
      GRPC_SSL_TRANSPORT_SECURITY_TYPE);
  for (i = 0; i < peer->property_count; i++) {
    const tsi_peer_property *prop = &peer->properties[i];
    if (prop->name == NULL) continue;
    if (strcmp(prop->name, TSI_X509_SUBJECT_COMMON_NAME_PEER_PROPERTY) == 0) {
      /* If there is no subject alt name, have the CN as the identity. */
      if (peer_identity_property_name == NULL) {
        peer_identity_property_name = GRPC_X509_CN_PROPERTY_NAME;
      }
      grpc_auth_context_add_property(ctx, GRPC_X509_CN_PROPERTY_NAME,
                                     prop->value.data, prop->value.length);
    } else if (strcmp(prop->name,
                      TSI_X509_SUBJECT_ALTERNATIVE_NAME_PEER_PROPERTY) == 0) {
      peer_identity_property_name = GRPC_X509_SAN_PROPERTY_NAME;
      grpc_auth_context_add_property(ctx, GRPC_X509_SAN_PROPERTY_NAME,
                                     prop->value.data, prop->value.length);
    }
  }
  if (peer_identity_property_name != NULL) {
    GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(
                   ctx, peer_identity_property_name) == 1);
  }
  return ctx;
}
コード例 #2
0
static void process_oauth2_success(void *state, grpc_auth_context *ctx,
                                   const grpc_metadata *md, size_t md_count,
                                   grpc_process_auth_metadata_done_cb cb,
                                   void *user_data) {
  const grpc_metadata *oauth2 =
      find_metadata(md, md_count, "Authorization", oauth2_md);
  GPR_ASSERT(state == NULL);
  GPR_ASSERT(oauth2 != NULL);
  grpc_auth_context_add_cstring_property(ctx, client_identity_property_name,
                                         client_identity);
  GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(
                 ctx, client_identity_property_name) == 1);
  cb(user_data, oauth2, 1, NULL, 0, GRPC_STATUS_OK, NULL);
}
コード例 #3
0
static void test_chained_context(void) {
  grpc_auth_context *chained = grpc_auth_context_create(NULL);
  grpc_auth_context *ctx = grpc_auth_context_create(chained);
  grpc_auth_property_iterator it;
  size_t i;

  gpr_log(GPR_INFO, "test_chained_context");
  GRPC_AUTH_CONTEXT_UNREF(chained, "chained");
  grpc_auth_context_add_cstring_property(chained, "name", "padapo");
  grpc_auth_context_add_cstring_property(chained, "foo", "baz");
  grpc_auth_context_add_cstring_property(ctx, "name", "chapi");
  grpc_auth_context_add_cstring_property(ctx, "name", "chap0");
  grpc_auth_context_add_cstring_property(ctx, "foo", "bar");
  GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx, "name") ==
             1);

  GPR_ASSERT(
      strcmp(grpc_auth_context_peer_identity_property_name(ctx), "name") == 0);
  it = grpc_auth_context_property_iterator(ctx);
  for (i = 0; i < ctx->properties.count; i++) {
    const grpc_auth_property *p = grpc_auth_property_iterator_next(&it);
    GPR_ASSERT(p == &ctx->properties.array[i]);
  }
  for (i = 0; i < chained->properties.count; i++) {
    const grpc_auth_property *p = grpc_auth_property_iterator_next(&it);
    GPR_ASSERT(p == &chained->properties.array[i]);
  }
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);

  it = grpc_auth_context_find_properties_by_name(ctx, "foo");
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
             &ctx->properties.array[2]);
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
             &chained->properties.array[1]);
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);

  it = grpc_auth_context_peer_identity(ctx);
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
             &ctx->properties.array[0]);
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
             &ctx->properties.array[1]);
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) ==
             &chained->properties.array[0]);
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);

  GRPC_AUTH_CONTEXT_UNREF(ctx, "test");
}
コード例 #4
0
static void test_empty_context(void) {
  grpc_auth_context *ctx = grpc_auth_context_create(NULL);
  grpc_auth_property_iterator it;

  gpr_log(GPR_INFO, "test_empty_context");
  GPR_ASSERT(ctx != NULL);
  GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx) == NULL);
  it = grpc_auth_context_peer_identity(ctx);
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
  it = grpc_auth_context_property_iterator(ctx);
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
  it = grpc_auth_context_find_properties_by_name(ctx, "foo");
  GPR_ASSERT(grpc_auth_property_iterator_next(&it) == NULL);
  GPR_ASSERT(grpc_auth_context_set_peer_identity_property_name(ctx, "bar") ==
             0);
  GPR_ASSERT(grpc_auth_context_peer_identity_property_name(ctx) == NULL);
  GRPC_AUTH_CONTEXT_UNREF(ctx, "test");
}