authx
authx
authx is a Go authentication and authorization abstraction for multi-scenario use (HTTP / gRPC / CLI).
Core principles:
- Separation of authentication and authorization:
Check/Can - Authentication-mechanism agnostic: JWT / password / OTP and more
- Framework-agnostic core with scenario-specific integration layers
Roadmap
- Module roadmap: authx roadmap
- Iteration execution plan: authx iteration plan
- New version note: authx v0.3.0 release
- Global roadmap: ArcGo roadmap
Core API
Engine: orchestrates authentication and authorizationProviderManager: manages providers for multiple credential typesAuthenticationProvider[C]: generic provider abstractionAuthorizer: authorization decision interfaceCheck(ctx, credential): authenticateCan(ctx, AuthorizationModel): authorizeHook: before/after hooks for Check/Can
Quick Start (Core)
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"})
if err != nil {
panic(err)
}
decision, err := engine.Can(ctx, authx.AuthorizationModel{
Principal: result.Principal,
Action: "query",
Resource: "order",
})
if err != nil {
panic(err)
}
_ = decisionHTTP Integrations
authx/http provides a unified Guard plus middleware integrations:
authx/http/stdauthx/http/ginauthx/http/echoauthx/http/fiber
Unified extension points:
WithCredentialResolverFuncWithAuthorizationResolverFunc
guard := authhttp.NewGuard(
engine,
authhttp.WithCredentialResolverFunc(resolveCredential),
authhttp.WithAuthorizationResolverFunc(resolveAuthorization),
)
router.Use(authstd.Require(guard))
// hot path: router.Use(authstd.RequireFast(guard))Examples
authx/http/examples/sharedauthx/http/examples/jwtauthx/http/examples/stdauthx/http/examples/ginauthx/http/examples/echoauthx/http/examples/fiber
Testing and Benchmarks
go test ./authx/...
# core
go test ./authx -run ^$ -bench BenchmarkEngine -benchmem
# middleware
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