/
auth.go
295 lines (244 loc) · 7.42 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package handlers
import (
"context"
"errors"
"net/http"
"slices"
"time"
"github.com/alexferl/echo-openapi"
"github.com/alexferl/golib/http/api/server"
"github.com/labstack/echo/v4"
jwx "github.com/lestrrat-go/jwx/v2/jwt"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/alexferl/echo-boilerplate/config"
"github.com/alexferl/echo-boilerplate/models"
"github.com/alexferl/echo-boilerplate/services"
"github.com/alexferl/echo-boilerplate/util/cookie"
)
type AuthHandler struct {
*openapi.Handler
svc UserService
}
func NewAuthHandler(openapi *openapi.Handler, svc UserService) *AuthHandler {
return &AuthHandler{
Handler: openapi,
svc: svc,
}
}
func (h *AuthHandler) Register(s *server.Server) {
s.Add(http.MethodPost, "/auth/login", h.login)
s.Add(http.MethodPost, "/auth/logout", h.logout)
s.Add(http.MethodPost, "/auth/refresh", h.refresh)
s.Add(http.MethodPost, "/auth/signup", h.signup)
s.Add(http.MethodGet, "/auth/token", h.token)
if slices.Contains(viper.GetStringSlice(config.OAuth2Providers), "google") {
s.Add(http.MethodGet, "/oauth2/google/callback", h.oauth2GoogleCallback)
s.Add(http.MethodGet, "/oauth2/google/login", h.oauth2GoogleLogin)
}
}
type LoginRequest struct {
Email string `json:"email,omitempty"`
Password string `json:"password"`
Username string `json:"username,omitempty"`
}
type LoginResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
}
func (h *AuthHandler) login(c echo.Context) error {
body := &LoginRequest{}
if err := c.Bind(body); err != nil {
log.Error().Err(err).Msg("failed binding body")
return err
}
ctx, cancel := context.WithTimeout(c.Request().Context(), time.Second*10)
defer cancel()
user, err := h.svc.FindOneByEmailOrUsername(ctx, body.Email, body.Username)
if err != nil {
var se *services.Error
if errors.As(err, &se) {
if se.Kind == services.NotExist {
return h.Validate(c, http.StatusUnauthorized, echo.Map{"message": "invalid email or password"})
}
}
log.Error().Err(err).Msg("failed finding user")
return err
}
err = user.ValidatePassword(body.Password)
if err != nil {
return h.Validate(c, http.StatusUnauthorized, echo.Map{"message": "invalid email or password"})
}
access, refresh, err := user.Login()
if err != nil {
log.Error().Err(err).Msg("failed generating tokens")
return err
}
_, err = h.svc.Update(ctx, "", user)
if err != nil {
log.Error().Err(err).Msg("failed updating user")
return err
}
if viper.GetBool(config.CookiesEnabled) {
cookie.SetToken(c, access, refresh)
}
resp := &LoginResponse{
AccessToken: string(access),
ExpiresIn: int64(viper.GetDuration(config.JWTAccessTokenExpiry).Seconds()),
RefreshToken: string(refresh),
TokenType: "Bearer",
}
return h.Validate(c, http.StatusOK, resp)
}
type LogoutRequest struct {
RefreshToken string `json:"refresh_token"`
}
func (h *AuthHandler) logout(c echo.Context) error {
currentUser := c.Get("user").(*models.User)
encodedToken := c.Get("refresh_token_encoded").(string)
ctx, cancel := context.WithTimeout(c.Request().Context(), time.Second*10)
defer cancel()
user, err := h.svc.Read(ctx, currentUser.Id)
if err != nil {
var se *services.Error
if errors.As(err, &se) {
if se.Kind == services.NotExist {
return h.Validate(c, http.StatusUnauthorized, echo.Map{"message": "token not found"})
}
}
log.Error().Err(err).Msg("failed getting user")
return err
}
if err = user.ValidateRefreshToken(encodedToken); err != nil {
return h.Validate(c, http.StatusUnauthorized, echo.Map{"message": "token mismatch"})
}
user.Logout()
_, err = h.svc.Update(ctx, "", user)
if err != nil {
log.Error().Err(err).Msg("failed updating user")
return err
}
cookie.SetExpiredToken(c)
return h.Validate(c, http.StatusNoContent, nil)
}
type RefreshRequest struct {
GrantType string `json:"grant_type"`
RefreshToken string `json:"refresh_token"`
}
type RefreshResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
TokenType string `json:"token_type"`
}
func (h *AuthHandler) refresh(c echo.Context) error {
token := c.Get("refresh_token").(jwx.Token)
encodedToken := c.Get("refresh_token_encoded").(string)
ctx, cancel := context.WithTimeout(c.Request().Context(), time.Second*10)
defer cancel()
user, err := h.svc.Read(ctx, token.Subject())
if err != nil {
var se *services.Error
if errors.As(err, &se) {
if se.Kind == services.NotExist {
return h.Validate(c, http.StatusUnauthorized, echo.Map{"message": "token not found"})
}
}
log.Error().Err(err).Msg("failed getting user")
return err
}
if err = user.ValidateRefreshToken(encodedToken); err != nil {
return h.Validate(c, http.StatusUnauthorized, echo.Map{"message": "token mismatch"})
}
access, refresh, err := user.Refresh()
if err != nil {
log.Error().Err(err).Msg("failed generating tokens")
return err
}
_, err = h.svc.Update(ctx, "", user)
if err != nil {
log.Error().Err(err).Msg("failed updating user")
return err
}
if viper.GetBool(config.CookiesEnabled) {
cookie.SetToken(c, access, refresh)
}
resp := &RefreshResponse{
AccessToken: string(access),
ExpiresIn: int64(viper.GetDuration(config.JWTAccessTokenExpiry).Seconds()),
RefreshToken: string(refresh),
TokenType: "Bearer",
}
return h.Validate(c, http.StatusOK, resp)
}
type SignUpRequest struct {
Email string `json:"email"`
Username string `json:"username"`
Name string `json:"name"`
Bio string `json:"bio"`
Password string `json:"password"`
}
func (h *AuthHandler) signup(c echo.Context) error {
body := &SignUpRequest{}
if err := c.Bind(body); err != nil {
log.Error().Err(err).Msg("failed binding body")
return err
}
ctx, cancel := context.WithTimeout(c.Request().Context(), time.Second*10)
defer cancel()
res, err := h.svc.FindOneByEmailOrUsername(ctx, body.Email, body.Username)
if err != nil {
var se *services.Error
if errors.As(err, &se) {
if se.Kind == services.Exist {
return h.Validate(c, http.StatusConflict, echo.Map{"message": se.Message})
}
} else {
log.Error().Err(err).Msg("failed getting user")
}
}
user := models.NewUser(body.Email, body.Username)
user.Name = body.Name
user.Bio = body.Bio
err = user.SetPassword(body.Password)
if err != nil {
log.Error().Err(err).Msg("failed setting password")
return err
}
user.Create(user.Id)
res, err = h.svc.Create(ctx, user)
if err != nil {
var se *services.Error
if errors.As(err, &se) {
if se.Kind == services.Exist {
return h.Validate(c, http.StatusConflict, echo.Map{"message": se.Message})
}
} else {
log.Error().Err(err).Msg("failed inserting new user")
}
return err
}
return h.Validate(c, http.StatusOK, res.Response())
}
type TokenResponse struct {
Exp time.Time `json:"exp"`
Iat time.Time `json:"iat"`
Iss string `json:"iss"`
Nbf time.Time `json:"nbf"`
Roles []string `json:"roles"`
Sub string `json:"sub"`
Type string `json:"type"`
}
func (h *AuthHandler) token(c echo.Context) error {
token := c.Get("token").(jwx.Token)
ctx, cancel := context.WithTimeout(c.Request().Context(), time.Second*10)
defer cancel()
m, err := token.AsMap(ctx)
if err != nil {
log.Error().Err(err).Msg("failed converting token to map")
return err
}
return h.Validate(c, http.StatusOK, m)
}