forked from sigbit/mcp-auth-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
316 lines (284 loc) · 8.03 KB
/
auth.go
File metadata and controls
316 lines (284 loc) · 8.03 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
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
package auth
import (
"embed"
"encoding/json"
"errors"
"html/template"
"net/http"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/sigbit/mcp-auth-proxy/pkg/utils"
"golang.org/x/crypto/bcrypt"
)
//go:embed templates/*
var templateFS embed.FS
type AuthRouter struct {
passwordHash []string
providers []Provider
loginTemplate *template.Template
unauthorizedTemplate *template.Template
errorTemplate *template.Template
// When true, do not auto-redirect to the sole provider even if
// there is only one provider and no password is set.
noProviderAutoSelect bool
// userInfoFields is a list of top-level keys to retain from the
// provider's userinfo response. When non-empty, all other keys are
// stripped before the data is stored in the session cookie. This
// prevents oversized cookies when the provider returns many claims.
userInfoFields []string
}
func NewAuthRouter(passwordHash []string, noProviderAutoSelect bool, userInfoFields []string, providers ...Provider) (*AuthRouter, error) {
tmpl, err := template.ParseFS(templateFS, "templates/login.html")
if err != nil {
return nil, err
}
unauthorizedTmpl, err := template.ParseFS(templateFS, "templates/unauthorized.html")
if err != nil {
return nil, err
}
errorTmpl, err := template.ParseFS(templateFS, "templates/error.html")
if err != nil {
return nil, err
}
return &AuthRouter{
passwordHash: passwordHash,
providers: providers,
loginTemplate: tmpl,
unauthorizedTemplate: unauthorizedTmpl,
errorTemplate: errorTmpl,
noProviderAutoSelect: noProviderAutoSelect,
userInfoFields: userInfoFields,
}, nil
}
const (
LoginEndpoint = "/.auth/login"
LogoutEndpoint = "/.auth/logout"
GoogleAuthEndpoint = "/.auth/google"
GoogleCallbackEndpoint = "/.auth/google/callback"
GitHubAuthEndpoint = "/.auth/github"
GitHubCallbackEndpoint = "/.auth/github/callback"
OIDCAuthEndpoint = "/.auth/oidc"
OIDCCallbackEndpoint = "/.auth/oidc/callback"
PasswordProvider = "password"
PasswordUserID = "password_user"
SessionKeyAuthorized = "authorized"
SessionKeyRedirectURL = "redirect_url"
SessionKeyOAuthState = "oauth_state"
SessionKeyUserID = "user_id"
SessionKeyUserInfo = "user_info"
)
func (a *AuthRouter) SetupRoutes(router gin.IRouter) {
router.GET(LoginEndpoint, a.handleLogin)
router.POST(LoginEndpoint, a.handleLoginPost)
router.GET(LogoutEndpoint, a.handleLogout)
for _, provider := range a.providers {
router.GET(provider.RedirectURL(), func(c *gin.Context) {
session := sessions.Default(c)
state := session.Get(SessionKeyOAuthState)
if state == nil {
a.renderError(c, errors.New("OAuth state is missing"))
return
}
token, err := provider.Exchange(c, state.(string))
if err != nil {
a.renderError(c, err)
return
}
ok, user, userInfo, err := provider.Authorization(c, token)
if err != nil {
a.renderError(c, err)
return
}
if !ok {
a.renderUnauthorized(c, user, provider.Name())
return
}
session.Set(SessionKeyAuthorized, true)
session.Set(SessionKeyUserID, user)
if userInfo != nil {
if len(a.userInfoFields) > 0 {
userInfo = filterUserInfo(userInfo, a.userInfoFields)
}
if userInfoJSON, err := json.Marshal(userInfo); err == nil {
session.Set(SessionKeyUserInfo, string(userInfoJSON))
}
}
redirectURL := session.Get(SessionKeyRedirectURL)
if redirectURL != nil {
session.Delete(SessionKeyRedirectURL)
}
if err := session.Save(); err != nil {
a.renderError(c, err)
return
}
if redirectURL == nil {
c.Redirect(http.StatusFound, "/")
} else {
c.Redirect(http.StatusFound, redirectURL.(string))
}
})
router.GET(provider.AuthURL(), func(c *gin.Context) {
session := sessions.Default(c)
state, err := utils.GenerateState()
if err != nil {
a.renderError(c, err)
return
}
url, err := provider.AuthCodeURL(state)
if err != nil {
a.renderError(c, err)
return
}
session.Set(SessionKeyOAuthState, state)
if err := session.Save(); err != nil {
a.renderError(c, err)
return
}
c.Redirect(http.StatusFound, url)
})
}
}
func (a *AuthRouter) handleLogin(c *gin.Context) {
if c.Request.Method == "POST" {
a.handleLoginPost(c)
return
}
// Auto-redirect to the sole provider if enabled and no password is set
if !a.noProviderAutoSelect && len(a.passwordHash) == 0 && len(a.providers) == 1 {
c.Redirect(http.StatusFound, a.providers[0].AuthURL())
return
}
a.renderLogin(c, "")
}
func (a *AuthRouter) handleLoginPost(c *gin.Context) {
password := c.PostForm("password")
var errorMessage string
if password == "" {
errorMessage = "Password is required"
} else {
var isValid bool
for _, hash := range a.passwordHash {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
if err == nil {
isValid = true
break
}
}
if !isValid {
errorMessage = "Invalid password"
}
}
if errorMessage != "" {
a.renderLogin(c, errorMessage)
return
}
session := sessions.Default(c)
session.Set(SessionKeyAuthorized, true)
session.Set(SessionKeyUserID, PasswordUserID)
redirectURL := session.Get(SessionKeyRedirectURL)
if redirectURL != nil {
session.Delete(SessionKeyRedirectURL)
}
if err := session.Save(); err != nil {
a.renderError(c, err)
return
}
if redirectURL == nil {
c.Redirect(http.StatusFound, "/")
} else {
c.Redirect(http.StatusFound, redirectURL.(string))
}
}
func (a *AuthRouter) handleLogout(c *gin.Context) {
session := sessions.Default(c)
session.Delete(SessionKeyAuthorized)
if err := session.Save(); err != nil {
a.renderError(c, err)
return
}
c.Redirect(http.StatusFound, LoginEndpoint)
}
func (a *AuthRouter) RequireAuth() gin.HandlerFunc {
return func(c *gin.Context) {
session := sessions.Default(c)
authorized := session.Get(SessionKeyAuthorized)
if authorized == nil {
session.Set(SessionKeyRedirectURL, c.Request.URL.String())
if err := session.Save(); err != nil {
a.renderError(c, err)
return
}
c.Redirect(http.StatusFound, LoginEndpoint)
return
}
if !authorized.(bool) {
// not expected
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
return
}
c.Next()
}
}
type loginTemplateData struct {
Providers []Provider
HasPassword bool
PasswordError string
}
type unauthorizedTemplateData struct {
UserID string
Provider string
}
type errorTemplateData struct {
ErrorMessage string
}
func (a *AuthRouter) renderLogin(c *gin.Context, passwordError string) {
data := loginTemplateData{
Providers: a.providers,
HasPassword: len(a.passwordHash) > 0,
PasswordError: passwordError,
}
c.Header("Content-Type", "text/html; charset=utf-8")
if passwordError != "" {
c.Status(http.StatusBadRequest)
} else {
c.Status(http.StatusOK)
}
if err := a.loginTemplate.Execute(c.Writer, data); err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
func (a *AuthRouter) renderUnauthorized(c *gin.Context, userID, providerName string) {
data := unauthorizedTemplateData{
UserID: userID,
Provider: providerName,
}
c.Header("Content-Type", "text/html; charset=utf-8")
c.Status(http.StatusForbidden)
if err := a.unauthorizedTemplate.Execute(c.Writer, data); err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
// filterUserInfo returns a copy of m containing only the listed keys.
func filterUserInfo(m map[string]any, keys []string) map[string]any {
filtered := make(map[string]any, len(keys))
for _, k := range keys {
if v, ok := m[k]; ok {
filtered[k] = v
}
}
return filtered
}
func (a *AuthRouter) renderError(c *gin.Context, err error) {
data := errorTemplateData{
ErrorMessage: err.Error(),
}
c.Header("Content-Type", "text/html; charset=utf-8")
c.Status(http.StatusInternalServerError)
if templateErr := a.errorTemplate.Execute(c.Writer, data); templateErr != nil {
c.AbortWithError(http.StatusInternalServerError, templateErr)
return
}
c.Abort()
}