Part 4: Authentication and Authorization in gRPC

The $75,000 Security Lesson

In March 2022, I deployed a gRPC microservices system for a fintech client. Authentication was "on my todo list." Within 72 hours, an attacker discovered an unprotected gRPC endpoint and drained test funds from merchant accounts. While it was test money, the incident cost approximately $75,000 in emergency security audits, legal reviews, and implementation time.

The mistake? I assumed gRPC services behind a firewall didn't need authentication. I was catastrophically wrong. This part covers everything I learned from that painful lesson.

Authentication Strategies for gRPC

1. JWT-Based Authentication (My Standard)

// protos/common/auth.proto
syntax = "proto3";

package common.auth.v1;

message AuthToken {
  string access_token = 1;
  string refresh_token = 2;
  int64 expires_at = 3;
}

message User {
  string id = 1;
  string email = 2;
  repeated string roles = 3;
  map<string, string> permissions = 4;
}

JWT Service Implementation

Authentication Interceptor

Using Auth Interceptor in Server

Client Authentication

Authorization Strategies

1. Role-Based Access Control (RBAC)

Authorization Interceptor

Resource-Level Authorization

Handler with Authorization

API Key Authentication (Service-to-Service)

API Key Service

API Key Interceptor

Mutual TLS (mTLS) Authentication

Server Configuration

Server with mTLS

Client with mTLS

Rate Limiting

Complete Server with All Interceptors

Best Practices from Production

1. Token Rotation

2. Audit Logging

Key Takeaways

  1. Defense in Depth: Use multiple security layers (authentication + authorization + rate limiting)

  2. JWT Best Practices: Short-lived access tokens (15 min), longer refresh tokens (7 days)

  3. mTLS for Services: Use mutual TLS for service-to-service communication

  4. API Keys for Services: Service accounts should use API keys, not user JWT

  5. Rate Limiting: Protect against abuse and DoS attacks

  6. Audit Everything: Log all authentication and authorization events

  7. Never Trust: Validate every request, even from internal services

My Lesson: The $75,000 incident taught me that security isn't optional. Implement it from day one.


Next: Part 5: Error Handling and Interceptors in gRPC

Series Navigation: gRPC 101 Series

Last updated