Implementing Authentication

1. Using TLS Credentials

FormDetail
Channel-levelEncrypt all RPCs on connection
Per-RPCgrpc.PerRPCCredentials on top of TLS
mTLSIdentity carried in client cert

2. Implementing Token-Based Auth

Example: PerRPCCredentials with token

type tokenAuth struct{ token string }
func (t tokenAuth) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error) {
    return map[string]string{"authorization": "Bearer " + t.token}, nil
}
func (tokenAuth) RequireTransportSecurity() bool { return true }

conn, _ := grpc.NewClient(addr,
    grpc.WithTransportCredentials(credentials.NewTLS(cfg)),
    grpc.WithPerRPCCredentials(tokenAuth{tok}),
)

3. Creating Custom Credentials

InterfaceMethod
PerRPCCredentialsGetRequestMetadata(ctx, uri...)
SecurityRequireTransportSecurity() bool
RefreshImplement inside GetRequestMetadata

4. Validating Tokens in Interceptor

Example: JWT verification

func authInterceptor(jwks jwt.Keyfunc) grpc.UnaryServerInterceptor {
    return func(ctx context.Context, req any, info *grpc.UnaryServerInfo,
        h grpc.UnaryHandler) (any, error) {
        md, _ := metadata.FromIncomingContext(ctx)
        raw := strings.TrimPrefix(first(md, "authorization"), "Bearer ")
        tok, err := jwt.Parse(raw, jwks)
        if err != nil || !tok.Valid {
            return nil, status.Error(codes.Unauthenticated, "invalid token")
        }
        ctx = context.WithValue(ctx, claimsKey{}, tok.Claims)
        return h(ctx, req)
    }
}

5. Implementing JWT Authentication

StepDetail
Discover JWKSFrom /.well-known/jwks.json
Cache keysTTL with background refresh
VerifySignature, iss, aud, exp
Inject claimsInto ctx for downstream

6. Setting Insecure Connection

Example: Insecure for local dev

import "google.golang.org/grpc/credentials/insecure"
conn, _ := grpc.NewClient("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
UseWarning
Local dev onlyNever in production
PerRPC creds blockedIf RequireTransportSecurity=true

7. Using Transport Credentials

TypeUse
credentials.NewTLSStandard TLS
alts.NewServerCreds / NewClientCredsGCP ALTS
insecure.NewCredentialsNone

8. Implementing OAuth2

Example: oauth2 token source

import (
    "golang.org/x/oauth2/google"
    "google.golang.org/grpc/credentials/oauth"
)
ts, _ := google.DefaultTokenSource(ctx, "https://www.googleapis.com/auth/cloud-platform")
conn, _ := grpc.NewClient(addr,
    grpc.WithTransportCredentials(credentials.NewTLS(cfg)),
    grpc.WithPerRPCCredentials(oauth.TokenSource{TokenSource: ts}),
)

9. Extracting Peer Information

Example: Peer info on server

p, _ := peer.FromContext(ctx)
fmt.Println("client addr", p.Addr)
if t, ok := p.AuthInfo.(credentials.TLSInfo); ok {
    if len(t.State.VerifiedChains) > 0 {
        fmt.Println("subject", t.State.VerifiedChains[0][0].Subject)
    }
}

10. Implementing API Key Auth

StepDetail
Headerx-api-key via metadata
ValidateHash compare against store; check active & scopes
Rate limitPer-key bucket
Always TLSNever send keys in plaintext