-
Notifications
You must be signed in to change notification settings - Fork 117
/
issuer.go
39 lines (31 loc) · 972 Bytes
/
issuer.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
package authnmock
import (
"context"
"errors"
"time"
"golang.org/x/oauth2"
authnv1 "github.com/lyft/clutch/backend/api/authn/v1"
)
type MockIssuer struct {
AllowRefresh bool
}
func (MockIssuer) CreateToken(ctx context.Context, subject string, tokenType authnv1.CreateTokenRequest_TokenType, expiry *time.Duration) (token *oauth2.Token, err error) {
if expiry == nil {
return &oauth2.Token{
AccessToken: authnv1.CreateTokenRequest_TokenType_name[int32(tokenType)] + "_" + subject + "_token-without-expiry",
}, nil
}
return &oauth2.Token{
AccessToken: authnv1.CreateTokenRequest_TokenType_name[int32(tokenType)] + "_" + subject + "_token-with-expiry",
Expiry: time.Now().Add(*expiry),
}, nil
}
func (m MockIssuer) RefreshToken(context.Context, *oauth2.Token) (*oauth2.Token, error) {
if !m.AllowRefresh {
return nil, errors.New("not allowed")
}
return &oauth2.Token{
AccessToken: "newAccess",
RefreshToken: "refreshed",
}, nil
}