-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.go
381 lines (318 loc) · 10.1 KB
/
middleware.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright (c) 2024 H0llyW00dz All rights reserved.
//
// License: BSD 3-Clause License
package twofa
import (
"encoding/json"
"encoding/xml"
"fmt"
"strings"
"time"
otp "github.com/H0llyW00dzZ/fiber2fa/internal/otpverifier"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"github.com/gofiber/storage/memory/v2"
)
// Middleware represents the 2FA middleware.
type Middleware struct {
Config *Config
Info InfoManager
PathHandlers []PathHandler
}
// PathHandler represents a path handler for the 2FA middleware.
type PathHandler struct {
// Path is the URL path to match for the handler.
Path string
// Handler is the fiber.Handler function to be executed when the path matches.
Handler fiber.Handler
}
// New creates a new instance of the 2FA middleware with the provided configuration.
func New(config ...Config) fiber.Handler {
cfg := DefaultConfig
if len(config) > 0 {
cfg = config[0]
}
// Set default values for JSONMarshal and JSONUnmarshal if not provided
if cfg.JSONMarshal == nil && cfg.JSONUnmarshal == nil {
cfg.JSONMarshal = json.Marshal
cfg.JSONUnmarshal = json.Unmarshal
}
// Directly create the storage inside the middleware if not provided.
if cfg.Storage == nil {
cfg.Storage = memory.New()
}
info := NewInfo(&cfg)
m := &Middleware{
Config: &cfg, // Store a pointer to the config
Info: info,
}
m.PathHandlers = []PathHandler{
{
Path: m.Config.QRCode.PathTemplate,
Handler: m.GenerateQRcodePath,
},
}
// Return the handle method bound to the Middleware instance.
return m.Handle
}
// Handle is the method on Middleware that handles the 2FA authentication process.
func (m *Middleware) Handle(c *fiber.Ctx) error {
// Check if the middleware should be skipped
if m.shouldSkipMiddleware(c) {
return c.Next()
}
// Get the context key
contextKey, err := m.getContextKey(c)
if err != nil {
return m.SendInternalErrorResponse(c, err)
}
// Get the 2FA information from storage
info, err := m.getInfoFromStorage(contextKey)
if err != nil {
return m.SendInternalErrorResponse(c, ErrorFailedToRetrieveInfo)
}
// Check if 2FA information was found in the storage
if info == nil {
// No 2FA information found, handle missing information.
return m.handleMissingInfo(c)
}
// Set the Info field of the Middleware struct
m.Info = info
// Check if the user has a valid 2FA cookie
if m.isValidCookie(c) {
return c.Next()
}
// Find the matching path handler
for _, handler := range m.PathHandlers {
if c.Path() == handler.Path {
return handler.Handler(c)
}
}
// Handle token verification and further processing
return m.handleTokenVerification(c, contextKey)
}
// shouldSkipMiddleware checks if the middleware should be skipped based on the configuration.
func (m *Middleware) shouldSkipMiddleware(c *fiber.Ctx) bool {
// Check if the middleware should be skipped using the Next function
if m.Config.Next != nil && m.Config.Next(c) {
return true
}
// Check if the requested path is in the skip list
if m.isPathSkipped(c.Path()) {
return true
}
return false
}
// handleMissingInfo handles the case when 2FA information is missing.
func (m *Middleware) handleMissingInfo(c *fiber.Ctx) error {
// TODO: Handle missing 2FA mechanism.
// This should be the place where "/2fa/register?account=%s" is in the wild (handled) instead of returning Unauthorized.
return m.SendUnauthorizedResponse(c, fiber.NewError(fiber.StatusUnauthorized, "2FA information not found"))
}
// handleTokenVerification handles the token verification process and further processing.
func (m *Middleware) handleTokenVerification(c *fiber.Ctx, contextKey string) error {
// Extract the token from the specified token lookup sources
token := m.extractToken(c)
if token == "" {
// No token provided, redirecting to 2FA page.
return c.Redirect(m.Config.RedirectURL, fiber.StatusFound)
}
// Verify the provided token and get the updated Info struct
if !m.verifyToken(token) {
// Token is invalid, sending unauthorized response.
return m.SendUnauthorizedResponse(c, fiber.NewError(fiber.StatusUnauthorized, "Invalid 2FA token"))
}
// Check if the requested path is in the skip list
if !m.isPathSkipped(c.Path()) {
if err := m.setCookie(c); err != nil {
return m.SendInternalErrorResponse(c, ErrorFailedToStoreInfo)
}
}
// Store the updated Info struct in the storage
if err := m.updateInfoInStorage(contextKey); err != nil {
return m.SendInternalErrorResponse(c, ErrorFailedToStoreInfo)
}
return c.Next()
}
// isPathSkipped checks if the given path is in the skip list.
func (m *Middleware) isPathSkipped(path string) bool {
// Note: No need to explicitly check for nil, you poggers.
for _, skippedPath := range m.Config.SkipCookies {
if path == skippedPath {
return true
}
}
return false
}
// getContextKey retrieves the context key from c.Locals using the provided ContextKey.
func (m *Middleware) getContextKey(c *fiber.Ctx) (string, error) {
if m.Config.ContextKey == "" {
return "", ErrorContextKeyNotSet
}
contextKeyValue := c.Locals(m.Config.ContextKey)
if contextKeyValue == nil {
return "", ErrorContextKeyNotSet
}
contextKey, ok := contextKeyValue.(string)
if !ok {
return "", ErrorFailedToRetrieveContextKey
}
return contextKey, nil
}
// isValidCookie checks if the user has a valid 2FA cookie.
func (m *Middleware) isValidCookie(c *fiber.Ctx) bool {
// Check if the middleware should be skipped
// Note: No need to explicitly check for boolean by adding if-else statement, you poggers.
m.shouldSkipMiddleware(c)
cookie := c.Cookies(m.Config.CookieName)
if cookie == "" {
return false
}
if !m.validateCookie(cookie) {
// Cookie is no longer valid, delete the Info struct from the storage using the ContextKey from the Info struct
contextKeyValue := m.Info.GetCookieValue()
if err := m.deleteInfoFromStorage(contextKeyValue); err != nil {
// Handle the error if needed
fmt.Println("Failed to delete Info struct from storage:", err)
}
// Redirect to the 2FA URL from the default config
c.Redirect(m.Config.RedirectURL, fiber.StatusFound)
return false
}
return true
}
// setCookie sets the 2FA cookie with an expiration time.
//
// Note: This is suitable for use with encrypted cookies Fiber.
func (m *Middleware) setCookie(c *fiber.Ctx) error {
cookieValue := m.Info.GetCookieValue()
expiresValue := m.Info.GetExpirationTime()
// Set the cookie domain dynamically based on the request's domain if HTTPS is used
cookieDomain := m.Config.CookieDomain
secure := m.Config.CookieSecure
if cookieDomain == "auto" && c.Secure() {
cookieDomain = utils.CopyString(c.Hostname())
secure = true
}
c.Cookie(&fiber.Cookie{
Name: m.Config.CookieName,
Value: cookieValue,
Expires: expiresValue,
Path: m.Config.CookiePath,
Domain: cookieDomain,
Secure: secure,
HTTPOnly: true,
})
return nil
}
// extractToken extracts the token from the specified token lookup sources.
func (m *Middleware) extractToken(c *fiber.Ctx) string {
tokenLookup := m.Config.TokenLookup
sources := strings.Split(tokenLookup, ",")
for _, source := range sources {
parts := strings.Split(source, ":")
if len(parts) != 2 {
continue
}
sourceType := parts[0]
key := parts[1]
switch sourceType {
case "query":
token := c.Query(key)
if token != "" {
return utils.CopyString(token)
}
case "form":
token := c.FormValue(key)
if token != "" {
return utils.CopyString(token)
}
case "cookie":
token := c.Cookies(key)
if token != "" {
return utils.CopyString(token)
}
case "header":
token := c.Get(key)
if strings.HasPrefix(token, "Bearer ") {
return utils.CopyString(strings.TrimPrefix(token, "Bearer "))
}
case "param":
token := c.Params(key)
if token != "" {
return utils.CopyString(token)
}
}
}
return ""
}
// verifyToken verifies the provided token against the user's secret and returns the updated Info struct.
func (m *Middleware) verifyToken(token string) bool {
totp := otp.NewTOTPVerifier(otp.Config{
Secret: m.Info.GetSecret(),
Digits: m.Config.DigitsCount,
Period: m.Config.Period,
SyncWindow: m.Config.SyncWindow,
Hash: m.Config.Hash,
TimeSource: m.Config.TimeSource,
})
if !totp.Verify(token) {
return false
}
expirationTime := time.Now().Add(time.Duration(m.Config.CookieMaxAge) * time.Second)
cookieValue := m.GenerateCookieValue(expirationTime)
m.Info.SetCookieValue(cookieValue)
m.Info.SetExpirationTime(expirationTime)
return true
}
// SendUnauthorizedResponse sends an unauthorized response based on the configured MIME type.
func (m *Middleware) SendUnauthorizedResponse(c *fiber.Ctx, err error) error {
c.Status(fiber.StatusUnauthorized)
if m.Config.UnauthorizedHandler != nil {
return m.Config.UnauthorizedHandler(c, err)
}
switch m.Config.ResponseMIME {
case fiber.MIMEApplicationJSON,
fiber.MIMEApplicationJSONCharsetUTF8:
return c.JSON(fiber.Map{"error": err.Error()})
case fiber.MIMEApplicationXML,
fiber.MIMEApplicationXMLCharsetUTF8:
return c.XML(struct {
XMLName xml.Name `xml:"error"`
Message string `xml:"message"`
}{
Message: err.Error(),
})
default:
return c.SendString(err.Error())
}
}
// SendInternalErrorResponse sends an internal server error response based on the configured MIME type.
func (m *Middleware) SendInternalErrorResponse(c *fiber.Ctx, err error) error {
c.Status(fiber.StatusInternalServerError)
if m.Config.InternalErrorHandler != nil {
return m.Config.InternalErrorHandler(c, err)
}
switch m.Config.ResponseMIME {
case fiber.MIMEApplicationJSON,
fiber.MIMEApplicationJSONCharsetUTF8:
return c.JSON(fiber.Map{"error": err.Error()})
case fiber.MIMEApplicationXML,
fiber.MIMEApplicationXMLCharsetUTF8:
return c.XML(struct {
XMLName xml.Name `xml:"error"`
Message string `xml:"message"`
}{
Message: err.Error(),
})
default:
return c.SendString(err.Error())
}
}
// GenerateIdentifier generates an identifier using the configured identifier generator.
func (m *Middleware) GenerateIdentifier(c *fiber.Ctx) string {
if m.Config.IdentifierGenerator != nil {
return m.Config.IdentifierGenerator(c)
}
return utils.UUIDv4()
}