Backend & Security

Go RBAC API — a reusable authorization service for microservices

A production-oriented RBAC and authorization service built with Go, PostgreSQL, REST, gRPC, service-to-service authentication, local sessions, audit logging, OpenAPI docs and a reusable Go client SDK.

Client
Open-source project
My role
Design, backend engineering & documentation
  • Go
  • Echo
  • gRPC
  • PostgreSQL
  • GORM
  • Docker
  • OpenAPI
  • Zap
  • OpenTelemetry

REST + gRPC

two API surfaces, one authorization model

16

features shipped, from sessions to an SDK

1 call

Authorize answers any permission question

401 · 403 · 503

explicit, fail-closed error contract

The challenge

Authorization logic becomes hard to maintain when every backend service implements its own role checks, permission naming, audit behaviour and access rules.

Identity providers such as Keycloak are powerful, but complex RBAC customization can be difficult to configure and operate consistently across many services.

Teams need a centralized authorization service that is easy to integrate, does not leak secrets, and keeps a record of every decision it makes.

The core question

Can this user perform this action on this resource?

What I built

  1. 01

    Centralized roles & permissions

    Service-aware roles and permissions stored in PostgreSQL, with user-role and role-permission assignment managed through the API.

  2. 02

    Authorization decision API

    A single Authorize call over REST or gRPC resolves the user, the permission and the matching role, and explains its answer with a reason.

  3. 03

    Service-to-service authentication

    Each calling service authenticates with its own key. Keys are hashed in the database and the plain key is returned only once.

  4. 04

    Local sessions & external identity

    Login, refresh and logout with opaque, hashed sessions — or sync users from an external provider such as Keycloak by provider subject.

  5. 05

    Audit logging

    Every authorization decision is written to an audit log, allowed or denied, without ever recording secrets.

  6. 06

    Go client SDK & billing example

    A reusable rbacclient package plus an example billing service that only creates invoices when RBAC returns allowed=true.

Architecture

Client / Frontend

User interaction

Billing Service

Business logic

RBAC Service

Authorization decisions

PostgreSQL

Roles, policies & audit data

External services never touch the RBAC database directly — they talk to the service over REST or gRPC, either directly or through the Go client SDK.

Key decisions and why

  • Separate authentication from authorization

    Callers prove who they are first; RBAC only answers what they may do. External JWTs are validated by the calling service before SyncIdentity or Authorize.

  • Fail closed, with an explicit contract

    The billing middleware returns 401 when authentication is missing, 403 when permission is denied and 503 when RBAC is unavailable — it never defaults to allow.

  • Never store or expose secrets in plain text

    Service keys, sessions and passwords are hashed. Service keys are compared in constant time and must never reach frontend, mobile or browser clients.

  • Make integration boring

    OpenAPI and Swagger UI, a Postman collection, a smoke-test script and a Go SDK mean another team can integrate without reading the source.

Code from the build

Fail-closed RBAC middleware (billing service)go
result, err := client.Authorize(r.Context(), req)
if err != nil || result == nil {
    writeError(w, http.StatusServiceUnavailable,
        "RBAC_UNAVAILABLE", "authorization service unavailable", "")
    return
}
if !result.Allowed {
    writeError(w, http.StatusForbidden,
        "PERMISSION_DENIED", "permission denied", result.Reason)
    return
}

next.ServeHTTP(w, r)

If RBAC cannot answer, the request does not go through. Unavailable is a 503, never an implicit allow.

Service keys: hashed at rest, compared in constant timego
func Validate(plainKey string, hash string) bool {
    if plainKey == "" || hash == "" {
        return false
    }

    calculated := Hash(plainKey)
    return subtle.ConstantTimeCompare([]byte(calculated), []byte(hash)) == 1
}

func Hash(plainKey string) string {
    sum := sha256.Sum256([]byte(strings.TrimSpace(plainKey)))
    return hex.EncodeToString(sum[:])
}

The database only ever sees a SHA-256 digest, and ConstantTimeCompare avoids leaking information through timing.

One RPC for every permission questionproto
package rbac.v1;

service RBACService {
  rpc Authorize(AuthorizeRequest) returns (AuthorizeResponse);
}

The same decision is exposed over REST for simple callers and gRPC for internal services.

Results

  • External services check permissions with a single Authorize call instead of duplicating role logic.
  • Works with local users or with external identity providers through identity sync.
  • Every decision is audited, and the service fails closed by design.
  • Ships with OpenAPI docs, Swagger UI, a Postman collection, a smoke test and a reusable Go SDK.
  • Provides a clean, documented foundation for RBAC across a microservice system.

Questions people ask

Why build a separate RBAC service instead of using Keycloak roles?

Keycloak is excellent at identity, but complex, service-aware permission models are hard to customize and operate across many services. Keeping authorization in a small dedicated service makes the rules explicit, testable and auditable — and it can still sync identities from Keycloak.

How does another service integrate with it?

The service authenticates with its own service key, then calls Authorize over REST or gRPC — directly or through the pkg/rbacclient Go SDK. The billing example shows the full flow in a few lines of middleware.

What happens if the RBAC service is down?

Callers fail closed. The example middleware returns 503 RBAC_UNAVAILABLE and the protected action never runs.

Is it production ready?

It is production-oriented: hashed secrets, fail-closed behaviour, audit logs, tracing and metrics. Before production you would add TLS or mTLS between services and service-key rotation, as the security notes in the repo describe.