2022-02-21 09:55:02 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2022-03-16 13:33:47 +00:00
|
|
|
"context"
|
2022-02-21 09:55:02 +00:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2022-03-16 13:33:47 +00:00
|
|
|
type contextKey string
|
2022-02-23 12:42:28 +00:00
|
|
|
|
2022-03-16 13:33:47 +00:00
|
|
|
var authCtxKey contextKey = "auth"
|
2022-02-23 12:42:28 +00:00
|
|
|
|
|
|
|
type AuthContext struct {
|
|
|
|
AuthMethod string
|
|
|
|
UserName string
|
|
|
|
// TODO more?
|
|
|
|
}
|
|
|
|
|
2022-03-16 13:33:47 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-21 09:55:02 +00:00
|
|
|
// Abstracts the authentication backend for the server.
|
|
|
|
type AuthProvider interface {
|
|
|
|
// Returns HTTP middleware for performing authentication.
|
|
|
|
Middleware() func(http.Handler) http.Handler
|
|
|
|
}
|