Repository navigation
Expand file tree
/
Copy pathauth.go
More file actions
139 lines (121 loc) · 3.83 KB
/
Copy pathauth.go
File metadata and controls
139 lines (121 loc) · 3.83 KB
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
package middleware
import (
"context"
"net/http"
"strings"
"staticsend/pkg/auth"
"staticsend/pkg/database"
"staticsend/pkg/models"
)
// Context keys for storing authentication data
type contextKey string
const (
// UserKey is the context key for storing user object
UserKey contextKey = "user"
// ClaimsKey is the context key for storing JWT claims
ClaimsKey contextKey = "claims"
)
// AuthConfig holds authentication configuration
type AuthConfig struct {
SecretKey []byte
DB *database.Database
// Optional: paths that don't require authentication
PublicPaths []string
}
// AuthMiddleware provides JWT authentication middleware with cookie support
func AuthMiddleware(config AuthConfig) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Skip authentication for public paths
if isPublicPath(r.URL.Path, config.PublicPaths) {
next.ServeHTTP(w, r)
return
}
var tokenString string
var err error
// First try to get token from Authorization header
tokenString, err = auth.GetTokenFromRequest(r)
if err != nil {
// If no Authorization header, try to get from cookie
if cookie, err := r.Cookie("auth_token"); err == nil {
tokenString = cookie.Value
} else {
// For web requests, redirect to login instead of 401 error
if r.Header.Get("HX-Request") == "true" {
// HTMX request, return 401
http.Error(w, "Unauthorized: authentication required", http.StatusUnauthorized)
} else {
// Regular browser request, redirect to login
http.Redirect(w, r, "/login", http.StatusFound)
}
return
}
}
claims, err := auth.ValidateToken(tokenString, config.SecretKey)
if err != nil {
// Invalid token - clear the bad cookie and redirect to login
http.SetCookie(w, &http.Cookie{
Name: "auth_token",
Value: "",
Path: "/",
HttpOnly: true,
Secure: false,
MaxAge: -1,
})
if r.Header.Get("HX-Request") == "true" {
http.Error(w, "Unauthorized: invalid token", http.StatusUnauthorized)
} else {
http.Redirect(w, r, "/login", http.StatusFound)
}
return
}
userID, err := auth.GetUserIDFromToken(claims)
if err != nil {
http.Error(w, "Unauthorized: invalid token claims", http.StatusUnauthorized)
return
}
// Get user from database
user, err := models.GetUserByID(config.DB.Connection, userID)
if err != nil || user == nil {
// User not found - clear the bad cookie and redirect to login
http.SetCookie(w, &http.Cookie{
Name: "auth_token",
Value: "",
Path: "/",
HttpOnly: true,
Secure: false,
MaxAge: -1,
})
if r.Header.Get("HX-Request") == "true" {
http.Error(w, "Unauthorized: user not found", http.StatusUnauthorized)
} else {
http.Redirect(w, r, "/login", http.StatusFound)
}
return
}
// Add user and claims to context
ctx := context.WithValue(r.Context(), UserKey, user)
ctx = context.WithValue(ctx, ClaimsKey, claims)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
}
// GetUserFromContext retrieves the user from request context
func GetUserFromContext(ctx context.Context) (*models.User, bool) {
user, ok := ctx.Value(UserKey).(*models.User)
return user, ok
}
// GetClaimsFromContext retrieves the JWT claims from request context
func GetClaimsFromContext(ctx context.Context) (map[string]interface{}, bool) {
claims, ok := ctx.Value(ClaimsKey).(map[string]interface{})
return claims, ok
}
// isPublicPath checks if the current path should bypass authentication
func isPublicPath(path string, publicPaths []string) bool {
for _, publicPath := range publicPaths {
if path == publicPath || strings.HasPrefix(path, publicPath+"/") {
return true
}
}
return false
}