Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/authorizerdev/authorizer/internal/audit"
"github.com/authorizerdev/authorizer/internal/authenticators"
"github.com/authorizerdev/authorizer/internal/authorization"
"github.com/authorizerdev/authorizer/internal/config"
"github.com/authorizerdev/authorizer/internal/constants"
"github.com/authorizerdev/authorizer/internal/email"
Expand Down Expand Up @@ -235,6 +236,12 @@ func init() {
// Back-channel logout (OIDC BCL 1.0)
f.StringVar(&rootArgs.config.BackchannelLogoutURI, "backchannel-logout-uri", "", "URL to POST a signed logout_token to when users log out successfully. Leave empty (default) to disable back-channel logout notifications. See OIDC Back-Channel Logout 1.0.")

// Fine-grained authorization flags
f.StringVar(&rootArgs.config.AuthorizationEnforcement, "authorization-enforcement", "disabled", "Authorization enforcement mode: disabled, permissive, or enforcing")
f.Int64Var(&rootArgs.config.AuthorizationCacheTTL, "authorization-cache-ttl", 300, "Cache TTL in seconds for permission checks (0 to disable)")
f.BoolVar(&rootArgs.config.IncludePermissionsInToken, "include-permissions-in-token", false, "Include permissions in JWT access tokens")
f.BoolVar(&rootArgs.config.AuthorizationLogAllChecks, "authorization-log-all-checks", false, "Audit log all permission checks, not just denials")

// Deprecated flags
f.MarkDeprecated("database_url", "use --database-url instead")
f.MarkDeprecated("database_type", "use --database-type instead")
Expand Down Expand Up @@ -321,6 +328,9 @@ func applyFlagDefaults() {
if len(c.RobloxScopes) == 0 {
c.RobloxScopes = append([]string(nil), defaultRobloxScopes...)
}
if strings.TrimSpace(c.AuthorizationEnforcement) == "" {
c.AuthorizationEnforcement = "disabled"
}
}

// Run the service
Expand Down Expand Up @@ -455,6 +465,24 @@ func runRoot(c *cobra.Command, args []string) {
}
defer rateLimitProvider.Close()

// Authorization provider
authorizationProvider, err := authorization.New(
&authorization.Config{
Enforcement: rootArgs.config.AuthorizationEnforcement,
CacheTTL: rootArgs.config.AuthorizationCacheTTL,
},
&authorization.Dependencies{
Log: &log,
StorageProvider: storageProvider,
},
)
if err != nil {
log.Fatal().Err(err).Msg("failed to create authorization provider")
}
if rootArgs.config.AuthorizationEnforcement != "disabled" {
log.Info().Str("enforcement", rootArgs.config.AuthorizationEnforcement).Msg("authorization enforcement enabled")
}

// SMS provider
smsProvider, err := sms.New(&rootArgs.config, &sms.Dependencies{
Log: &log,
Expand Down Expand Up @@ -505,6 +533,7 @@ func runRoot(c *cobra.Command, args []string) {
TokenProvider: tokenProvider,
OAuthProvider: oauthProvider,
RateLimitProvider: rateLimitProvider,
AuthorizationProvider: authorizationProvider,
})
if err != nil {
log.Fatal().Err(err).Msg("failed to create http provider")
Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ require (
golang.org/x/time v0.15.0
gopkg.in/mail.v2 v2.3.1
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.4
gorm.io/driver/postgres v1.6.0
gorm.io/driver/sqlserver v1.5.2
gorm.io/gorm v1.25.5
gorm.io/gorm v1.25.10
)

require (
Expand Down Expand Up @@ -88,8 +88,8 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.5.4 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jackc/pgx/v5 v5.9.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.4 h1:Xp2aQS8uXButQdnCMWNmvx6UysWQQC+u1EoizjguY+8=
github.com/jackc/pgx/v5 v5.5.4/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jackc/pgx/v5 v5.9.1 h1:uwrxJXBnx76nyISkhr33kQLlUqjv7et7b9FjCen/tdc=
github.com/jackc/pgx/v5 v5.9.1/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs=
github.com/jcmturner/dnsutils/v2 v2.0.0/go.mod h1:b0TnjGOvI/n42bZa+hmXL+kFJZsFT7G4t3HTlQ184QM=
github.com/jcmturner/gofork v1.7.6/go.mod h1:1622LH6i/EZqLloHfE7IeZ0uEJwMSUyQ/nDd82IeqRo=
Expand Down Expand Up @@ -481,14 +481,14 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.2 h1:QC2HRskSE75wBuOxe0+iCkyJZ+RqpudsQtqkp+IMuXs=
gorm.io/driver/mysql v1.5.2/go.mod h1:pQLhh1Ut/WUAySdTHwBpBv6+JKcj+ua4ZFx1QQTBzb8=
gorm.io/driver/postgres v1.5.4 h1:Iyrp9Meh3GmbSuyIAGyjkN+n9K+GHX9b9MqsTL4EJCo=
gorm.io/driver/postgres v1.5.4/go.mod h1:Bgo89+h0CRcdA33Y6frlaHHVuTdOf87pmyzwW9C/BH0=
gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo=
gorm.io/driver/sqlserver v1.5.2 h1:+o4RQ8w1ohPbADhFqDxeeZnSWjwOcBnxBckjTbcP4wk=
gorm.io/driver/sqlserver v1.5.2/go.mod h1:gaKF0MO0cfTq9Q3/XhkowSw4g6nIwHPGAs4hzKCmvBo=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.2-0.20230610234218-206613868439/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
modernc.org/libc v1.22.5/go.mod h1:jj+Z7dTNX8fBScMVNRAYZ/jF91K8fdT2hYMThc3YjBY=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
Expand Down
95 changes: 95 additions & 0 deletions internal/authorization/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package authorization

import (
"fmt"
"strings"
"sync"
"time"
)

// cache is a local in-memory cache with TTL support.
// It uses sync.Map for concurrent access and tracks per-key expiry.
// A distributed cache (via memory_store) will be layered on top in Phase 7.
type cache struct {
ttl time.Duration
data sync.Map
expiryMap sync.Map
}

// newCache creates a new local cache. If ttlSeconds is 0, caching is disabled.
func newCache(ttlSeconds int64) *cache {
return &cache{
ttl: time.Duration(ttlSeconds) * time.Second,
}
}

// enabled returns true if caching is active (TTL > 0).
func (c *cache) enabled() bool {
return c.ttl > 0
}

// get retrieves a cached value by key. Returns the value and whether the key
// was found and still valid. Expired entries are lazily deleted on access.
// This returns cached "false" results identically to "true" results,
// ensuring constant-time behavior for both outcomes.
func (c *cache) get(key string) (string, bool) {
if !c.enabled() {
return "", false
}

expiry, ok := c.expiryMap.Load(key)
if !ok {
return "", false
}
if time.Now().After(expiry.(time.Time)) {
// Lazily evict expired entry.
c.data.Delete(key)
c.expiryMap.Delete(key)
return "", false
}

val, ok := c.data.Load(key)
if !ok {
return "", false
}
return val.(string), true
}

// set stores a value in the cache with the configured TTL.
// Both "true" and "false" values are cached (negative caching)
// to prevent cache stampede on non-existent resource:scope combos.
func (c *cache) set(key string, value string) {
if !c.enabled() {
return
}
c.data.Store(key, value)
c.expiryMap.Store(key, time.Now().Add(c.ttl))
}

// deleteByPrefix removes all cached entries whose key starts with the given prefix.
// Used when admin mutations change resources, scopes, or policies to invalidate
// all related cached decisions.
func (c *cache) deleteByPrefix(prefix string) {
c.data.Range(func(key, _ any) bool {
if strings.HasPrefix(key.(string), prefix) {
c.data.Delete(key)
c.expiryMap.Delete(key)
}
return true
})
}

// evalKey constructs a cache key for an authorization evaluation result.
func evalKey(principalID, resource, scope string) string {
return fmt.Sprintf("authz:eval:%s:%s:%s", principalID, resource, scope)
}

// validResourcesKey returns the cache key for the set of known resource names.
func validResourcesKey() string {
return "authz:valid_resources"
}

// validScopesKey returns the cache key for the set of known scope names.
func validScopesKey() string {
return "authz:valid_scopes"
}
Loading