tokidoki/auth/interface.go
Conrad Hoffmann 78bd2a9b84 Keep context keys private
Instead, offer type safe accessors, as documented here:

https://pkg.go.dev/context#Context
2022-03-16 14:33:47 +01:00

32 lines
629 B
Go

package auth
import (
"context"
"net/http"
)
type contextKey string
var authCtxKey contextKey = "auth"
type AuthContext struct {
AuthMethod string
UserName string
// TODO more?
}
func NewContext(ctx context.Context, a *AuthContext) context.Context {
return context.WithValue(ctx, authCtxKey, a)
}
func FromContext(ctx context.Context) (*AuthContext, bool) {
a, ok := ctx.Value(authCtxKey).(*AuthContext)
return a, ok
}
// Abstracts the authentication backend for the server.
type AuthProvider interface {
// Returns HTTP middleware for performing authentication.
Middleware() func(http.Handler) http.Handler
}