-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
174 lines (139 loc) · 3.99 KB
/
service.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package oidc
import (
"context"
"fmt"
"strings"
"github.com/google/uuid"
"github.com/konstantinfoerster/card-service-go/internal/common"
"github.com/konstantinfoerster/card-service-go/internal/common/auth"
"github.com/konstantinfoerster/card-service-go/internal/common/config"
commonhttp "github.com/konstantinfoerster/card-service-go/internal/common/http"
)
func NewRedirectURL(p Provider, redirectURI string) (*RedirectURL, error) {
stateBase64, err := NewState(p.GetName()).Encode()
if err != nil {
return nil, err
}
return &RedirectURL{
URL: p.GetAuthURL(stateBase64, redirectURI),
State: stateBase64,
}, nil
}
type RedirectURL struct {
URL string
State string
}
func NewState(provider string) *State {
return &State{ID: uuid.New().String(), Provider: provider}
}
// DecodeState decode given base64 url encoded State.
func DecodeState(value string) (*State, error) {
return commonhttp.DecodeBase64[State](value)
}
type State struct {
ID string `json:"id"`
Provider string `json:"provider"`
}
func (s *State) Encode() (string, error) {
return commonhttp.EncodeBase64(s)
}
// DecodeToken decode given base64 url encoded JSONWebToken.
func DecodeToken(value string) (*JSONWebToken, error) {
return commonhttp.DecodeBase64[JSONWebToken](value)
}
type JSONWebToken struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
IDToken string `json:"id_token"`
Scope string `json:"scope"`
ExpiresIn int `json:"expires_in"`
Type string `json:"token_type"`
Provider string `json:"provider"`
}
func (t *JSONWebToken) Encode() (string, error) {
return commonhttp.EncodeBase64(t)
}
type claims struct {
ID string
Email string
}
func claimsToUser(claims *claims) *auth.User {
return &auth.User{
ID: claims.ID,
Username: claims.Email,
}
}
type UserService interface {
GetAuthenticatedUser(provider string, token *JSONWebToken) (*auth.User, error)
}
type Service interface {
UserService
GetAuthURL(provider string) (*RedirectURL, error)
Authenticate(provider string, code string) (*auth.User, *JSONWebToken, error)
Logout(token *JSONWebToken) error
}
func New(cfg config.Oidc, provider []Provider) Service {
pp := make(map[string]Provider)
for _, p := range provider {
pp[p.GetName()] = p
}
return &authFlowService{
provider: pp,
cfg: cfg,
}
}
type authFlowService struct {
provider map[string]Provider
cfg config.Oidc
}
func (s *authFlowService) GetAuthURL(provider string) (*RedirectURL, error) {
p, err := s.getProvider(provider)
if err != nil {
return nil, err
}
return NewRedirectURL(p, s.cfg.RedirectURI)
}
func (s *authFlowService) Authenticate(provider string, authCode string) (*auth.User, *JSONWebToken, error) {
p, err := s.getProvider(provider)
if err != nil {
return nil, nil, err
}
ctx := context.Background()
claims, jwtToken, err := p.ExchangeCode(ctx, authCode, s.cfg.RedirectURI)
if err != nil {
return nil, nil, err
}
return claimsToUser(claims), jwtToken, nil
}
func (s *authFlowService) GetAuthenticatedUser(provider string, token *JSONWebToken) (*auth.User, error) {
p, err := s.getProvider(provider)
if err != nil {
return nil, err
}
claims, err := p.ValidateToken(context.Background(), token)
if err != nil {
return nil, err
}
return claimsToUser(claims), nil
}
func (s *authFlowService) Logout(token *JSONWebToken) error {
p, err := s.getProvider(token.Provider)
if err != nil {
return err
}
// FIXME with timeout?
ctx := context.Background()
return p.RevokeToken(ctx, token.AccessToken)
}
func (s *authFlowService) getProvider(key string) (Provider, error) {
if strings.TrimSpace(key) == "" {
err := fmt.Errorf("provider mut not be empty")
return nil, common.NewInvalidInputError(err, "login-provider-empty", err.Error())
}
p, ok := s.provider[strings.ToLower(key)]
if ok {
return p, nil
}
err := fmt.Errorf("provider '%s' not supported", key)
return nil, common.NewInvalidInputError(err, "login-provider-not-supported", err.Error())
}