ginmw

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ginmw provides Gin-compatible middleware for the api-security-sdk. It bridges the SDK's JWT, RBAC, and audit packages with the Gin web framework.

JWT authentication

svc := jwt.New(jwt.WithHMAC(secret))
r.Use(ginmw.JWT(svc))

Verify-only (Auth0 / external IdP)

src := jwks.Auth0("myapp.auth0.com")
svc := jwt.New(jwt.WithJWKS(src.KeyFunc))
r.Use(ginmw.JWT(svc))

RBAC middleware

enforcer := rbac.New(store)
r.GET("/admin", ginmw.RequireRole(enforcer, "admin"), handler)
r.DELETE("/posts/:id", ginmw.RequirePermission(enforcer, "delete", "posts"), handler)

Scope checking (OAuth 2.0 / OIDC scopes)

r.GET("/profile", ginmw.ScopeChecker("read:profile"), handler)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClaimsFrom

func ClaimsFrom(c *gin.Context) *jwtpkg.Claims

ClaimsFrom retrieves the *jwt.Claims stored by the JWT middleware. Returns nil if JWT middleware has not run or the token was invalid.

func JWT

func JWT(svc *jwtpkg.Service) gin.HandlerFunc

JWT returns a Gin middleware that:

  1. Extracts the Bearer token from the Authorization header.
  2. Validates it via svc.Verify (supports JWKS, HMAC, RSA, ECDSA).
  3. Stores the *jwt.Claims in the Gin context under "ginmw:claims".

Requests without a valid token are rejected with 401. To make auth optional, do not use this middleware globally — instead apply it only to protected groups.

func RequirePermission

func RequirePermission(enforcer *rbac.Enforcer, action, resource string) gin.HandlerFunc

RequirePermission returns a middleware that rejects requests (403) unless the authenticated subject has permission to perform action on resource.

Must be used after ginmw.JWT.

func RequireRole

func RequireRole(enforcer *rbac.Enforcer, roles ...string) gin.HandlerFunc

RequireRole returns a middleware that rejects requests (403) unless the authenticated subject has at least one of the provided roles.

Must be used after ginmw.JWT (or another middleware that sets the subject).

func ScopeChecker

func ScopeChecker(required ...string) gin.HandlerFunc

ScopeChecker returns a middleware that verifies the JWT contains all of the required OAuth 2.0 / OIDC scopes. It inspects two standard claim shapes:

  • "scope" — a single space-delimited string (e.g. "read:users write:posts")
  • "scopes" — a JSON array of strings (e.g. ["read:users","write:posts"])

Both conventions are widely used; Auth0 uses "scope" (string), some OIDC providers use "scopes" (array). ScopeChecker handles either.

Must be used after ginmw.JWT.

func SubjectFrom

func SubjectFrom(c *gin.Context) string

SubjectFrom retrieves the authenticated subject (sub claim) stored by the JWT middleware. Returns "" if JWT middleware has not run.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL