xref: /aosp_15_r20/external/grpc-grpc/doc/server_side_auth.md (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1*cc02d7e2SAndroid Build Coastguard WorkerServer-side API for Authenticating Clients
2*cc02d7e2SAndroid Build Coastguard Worker==========================================
3*cc02d7e2SAndroid Build Coastguard Worker
4*cc02d7e2SAndroid Build Coastguard WorkerNOTE: 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*cc02d7e2SAndroid Build Coastguard Worker
6*cc02d7e2SAndroid Build Coastguard WorkerNOTE2: `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*cc02d7e2SAndroid Build Coastguard Worker## AuthContext
8*cc02d7e2SAndroid Build Coastguard Worker
9*cc02d7e2SAndroid Build Coastguard WorkerTo 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*cc02d7e2SAndroid Build Coastguard Worker
11*cc02d7e2SAndroid Build Coastguard WorkerThe 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*cc02d7e2SAndroid Build Coastguard Worker
13*cc02d7e2SAndroid Build Coastguard WorkerThe 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*cc02d7e2SAndroid Build Coastguard Worker
15*cc02d7e2SAndroid Build Coastguard WorkerNote 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)).
16*cc02d7e2SAndroid Build Coastguard WorkerHowever, 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*cc02d7e2SAndroid Build Coastguard Worker
18*cc02d7e2SAndroid Build Coastguard WorkerWARNING: 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*cc02d7e2SAndroid Build Coastguard Worker
20*cc02d7e2SAndroid Build Coastguard Worker#### Example AuthContext contents
21*cc02d7e2SAndroid Build Coastguard Worker
22*cc02d7e2SAndroid Build Coastguard WorkerFor secure channel using mutual TLS authentication with both client and server certificates (test certificates from this repository are used).
23*cc02d7e2SAndroid Build Coastguard Worker
24*cc02d7e2SAndroid Build Coastguard WorkerPopulated auth properties:
25*cc02d7e2SAndroid Build Coastguard Worker```
26*cc02d7e2SAndroid Build Coastguard Worker"transport_security_type": "ssl"  # connection is secured using TLS/SSL
27*cc02d7e2SAndroid Build Coastguard Worker"x509_common_name": "*.test.google.com"  # from client's certificate
28*cc02d7e2SAndroid Build Coastguard Worker"x509_pem_cert": "-----BEGIN CERTIFICATE-----\n..."  # client's PEM encoded certificate
29*cc02d7e2SAndroid Build Coastguard Worker"x509_subject_alternative_name": "*.test.google.fr"
30*cc02d7e2SAndroid Build Coastguard Worker"x509_subject_alternative_name": "waterzooi.test.google.be"
31*cc02d7e2SAndroid Build Coastguard Worker"x509_subject_alternative_name": "*.test.youtube.com"
32*cc02d7e2SAndroid Build Coastguard Worker"x509_subject_alternative_name": "192.168.1.3"
33*cc02d7e2SAndroid Build Coastguard Worker```
34*cc02d7e2SAndroid Build Coastguard Worker
35*cc02d7e2SAndroid Build Coastguard WorkerThe peer identity is set of all properties named `"x509_subject_alternative_name"`:
36*cc02d7e2SAndroid Build Coastguard Worker```
37*cc02d7e2SAndroid Build Coastguard Workerpeer_identity_property_name = "x509_subject_alternative_name"
38*cc02d7e2SAndroid Build Coastguard Worker```
39*cc02d7e2SAndroid Build Coastguard Worker
40*cc02d7e2SAndroid Build Coastguard Worker## AuthProperty
41*cc02d7e2SAndroid Build Coastguard Worker
42*cc02d7e2SAndroid Build Coastguard WorkerAuth 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*cc02d7e2SAndroid Build Coastguard Worker
44*cc02d7e2SAndroid Build Coastguard Worker## Auth Interceptors
45*cc02d7e2SAndroid Build Coastguard Worker
46*cc02d7e2SAndroid Build Coastguard WorkerAuth interceptors are gRPC components that populate contents of the auth context based on gRPC's internal state and/or call metadata.
47*cc02d7e2SAndroid Build Coastguard WorkergRPC comes with some basic "interceptors" already built-in.
48*cc02d7e2SAndroid Build Coastguard Worker
49*cc02d7e2SAndroid Build Coastguard WorkerWARNING: While there is a public API that allows anyone to write their own custom interceptor, please think twice before using it.
50*cc02d7e2SAndroid Build Coastguard WorkerThere 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*cc02d7e2SAndroid Build Coastguard Worker
52*cc02d7e2SAndroid Build Coastguard Worker#### Available auth interceptors
53*cc02d7e2SAndroid Build Coastguard Worker- TLS/SSL certificate authentication (built into gRPC's security layer, automatically used whenever you use a secure connection)
54*cc02d7e2SAndroid Build Coastguard Worker- (coming soon) JWT auth token authentication
55*cc02d7e2SAndroid Build Coastguard Worker- more will be added over time
56*cc02d7e2SAndroid Build Coastguard Worker
57*cc02d7e2SAndroid Build Coastguard Worker## Status (by language)
58*cc02d7e2SAndroid Build Coastguard WorkerC-core exposes low level API to access auth context contents and to implement an auth interceptor.
59*cc02d7e2SAndroid Build Coastguard WorkerIn C++, the auth interceptor API is exposed as `AuthMetadataProcessor`.
60*cc02d7e2SAndroid Build Coastguard Worker
61*cc02d7e2SAndroid Build Coastguard WorkerA high level API to access AuthContext contents is available in these languages:
62*cc02d7e2SAndroid Build Coastguard Worker- C++
63*cc02d7e2SAndroid Build Coastguard Worker- C# (implementation in-progress)
64*cc02d7e2SAndroid Build Coastguard Worker- other languages coming soon
65