xref: /aosp_15_r20/external/grpc-grpc/doc/server_side_auth.md (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1Server-side API for Authenticating Clients
2==========================================
3
4NOTE: This document describes how server-side authentication works in C-core based gRPC implementations only. In gRPC Java and Go, server side authentication is handled differently.
5
6NOTE2: `CallCredentials` class is only valid if the security level it requires is less than or equal to the security level of the connection used to transfer it. See the [gRFC](https://github.com/grpc/proposal/blob/master/L62-core-call-credential-security-level.md) for more information.
7## AuthContext
8
9To perform server-side authentication, gRPC exposes the *authentication context* for each call. The context exposes important authentication-related information about the RPC such as the type of security/authentication type being used and the peer identity.
10
11The authentication context is structured as a multi-map of key-value pairs - the *auth properties*. In addition to that, for authenticated RPCs, the set of properties corresponding to a selected key will represent the verified identity of the caller - the *peer identity*.
12
13The contents of the *auth properties* are populated by an *auth interceptor*. The interceptor also chooses which property key will act as the peer identity (e.g. for client certificate authentication this property will be `"x509_common_name"` or `"x509_subject_alternative_name"`).
14
15Note that AuthContext is generally not modifiable, except when used via an AuthMetadataProcessor([reference](https://github.com/grpc/grpc/blob/master/include/grpcpp/security/auth_context.h)).
16However, because the AuthContext is a connection-level object, when it is modified via an AuthMetadataProcessor, the modifications will be visible on all subsequent calls on the same connection.
17
18WARNING: AuthContext is the only reliable source of truth when it comes to authenticating RPCs. Using any other call/context properties for authentication purposes is wrong and inherently unsafe.
19
20#### Example AuthContext contents
21
22For secure channel using mutual TLS authentication with both client and server certificates (test certificates from this repository are used).
23
24Populated auth properties:
25```
26"transport_security_type": "ssl"  # connection is secured using TLS/SSL
27"x509_common_name": "*.test.google.com"  # from client's certificate
28"x509_pem_cert": "-----BEGIN CERTIFICATE-----\n..."  # client's PEM encoded certificate
29"x509_subject_alternative_name": "*.test.google.fr"
30"x509_subject_alternative_name": "waterzooi.test.google.be"
31"x509_subject_alternative_name": "*.test.youtube.com"
32"x509_subject_alternative_name": "192.168.1.3"
33```
34
35The peer identity is set of all properties named `"x509_subject_alternative_name"`:
36```
37peer_identity_property_name = "x509_subject_alternative_name"
38```
39
40## AuthProperty
41
42Auth properties are elements of the AuthContext. They have a name (a key of type string) and a value which can be a string or binary data.
43
44## Auth Interceptors
45
46Auth interceptors are gRPC components that populate contents of the auth context based on gRPC's internal state and/or call metadata.
47gRPC comes with some basic "interceptors" already built-in.
48
49WARNING: While there is a public API that allows anyone to write their own custom interceptor, please think twice before using it.
50There are legitimate uses for custom interceptors but you should keep in mind that as auth interceptors essentially decide which RPCs are authenticated and which are not, their code is very sensitive from the security perspective and getting things wrong might have serious consequences. If unsure, we strongly recommend to rely on official & proven interceptors that come with gRPC.
51
52#### Available auth interceptors
53- TLS/SSL certificate authentication (built into gRPC's security layer, automatically used whenever you use a secure connection)
54- (coming soon) JWT auth token authentication
55- more will be added over time
56
57## Status (by language)
58C-core exposes low level API to access auth context contents and to implement an auth interceptor.
59In C++, the auth interceptor API is exposed as `AuthMetadataProcessor`.
60
61A high level API to access AuthContext contents is available in these languages:
62- C++
63- C# (implementation in-progress)
64- other languages coming soon
65