-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
252 lines (209 loc) · 7.17 KB
/
auth.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package api
import (
"context"
"errors"
"fmt"
"github.com/gorilla/mux"
jwt2 "github.com/imba28/spolyr/pkg/jwt"
"github.com/imba28/spolyr/pkg/openapi/openapi"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
"golang.org/x/oauth2"
"net/http"
"strings"
"time"
)
type authContextKey int
const (
spotifyRefreshTokenKey authContextKey = iota
jwtRefreshKey
jwtAccessKey
spotifyOauthClientKey
accessTokenExpiry = time.Minute * 10
refreshTokenExpiry = time.Hour * 24
)
var (
permissions = []string{
spotifyauth.ScopeUserLibraryRead,
spotifyauth.ScopeUserReadEmail,
spotifyauth.ScopePlaylistReadCollaborative,
spotifyauth.ScopePlaylistReadPrivate}
scope = strings.Join(permissions, " ")
auth = spotifyauth.New(spotifyauth.WithScopes(scope))
ErrNotAuthenticated = errors.New("no authentication provided")
)
func refreshTokenFromContext(ctx context.Context) *string {
if t, ok := ctx.Value(jwtRefreshKey).(string); ok {
return &t
}
return nil
}
func accessTokenFromContext(ctx context.Context) *string {
if t, ok := ctx.Value(jwtAccessKey).(string); ok {
return &t
}
return nil
}
func oauthClientFromContext(ctx context.Context) *spotify.Client {
if c, ok := ctx.Value(spotifyOauthClientKey).(*spotify.Client); ok {
return c
}
return nil
}
func oauthRefreshTokenFromContext(ctx context.Context) string {
if t, ok := ctx.Value(spotifyRefreshTokenKey).(string); ok {
return t
}
return ""
}
func (a AuthApiService) redirectUrl() string {
port := a.publicHttpPort
publicPort := ""
if port != 80 && port != 443 && port != 0 {
publicPort = fmt.Sprintf(":%d", port)
}
return fmt.Sprintf("%s://%s%s/auth/callback", a.publicHttpProtocol, a.publicHostname, publicPort)
}
func isAuthenticated(ctx context.Context) bool {
t := accessTokenFromContext(ctx)
return t != nil
}
func hasValidRefreshToken(ctx context.Context) bool {
t := refreshTokenFromContext(ctx)
// todo: check database if token has been revoked
return t != nil
}
func AuthenticationMiddleware(j jwt2.JWT) mux.MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.Method != http.MethodOptions {
if c, err := r.Cookie("jwt"); err == nil {
claims, valid := j.ValidateAccessToken(c.Value)
if valid {
ctx = context.WithValue(ctx, jwtAccessKey, c.Value)
ctx = context.WithValue(ctx, spotifyOauthClientKey, spotify.New(auth.Client(ctx, &claims.Token)))
}
}
if c, err := r.Cookie("jwt-refresh"); err == nil {
if claims, valid := j.ValidateRefreshToken(c.Value); valid {
ctx = context.WithValue(ctx, spotifyRefreshTokenKey, claims.RefreshToken)
ctx = context.WithValue(ctx, jwtRefreshKey, c.Value)
}
}
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
type AuthApiService struct {
clientId string
jwt jwt2.JWT
publicHttpProtocol string
publicHostname string
publicHttpPort int
}
func (a AuthApiService) cookie(name, value, path string) http.Cookie {
return http.Cookie{
Name: name,
Path: path,
Value: value,
Secure: a.publicHttpProtocol == "https",
HttpOnly: true,
SameSite: http.SameSiteNoneMode,
}
}
func (a AuthApiService) jwtTokenHeaders(t oauth2.Token, generateRefreshToken bool) (map[string][]string, error) {
headers := make(map[string][]string)
var cookies []string
accessToken, err := a.jwt.NewAccessToken(t, time.Now().Add(accessTokenExpiry))
if err != nil {
return nil, errors.New("could not sign access jwt")
}
accessTokenCookie := a.cookie("jwt", accessToken, "/api")
cookies = append(cookies, accessTokenCookie.String())
if generateRefreshToken {
// todo: save valid refresh to database
refreshToken, err := a.jwt.NewRefreshToken(t.RefreshToken, time.Now().Add(refreshTokenExpiry))
if err != nil {
return nil, errors.New("could not sign refresh jwt")
}
refreshTokenCookie := a.cookie("jwt-refresh", refreshToken, "/api/auth")
refreshTokenCookie.Expires = time.Now().Add(refreshTokenExpiry)
cookies = append(cookies, refreshTokenCookie.String())
}
headers["Set-Cookie"] = cookies
return headers, nil
}
func (a AuthApiService) AuthLogoutGet(ctx context.Context) (openapi.ImplResponse, error) {
accessTokenCookie := a.cookie("jwt", "", "/api")
accessTokenCookie.Expires = time.Unix(0, 0)
// todo: revoke refresh token => delete from database
refreshTokenCookie := a.cookie("jwt-refresh", "", "/api/auth/")
refreshTokenCookie.Expires = time.Unix(0, 0)
headers := make(map[string][]string)
headers["Set-Cookie"] = []string{accessTokenCookie.String(), refreshTokenCookie.String()}
return openapi.ResponseWithHeaders(http.StatusOK, headers, nil), nil
}
func (a AuthApiService) AuthLoginPost(ctx context.Context, request openapi.AuthLoginPostRequest) (openapi.ImplResponse, error) {
t, err := auth.Exchange(ctx, request.Code)
if err != nil {
return openapi.Response(http.StatusBadRequest, nil), errors.New("could not exchange code for token")
}
c := spotify.New(auth.Client(ctx, t))
user, err := c.CurrentUser(ctx)
if err != nil {
return openapi.Response(http.StatusInternalServerError, nil), errors.New("could not get user info")
}
headers, err := a.jwtTokenHeaders(*t, true)
if err != nil {
return openapi.Response(http.StatusInternalServerError, nil), err
}
body := openapi.OAuthUserInfo{
DisplayName: user.DisplayName,
}
if len(user.Images) > 0 {
body.AvatarUrl = user.Images[0].URL
}
return openapi.ResponseWithHeaders(http.StatusOK, headers, body), nil
}
func (a AuthApiService) AuthConfigurationGet(ctx context.Context) (openapi.ImplResponse, error) {
res := openapi.OAuthConfiguration{
RedirectUrl: a.redirectUrl(),
ClientId: a.clientId,
Scope: scope,
}
return openapi.Response(http.StatusOK, res), nil
}
func (a AuthApiService) AuthRefreshGet(ctx context.Context) (openapi.ImplResponse, error) {
if !hasValidRefreshToken(ctx) {
return openapi.Response(http.StatusUnauthorized, nil), ErrNotAuthenticated
}
spotifyToken := &oauth2.Token{
TokenType: "Bearer",
RefreshToken: oauthRefreshTokenFromContext(ctx),
}
c := spotify.New(auth.Client(ctx, spotifyToken))
newToken, err := c.Token()
if err != nil {
return openapi.Response(http.StatusInternalServerError, nil), errors.New("refreshing oauth2 access token failed")
}
headers, err := a.jwtTokenHeaders(*newToken, false)
return openapi.ResponseWithHeaders(http.StatusOK, headers, nil), nil
}
func newAuthApiService(clientId, clientSecret string, secret []byte, publicProtocol, publicHostname string, publicPort int) AuthApiService {
a := AuthApiService{
clientId: clientId,
jwt: jwt2.New(secret),
publicHttpPort: publicPort,
publicHostname: publicHostname,
publicHttpProtocol: publicProtocol,
}
// todo: this is extremely ugly, but public hostname/port are only available after setting up the service
// alternatively, we could directly access the environment variables (but that breaks the top-down approach of the configuration flow)
spotifyauth.WithRedirectURL(a.redirectUrl())(auth)
spotifyauth.WithClientID(clientId)(auth)
spotifyauth.WithClientSecret(clientSecret)(auth)
return a
}
var _ openapi.AuthApiServicer = AuthApiService{}