2023-02-24 14:40:28 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2023-03-10 14:33:12 +01:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2023-02-24 14:40:28 +01:00
|
|
|
)
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
type GetClaimsFunc func(ctx context.Context, r *http.Request, claims ...string) ([]string, error)
|
2023-02-24 14:40:28 +01:00
|
|
|
|
|
|
|
type Option struct {
|
2023-04-18 17:57:16 +02:00
|
|
|
GetClaims GetClaimsFunc
|
|
|
|
ProfileClaims []string
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type OptionFunc func(*Option)
|
|
|
|
|
2023-03-10 14:33:12 +01:00
|
|
|
func defaultOptions() *Option {
|
|
|
|
return &Option{
|
2023-04-18 17:57:16 +02:00
|
|
|
GetClaims: dummyGetClaims,
|
|
|
|
ProfileClaims: []string{
|
|
|
|
ClaimSubject,
|
|
|
|
ClaimIssuer,
|
|
|
|
ClaimEdgeEntrypoint,
|
|
|
|
ClaimEdgeRole,
|
|
|
|
ClaimPreferredUsername,
|
|
|
|
ClaimEdgeTenant,
|
|
|
|
},
|
2023-03-10 14:33:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
func dummyGetClaims(ctx context.Context, r *http.Request, claims ...string) ([]string, error) {
|
|
|
|
return nil, errors.Errorf("dummy getclaim func cannot retrieve claims '%s'", claims)
|
2023-03-10 14:33:12 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
func WithGetClaims(fn GetClaimsFunc) OptionFunc {
|
2023-02-24 14:40:28 +01:00
|
|
|
return func(o *Option) {
|
2023-04-18 17:57:16 +02:00
|
|
|
o.GetClaims = fn
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithProfileClaims(claims ...string) OptionFunc {
|
|
|
|
return func(o *Option) {
|
|
|
|
o.ProfileClaims = claims
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
|
|
|
}
|