auth

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package auth authenticates HTTP requests and carries the caller's identity on the request context.

JWTManager issues and validates this package's own tokens, but the middleware is written against the IdentityProvider interface, so a service that authenticates against an IAM service or an opaque session store reuses the same middleware and helpers with its own resolver.

Index

Examples

Constants

View Source
const AdminRole = "admin"

AdminRole is the role that satisfies every RequireRole check.

Variables

This section is empty.

Functions

func Authenticate

func Authenticate(provider IdentityProvider) func(http.Handler) http.Handler

Authenticate returns a middleware that resolves the request's bearer token through provider and publishes the resulting Identity on the request context. Requests without a usable token, or whose token the provider rejects, are answered 401 and never reach the handler.

Example

The middleware authenticates a bearer token and publishes the caller on the request context, where handlers read it through a typed accessor.

package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/dobrevit/svckit/auth"
)

func main() {
	manager := auth.NewJWTManager("a-signing-secret", time.Hour)
	token, _ := manager.GenerateToken("u-1", []string{"editor"})

	handler := auth.Authenticate(manager)(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			id, _ := auth.UserID(r.Context())
			fmt.Println("authenticated:", id)
		}))

	r := httptest.NewRequest(http.MethodGet, "/orders", nil)
	r.Header.Set("Authorization", "Bearer "+token)
	handler.ServeHTTP(httptest.NewRecorder(), r)

}
Output:
authenticated: u-1

func BearerToken

func BearerToken(r *http.Request) (string, bool)

BearerToken returns the token from r's Authorization header, and whether the header carried a bearer token at all.

func GetUserFromContext deprecated

func GetUserFromContext(ctx context.Context) (string, []string, bool)

GetUserFromContext reads the user stored by SetUserContext.

Deprecated: use IdentityFromContext.

func RequireRole

func RequireRole(role string) func(http.Handler) http.Handler

RequireRole returns a middleware that rejects an authenticated caller lacking role. AdminRole satisfies every check. It must run after Authenticate.

func Roles

func Roles(ctx context.Context) ([]string, bool)

Roles returns the authenticated user's roles carried by ctx.

func SetUserContext deprecated

func SetUserContext(ctx context.Context, userID string, roles []string) context.Context

SetUserContext stores the user under both the current and the legacy keys.

Deprecated: use WithIdentity.

func UserID

func UserID(ctx context.Context) (string, bool)

UserID returns the authenticated user's ID carried by ctx.

func WithIdentity

func WithIdentity(ctx context.Context, identity Identity) context.Context

WithIdentity returns a context carrying identity.

Types

type Claims

type Claims struct {
	UserID string   `json:"user_id"`
	Roles  []string `json:"roles"`
	jwt.RegisteredClaims
}

Claims represents the JWT claims

func (*Claims) HasRole

func (c *Claims) HasRole(role string) bool

HasRole checks if the user has the specified role

type ContextKey deprecated

type ContextKey string

ContextKey is the legacy string key type used by SetUserContext.

Deprecated: use WithIdentity and IdentityFromContext, which use an unexported key type that cannot collide with keys set by other packages.

const (
	UserIDContextKey ContextKey = "user_id"
	RolesContextKey  ContextKey = "roles"
)

Legacy context keys.

Deprecated: see ContextKey.

type Identity

type Identity struct {
	UserID string
	Roles  []string
}

Identity is the authenticated caller a middleware publishes on the request context.

func IdentityFromContext

func IdentityFromContext(ctx context.Context) (Identity, bool)

IdentityFromContext returns the identity carried by ctx, if any.

func (Identity) HasRole

func (i Identity) HasRole(role string) bool

HasRole reports whether the identity holds role.

type IdentityProvider

type IdentityProvider interface {
	Identify(ctx context.Context, token string) (Identity, error)
}

IdentityProvider turns a bearer token into an Identity, or reports why it could not. Implementing it lets a service authenticate against something other than this package's JWTs — an IAM service, an opaque session store — while reusing the middleware and helpers here.

type IdentityProviderFunc

type IdentityProviderFunc func(ctx context.Context, token string) (Identity, error)

IdentityProviderFunc adapts a function to IdentityProvider.

Example

A service that authenticates against something other than this package's JWTs supplies its own IdentityProvider and keeps the same middleware.

package main

import (
	"context"
	"errors"
	"fmt"
	"net/http"
	"net/http/httptest"

	"github.com/dobrevit/svckit/auth"
)

func main() {
	sessions := map[string]string{"session-abc": "u-9"}

	provider := auth.IdentityProviderFunc(
		func(_ context.Context, token string) (auth.Identity, error) {
			userID, ok := sessions[token]
			if !ok {
				return auth.Identity{}, errors.New("unknown session")
			}
			return auth.Identity{UserID: userID, Roles: []string{"viewer"}}, nil
		})

	handler := auth.Authenticate(provider)(http.HandlerFunc(
		func(w http.ResponseWriter, r *http.Request) {
			id, _ := auth.UserID(r.Context())
			fmt.Println("authenticated:", id)
		}))

	r := httptest.NewRequest(http.MethodGet, "/orders", nil)
	r.Header.Set("Authorization", "Bearer session-abc")
	handler.ServeHTTP(httptest.NewRecorder(), r)

}
Output:
authenticated: u-9

func (IdentityProviderFunc) Identify

func (f IdentityProviderFunc) Identify(ctx context.Context, token string) (Identity, error)

Identify calls f.

type JWTManager

type JWTManager struct {
	// contains filtered or unexported fields
}

JWTManager handles JWT token operations

func NewJWTManager

func NewJWTManager(secretKey string, expiry time.Duration) *JWTManager

NewJWTManager creates a new JWT manager

func (*JWTManager) GenerateToken

func (j *JWTManager) GenerateToken(userID string, roles []string) (string, error)

GenerateToken generates a JWT token for the given user

func (*JWTManager) Identify

func (j *JWTManager) Identify(_ context.Context, token string) (Identity, error)

Identify implements IdentityProvider over the manager's own JWTs.

func (*JWTManager) ValidateToken

func (j *JWTManager) ValidateToken(tokenString string) (*Claims, error)

ValidateToken validates and parses a JWT token

Jump to

Keyboard shortcuts

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