-
Notifications
You must be signed in to change notification settings - Fork 116
/
authn.go
90 lines (72 loc) · 2.74 KB
/
authn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package authn
// <!-- START clutchdoc -->
// description: Produces tokens for the configured OIDC provider.
// <!-- END clutchdoc -->
import (
"context"
"fmt"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/uber-go/tally/v4"
"go.uber.org/zap"
"golang.org/x/oauth2"
"google.golang.org/protobuf/types/known/anypb"
authnmodulev1 "github.com/lyft/clutch/backend/api/authn/v1"
authnv1 "github.com/lyft/clutch/backend/api/config/service/authn/v1"
"github.com/lyft/clutch/backend/service"
)
const Name = "clutch.service.authn"
// AlwaysAllowedMethods is a list of method patterns that are always open and not blocked by authn or authz.
// TODO(maybe): convert this to an API annotation or make configurable on the middleware that use the list.
var AlwaysAllowedMethods = []string{
"/clutch.authn.v1.AuthnAPI/Callback",
"/clutch.authn.v1.AuthnAPI/Login",
"/clutch.healthcheck.v1.HealthcheckAPI/*",
}
func New(cfg *anypb.Any, logger *zap.Logger, scope tally.Scope) (service.Service, error) {
config := &authnv1.Config{}
if err := cfg.UnmarshalTo(config); err != nil {
return nil, err
}
tokenStorage, _ := service.Registry[StorageName].(Storage) // Ignoring 'ok', nil allowed.
switch t := config.Type.(type) {
case *authnv1.Config_Oidc:
return NewOIDCProvider(context.Background(), config, tokenStorage)
default:
return nil, fmt.Errorf("authn provider type '%T' not implemented", t)
}
}
// Standardized representation of a user's claims.
type Claims struct {
*jwt.StandardClaims
// Groups could be derived from the token or an external mapping.
Groups []string `json:"grp,omitempty"`
}
type Provider interface {
GetStateNonce(redirectURL string) (string, error)
ValidateStateNonce(state string) (redirectURL string, err error)
Verify(ctx context.Context, rawIDToken string) (*Claims, error)
GetAuthCodeURL(ctx context.Context, state string) (string, error)
Exchange(ctx context.Context, code string) (token *oauth2.Token, err error)
}
type Issuer interface {
// CreateToken creates a new OAuth2 for the provided subject with the provided expiration. If expiry is nil,
// the token will never expire.
CreateToken(ctx context.Context, subject string, tokenType authnmodulev1.CreateTokenRequest_TokenType, expiry *time.Duration) (token *oauth2.Token, err error)
RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error)
}
type Service interface {
Issuer
Provider
TokenReader // Read calls are proxied through the IssuerProvider so the token can be refreshed if needed.
}
type TokenReader interface {
Read(ctx context.Context, userID, provider string) (*oauth2.Token, error)
}
type TokenStorer interface {
Store(ctx context.Context, userID, provider string, token *oauth2.Token) error
}
type Storage interface {
TokenReader
TokenStorer
}