Documentation
¶
Index ¶
- Constants
- Variables
- func ExtractTokenFromAuthorizationHeader(c *echo.Context) string
- func KeycloakRoleMiddleware(verifier Verifier, audience string, roles ...string) echo.MiddlewareFunc
- func Middleware[Claims any](opts MiddlewareOptions[Claims]) echo.MiddlewareFunc
- func NewBuilder() *jwt.Builder
- func ParseJWT[T any](ctx context.Context, verifier Verifier, rawToken string) (T, jwt.Token, error)
- func WithIdentity(ctx context.Context, i Identity) context.Context
- type ClientRoles
- type ExtractToken
- type Identity
- type KeycloakClaims
- type MiddlewareOptions
- type Token
- type TokenVerifier
- type Verifier
Constants ¶
const ( RoleRead = "read" RoleWrite = "write" RoleAdmin = "admin" )
Role names used across the platform. The levels are ordered: read is any call that changes no state, write is a normal business state change (a refund that follows the service's own rules included), admin is anything outside the business rules such as forcing a state, replaying, or deleting.
const DevActorName = "Dev-Actor"
DevActorName is the header and cookie a developer sets to stand in as a staff member without a Keycloak login. Its value is the label that appears as the audit actor, so use your own email.
Variables ¶
var ErrForbidden = echo.NewHTTPError(http.StatusForbidden, "forbidden")
ErrForbidden is returned when a token is valid but not allowed to perform the request: wrong audience, or a missing role.
var ErrNoToken = ErrUnauthorized.Wrap(errors.New("no token"))
ErrNoToken unwraps to ErrUnauthorized
Functions ¶
func ExtractTokenFromAuthorizationHeader ¶ added in v2.8.5
func KeycloakRoleMiddleware ¶ added in v2.8.30
func KeycloakRoleMiddleware(verifier Verifier, audience string, roles ...string) echo.MiddlewareFunc
KeycloakRoleMiddleware verifies a Keycloak access token, requires that it was issued for audience and carries every role in roles on that audience, and puts the resulting Identity plus the audit actor into the request context.
audience is this service's own Keycloak client id, which is also its service name. Passing no roles requires a valid token for the audience and nothing more.
DEV-NOTE: see BauerMediaGroup-Stardust/platform-gitops docs/plans/keycloak-service-auth.md section 2. Authentication is the gateway's job, authorization is this middleware's; the audience check is what stops a token minted for another service from being replayed here.
func Middleware ¶ added in v2.5.5
func Middleware[Claims any](opts MiddlewareOptions[Claims]) echo.MiddlewareFunc
func NewBuilder ¶ added in v2.5.5
Types ¶
type ClientRoles ¶ added in v2.8.30
type ClientRoles struct {
Roles []string `json:"roles"`
}
ClientRoles is the role list Keycloak reports for one client.
type ExtractToken ¶ added in v2.8.5
func ExtractTokenFromCookie ¶ added in v2.8.5
func ExtractTokenFromCookie(name string) ExtractToken
type Identity ¶ added in v2.8.30
type Identity struct {
// Subject is the Keycloak sub claim: the user id for a human, the
// service-account user's UUID for a service. Never a service name - use
// ClientID or Actor() for that.
Subject string
// Email is set for humans only.
Email string
// ClientID is the calling service's Keycloak client id, set only for a
// client_credentials (service account) token. Empty for a human.
ClientID string
// Audience is the client id this token was accepted for, which is also the
// key its roles were read from.
Audience string
// Roles are the caller's roles on Audience, without the hierarchy applied.
Roles []string
}
Identity is the verified caller of a request.
func IdentityFrom ¶ added in v2.8.30
IdentityFrom returns the identity carried by ctx, if any.
func (Identity) Actor ¶ added in v2.8.30
Actor returns the audit actor for this identity: a service account becomes the calling service under its client id, anything else a staff user.
type KeycloakClaims ¶ added in v2.8.30
type KeycloakClaims struct {
Email string `json:"email"`
ResourceAccess map[string]ClientRoles `json:"resource_access"`
// ClientID is the client_credentials marker: set for a service account,
// absent for a human. Added by the service_account client scope.
ClientID string `json:"client_id"`
// PreferredUsername is service-account-<client-id> for a service account.
// Fallback for a realm whose service_account scope lacks the client_id
// mapper.
PreferredUsername string `json:"preferred_username"`
}
KeycloakClaims are the claims of a Keycloak access token.
DEV-NOTE: resource_access is a generic map, not a per-service struct. Every hand-written copy of this middleware hardcoded its own client key, which made the code un-shareable and, in one case, mislabelled (a field named PlayerService tagged limit-service).
type MiddlewareOptions ¶ added in v2.5.5
type MiddlewareOptions[Claims any] struct { TokenVerifier Verifier // ExtractToken extracts the raw token from the request. // If not defined, it will fall back to ExtractTokenFromAuthorizationHeader ExtractToken ExtractToken UpdateContext func(c *echo.Context, token jwt.Token, claims Claims) error }
type TokenVerifier ¶ added in v2.5.5
type TokenVerifier struct {
Close context.CancelFunc
// contains filtered or unexported fields
}
func NewTokenVerifier ¶ added in v2.5.5
func NewTokenVerifier(ctx context.Context, url string) (*TokenVerifier, error)