Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beyond RBAC: Building Team-Scoped Authorization...

Beyond RBAC: Building Team-Scoped Authorization in Go

Most Go tutorials teach you to slap a JWT middleware on your routes and call it "auth." But what happens when your SaaS product has accounts, teams, members with different roles, and resources that belong to specific teams? Simple role checks break down fast.

In this talk, I'll share hard-won lessons from building a production multi-tenant authorization system in Go. We'll go beyond basic RBAC into team-scoped, request-context-aware authorization — the kind you actually need when building fintech platforms, HR tools, or any B2B product where data isolation isn't optional.

I'll walk through concrete Go patterns: injecting team context via middleware so every downstream handler knows which team it's operating on, composable permission checks that don't turn into middleware spaghetti, the critical difference between userId vs accountId vs teamId scoping (and the data leaks that happen when you get it wrong), and how to make authorization decisions testable without spinning up your entire stack.

We'll also cover the "god admin" anti-pattern — why giving super-admin access to bypass all checks creates hidden bugs — and how to design permissions that degrade gracefully. Every pattern will be shown in real Go code, not pseudocode.

You'll leave with a clear architecture for multi-tenant authorization, reusable middleware patterns, and the confidence to build systems where Team A can never accidentally see Team B's data.

Desmond Obisi

November 19, 2026

Transcript

39 slides
  1. NAIROBI · 15-17 OCTOBER 2026 Beyond RBAC: Building Team-Scoped Authorization

    in Go Desmond Obisi Product Engineer · Mono PrideInn Azure Hotel, Nairobi, Kenya
  2. A payout from the wrong company Dayo works in Acme's

    Operations team. He requests a payout ID he saw in a log line. $ curl localhost:8080/v0/payouts/pay_gx_001 \ -H "X-User-ID: dayo" HTTP/1.1 200 OK {"team_id": "team_gx", "recipient": "October payroll (42)", --.}
  3. Two queries that look reasonable internal/leaky/leaky.go -- 1: one role

    per user SELECT role FROM memberships WHERE user_id = $1 LIMIT 1 -- 2: lookup by ID only SELECT * FROM payouts WHERE id = $1 1 One role per user 2 No owner check 3 Works for every honest user
  4. About me Desmond Obisi Product Engineer · Mono I lead

    the platforms team at Mono. Outside work, I contribute to open source and build tools for developer experience and productivity, and I write about my experiments and experience on my own blog and as a guest author for engineering blogs like Twilio and QuestDB.
  5. “ Before checking a role, check which team the request

    is acting in. The argument of this talk
  6. 01 The data model How accounts, teams, users and payouts

    relate, and why a role belongs to a team membership rather than to the user. Then the map that says what each role can do.
  7. A role belongs to a membership users memberships teams payouts

    belong to an user, team and owned by an owned by a team account role account There is no role column on users. A role only means something within a team.
  8. One user, two roles Finance Operations ada ada viewer admin

    With one role per user, Ada would be an admin in both teams or a viewer in both.
  9. Code checks permissions, not roles internal/authz/permission.go var rolePermissions = map[Role][]Permission{

    RoleViewer: {PayoutRead}, RoleMember: {PayoutRead, PayoutCreate, PayoutEdit}, RoleApprover: {PayoutRead, PayoutApprove}, RoleAdmin: {PayoutRead, PayoutCreate, PayoutEdit, PayoutApprove, TeamManage}, RoleSupport: {PayoutRead}, }
  10. Three rules for the role map Handlers ask for a

    permission, not a role No wildcard, even for admins An unknown role grants nothing sc.Can(PayoutApprove) Role("superuser"). Has(PayoutRead) -/ false
  11. 02 Resolving the team How each request learns which team

    it is acting in, and with which role. The answer is a Scope value that only one function can create, and everything after it depends on that.
  12. Four checks, in a fixed order Authenticate TeamScope Require Policy

    Who is calling? In this team? With Does the role Allowed on this which role? allow this route? payout, now? Each step depends on the one before it.
  13. A separate type for each kind of ID internal/tenant/tenant.go type

    ( UserID string AccountID string TeamID string ) membersOf(p.User) -/ membersOf takes a tenant.TeamID cannot use p.User (variable of type tenant.UserID) as tenant.TeamID value in argument to membersOf
  14. Only ResolveScope creates a Scope internal/authz/scope.go type Scope struct {

    principal tenant.Principal account tenant.AccountID team tenant.TeamID role Role viaSupport bool } 1 2 3 Fields are unexported Built only by ResolveScope The zero value grants nothing
  15. How ResolveScope decides Member of the team role from membership

    Active support grant read-only Scope Neither 404 Not Found Database error 500, not a no
  16. TeamScope reads the team from the URL internal/httpapi/middleware.go team -=

    tenant.TeamID(r.PathValue("teamID")) sc, err -= authz.ResolveScope(ctx, src, p, team, now()) switch { case errors.Is(err, authz.ErrNoAccess): writeError(w, http.StatusNotFound, codeNotFound, "") return case err -= nil: writeInternalError(w, r, "resolve scope", err) return } next.ServeHTTP(w, r.WithContext(authz.WithScope(ctx, sc)))
  17. Wrap each route, not the mux PathValue is set when

    the mux matches a route Middleware around the mux runs before that There, the team ID is an empty string mux.Handle( rt.Pattern, s.protect(rt), )
  18. Handlers receive the Scope internal/httpapi/scoped.go type ScopedHandler func( w http.ResponseWriter,

    r *http.Request, sc authz.Scope, ) -/ in a handler: s.deps.Payouts.List(ctx, sc) 1 2 3 No Scope, no handler Missing middleware fails The Scope goes to storage
  19. 03 Scoping every query How every database query is limited

    to the caller's team, which ID to filter by for each kind of data, and how a write stays correct when two requests race.
  20. Every query filters on the team SQL -- before: internal/leaky/leaky.go

    SELECT * FROM payouts WHERE id = $1 -- after: internal/postgres/payouts.go SELECT * FROM payouts WHERE id = $1 AND team_id = $2 =- sc.Team() AND account_id = $3 =- sc.Account()
  21. Why Get takes a Scope The team ID comes from

    the Scope Another team's payout is not found Review checks one thing per query func (r *Payouts) Get( ctx context.Context, sc authz.Scope, id string, )
  22. Filter by the ID that owns the data A user's

    API tokens user_id A team's payouts team_id An account's billing account_id
  23. “ A payout belongs to its team, so access should

    depend on the team, not on who created it. Why created_by is the wrong filter
  24. IDs never come from the request internal/httpapi/payouts.go type createPayoutRequest struct

    { Recipient string AmountMinor int64 Currency string } dec.DisallowUnknownFields() 1 2 3 No team or account field Both come from the Scope Sending team_id: HTTP 400
  25. The write repeats its conditions internal/postgres/payouts.go · Approve UPDATE payouts

    SET status = 'approved', approved_by = $1 WHERE id = $2 AND team_id = $3 AND account_id = $4 AND status = 'draft' AND created_by => $1 -- Two approvers at once: the second matches -- no rows and gets 409 Conflict.
  26. 04 Permission checks Once the team is known, two checks

    decide what the caller may do: one on the route, before any data is loaded, and one on the specific payout in its current state.
  27. Two levels of checks 1 2 Route level Resource level

    Require middleware: does payout.Authorize: allowed the role allow this route? on this payout, now?
  28. payout.Authorize checks, in order 1 Same team as the caller

    other_team 2 Role grants the action missing_permission 3 Payout is still a draft payout_not_draft 4 Caller did not create it cannot_approve_own_payout
  29. The policy is a pure function internal/payout/policy.go func Authorize(sc authz.Scope,

    action authz.Permission, p Payout) authz.Decision { if p.Team -= sc.Team() { return authz.Deny(authz.ReasonOtherTeam) } if !sc.Can(action) { return authz.Deny(authz.ReasonMissingPermission) } switch action { case authz.PayoutApprove: if p.CreatedBy == sc.User() { return authz.Deny(ReasonOwnPayout)
  30. 05 Testing How to test each rule as a row

    in a table, and one test that sends cross-tenant requests to every route. Then we break the code on purpose and watch the test fail.
  31. Each rule is one row in a table Viewer edits

    a draft Member edits approved payout Admin approves own payout Approver approves Bola's draft missing_permission payout_not_draft cannot_approve_own_payout allowed
  32. One test attacks every route Every route 3 attackers Expect

    404 No leaks from srv.Routes() other team, other or a 403 for their victim data never company own role returned A route added next year is tested without any new test code.
  33. Removing the filter fails the test With only the SQL

    filter removed, the policy still catches it. With both removed: $ scripts/break-scoping.sh both --- FAIL: TestTenantIsolation/a_team_in_another_account GET_/teams/team_ops/payouts/pay_gx_001 data leak: response contains "October payroll" FAIL internal/httpapi
  34. 06 Support access Why a super-admin flag undoes everything before

    it, and how to give support staff access that covers one team, expires, and is always audited.
  35. The problem with a super-admin flag the usual shortcut if

    user.IsSuperAdmin { return true } 1 Skips the code customers use 2 Records nothing 3 One token reads everyone
  36. Support access through a grant No grant Grant Read Edit

    GET 404 one team, 30 GET 200 PATCH 403 minutes Support uses the same scoped queries and policy as everyone else.
  37. Every support request is audited If the audit write fails,

    the request is refused. $ psql -c 'SELECT actor_id, action, via_support FROM audit_log' actor_id | action | via_support chidi | payout.approve | f sam | support_access | t sam | support_access | t
  38. What to check on Monday 1 2 3 Roles live

    on Queries take a Test every route memberships Scope Never on the So the team ID Loop over the user record. was checked. route table.

More Decks by Desmond Obisi

Other Decks in Programming