authx v0.3.0 (Refactor)
Overview
authx v0.3.0 is a major refactor focused on unified abstractions and multi-scenario extensibility.
Key updates:
- Authentication and authorization are split into
CheckandCan Engineis no longer coupled to a specific auth mechanismProviderManagercan register providers for multiple credential types- New
authx/httppackage withstd/gin/echo/fiberintegrations - New
RequireFastandTypedGuardfor hot-path and typed ergonomics
New Core Model
The new core is composed of:
AuthenticationProvider[C]: handles one credential typeCAuthenticationManager: selects and executes matched providerAuthorizer: performs authorization decisions independently
Engine orchestrates:
Check(ctx, credential)->AuthenticationResultCan(ctx, AuthorizationModel)->Decision
Typical Usage
engine := authx.NewEngine(
authx.WithAuthenticationManager(
authx.NewProviderManager(
authx.NewAuthenticationProviderFunc(func(
_ context.Context,
in UsernamePassword,
) (authx.AuthenticationResult, error) {
return authx.AuthenticationResult{
Principal: authx.Principal{ID: in.Username},
}, nil
}),
),
),
authx.WithAuthorizer(authx.AuthorizerFunc(func(
_ context.Context,
model authx.AuthorizationModel,
) (authx.Decision, error) {
return authx.Decision{Allowed: true}, nil
})),
)
result, err := engine.Check(ctx, UsernamePassword{Username: "alice", Password: "secret"})
decision, err := engine.Can(ctx, authx.AuthorizationModel{
Principal: result.Principal,
Action: "query",
Resource: "order",
})New HTTP Integrations
authx/http now provides a unified Guard layer:
authx/http/stdauthx/http/ginauthx/http/echoauthx/http/fiber
Unified extension points:
WithCredentialResolverFuncWithAuthorizationResolverFunc
Middleware usage:
guard := authhttp.NewGuard(
engine,
authhttp.WithCredentialResolverFunc(resolveCredential),
authhttp.WithAuthorizationResolverFunc(resolveAuthorization),
)
router.Use(authstd.Require(guard))
// or: router.Use(authstd.RequireFast(guard))Performance and Maintainability
- Added core benchmarks (including parallel scenarios)
- Added middleware benchmarks for
std/gin/echo/fiber - Reduced request-path temporary object creation
- Added
RequireFastpath to further reduce allocations - Split large files in
authxfor better maintainability
Examples
- Shared example:
authx/http/examples/shared - JWT example:
authx/http/examples/jwt - Framework examples:
authx/http/examples/std|gin|echo|fiber
Benchmark Commands
go test ./authx -run ^$ -bench BenchmarkEngine -benchmem
go test ./authx/http/std -run ^$ -bench BenchmarkRequire -benchmem
go test ./authx/http/gin -run ^$ -bench BenchmarkRequire -benchmem
go test ./authx/http/echo -run ^$ -bench BenchmarkRequire -benchmem
go test ./authx/http/fiber -run ^$ -bench BenchmarkRequire -benchmem