Skip to content

Commit fb3ffeb

Browse files
authored
Add sso.Group, context.Auth, context.APIAuth to allow auth special routes (#16086)
* Add sso.Group, context.Auth, context.APIAuth to allow auth special routes * Remove unnecessary check * Rename sso -> auth * remove unused method of Auth interface
1 parent da05799 commit fb3ffeb

19 files changed

+286
-220
lines changed

modules/auth/sso/session.go

Lines changed: 0 additions & 48 deletions
This file was deleted.

modules/auth/sso/user.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

modules/context/api.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
"strings"
1515

1616
"code.gitea.io/gitea/models"
17-
"code.gitea.io/gitea/modules/auth/sso"
1817
"code.gitea.io/gitea/modules/git"
1918
"code.gitea.io/gitea/modules/log"
2019
"code.gitea.io/gitea/modules/setting"
2120
"code.gitea.io/gitea/modules/web/middleware"
21+
"code.gitea.io/gitea/services/auth"
2222

2323
"gitea.com/go-chi/session"
2424
)
@@ -217,6 +217,26 @@ func (ctx *APIContext) CheckForOTP() {
217217
}
218218
}
219219

220+
// APIAuth converts auth.Auth as a middleware
221+
func APIAuth(authMethod auth.Auth) func(*APIContext) {
222+
return func(ctx *APIContext) {
223+
// Get user from session if logged in.
224+
ctx.User = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
225+
if ctx.User != nil {
226+
ctx.IsBasicAuth = ctx.Data["AuthedMethod"].(string) == new(auth.Basic).Name()
227+
ctx.IsSigned = true
228+
ctx.Data["IsSigned"] = ctx.IsSigned
229+
ctx.Data["SignedUser"] = ctx.User
230+
ctx.Data["SignedUserID"] = ctx.User.ID
231+
ctx.Data["SignedUserName"] = ctx.User.Name
232+
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
233+
} else {
234+
ctx.Data["SignedUserID"] = int64(0)
235+
ctx.Data["SignedUserName"] = ""
236+
}
237+
}
238+
}
239+
220240
// APIContexter returns apicontext as middleware
221241
func APIContexter() func(http.Handler) http.Handler {
222242
var csrfOpts = getCsrfOpts()
@@ -250,20 +270,6 @@ func APIContexter() func(http.Handler) http.Handler {
250270
}
251271
}
252272

253-
// Get user from session if logged in.
254-
ctx.User, ctx.IsBasicAuth = sso.SignedInUser(ctx.Req, ctx.Resp, &ctx, ctx.Session)
255-
if ctx.User != nil {
256-
ctx.IsSigned = true
257-
ctx.Data["IsSigned"] = ctx.IsSigned
258-
ctx.Data["SignedUser"] = ctx.User
259-
ctx.Data["SignedUserID"] = ctx.User.ID
260-
ctx.Data["SignedUserName"] = ctx.User.Name
261-
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
262-
} else {
263-
ctx.Data["SignedUserID"] = int64(0)
264-
ctx.Data["SignedUserName"] = ""
265-
}
266-
267273
ctx.Resp.Header().Set(`X-Frame-Options`, `SAMEORIGIN`)
268274

269275
ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken())

modules/context/context.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ import (
2121
"time"
2222

2323
"code.gitea.io/gitea/models"
24-
"code.gitea.io/gitea/modules/auth/sso"
2524
"code.gitea.io/gitea/modules/base"
2625
mc "code.gitea.io/gitea/modules/cache"
2726
"code.gitea.io/gitea/modules/log"
2827
"code.gitea.io/gitea/modules/setting"
2928
"code.gitea.io/gitea/modules/templates"
3029
"code.gitea.io/gitea/modules/translation"
3130
"code.gitea.io/gitea/modules/web/middleware"
31+
"code.gitea.io/gitea/services/auth"
3232

3333
"gitea.com/go-chi/cache"
3434
"gitea.com/go-chi/session"
@@ -605,6 +605,28 @@ func getCsrfOpts() CsrfOptions {
605605
}
606606
}
607607

608+
// Auth converts auth.Auth as a middleware
609+
func Auth(authMethod auth.Auth) func(*Context) {
610+
return func(ctx *Context) {
611+
ctx.User = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
612+
if ctx.User != nil {
613+
ctx.IsBasicAuth = ctx.Data["AuthedMethod"].(string) == new(auth.Basic).Name()
614+
ctx.IsSigned = true
615+
ctx.Data["IsSigned"] = ctx.IsSigned
616+
ctx.Data["SignedUser"] = ctx.User
617+
ctx.Data["SignedUserID"] = ctx.User.ID
618+
ctx.Data["SignedUserName"] = ctx.User.Name
619+
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
620+
} else {
621+
ctx.Data["SignedUserID"] = int64(0)
622+
ctx.Data["SignedUserName"] = ""
623+
624+
// ensure the session uid is deleted
625+
_ = ctx.Session.Delete("uid")
626+
}
627+
}
628+
}
629+
608630
// Contexter initializes a classic context for a request.
609631
func Contexter() func(next http.Handler) http.Handler {
610632
var rnd = templates.HTMLRenderer()
@@ -690,24 +712,6 @@ func Contexter() func(next http.Handler) http.Handler {
690712
}
691713
}
692714

693-
// Get user from session if logged in.
694-
ctx.User, ctx.IsBasicAuth = sso.SignedInUser(ctx.Req, ctx.Resp, &ctx, ctx.Session)
695-
696-
if ctx.User != nil {
697-
ctx.IsSigned = true
698-
ctx.Data["IsSigned"] = ctx.IsSigned
699-
ctx.Data["SignedUser"] = ctx.User
700-
ctx.Data["SignedUserID"] = ctx.User.ID
701-
ctx.Data["SignedUserName"] = ctx.User.Name
702-
ctx.Data["IsAdmin"] = ctx.User.IsAdmin
703-
} else {
704-
ctx.Data["SignedUserID"] = int64(0)
705-
ctx.Data["SignedUserName"] = ""
706-
707-
// ensure the session uid is deleted
708-
_ = ctx.Session.Delete("uid")
709-
}
710-
711715
ctx.Resp.Header().Set(`X-Frame-Options`, `SAMEORIGIN`)
712716

713717
ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken())

routers/api/v1/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import (
8383
"code.gitea.io/gitea/routers/api/v1/settings"
8484
_ "code.gitea.io/gitea/routers/api/v1/swagger" // for swagger generation
8585
"code.gitea.io/gitea/routers/api/v1/user"
86+
"code.gitea.io/gitea/services/auth"
8687
"code.gitea.io/gitea/services/forms"
8788

8889
"gitea.com/go-chi/binding"
@@ -573,6 +574,9 @@ func Routes() *web.Route {
573574
}
574575
m.Use(context.APIContexter())
575576

577+
// Get user from session if logged in.
578+
m.Use(context.APIAuth(auth.NewGroup(auth.Methods()...)))
579+
576580
m.Use(context.ToggleAPI(&context.ToggleOptions{
577581
SignInRequired: setting.Service.RequireSignInView,
578582
}))

routers/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
"code.gitea.io/gitea/models"
12-
"code.gitea.io/gitea/modules/auth/sso"
1312
"code.gitea.io/gitea/modules/cache"
1413
"code.gitea.io/gitea/modules/cron"
1514
"code.gitea.io/gitea/modules/eventsource"
@@ -34,6 +33,7 @@ import (
3433
"code.gitea.io/gitea/routers/common"
3534
"code.gitea.io/gitea/routers/private"
3635
web_routers "code.gitea.io/gitea/routers/web"
36+
"code.gitea.io/gitea/services/auth"
3737
"code.gitea.io/gitea/services/mailer"
3838
mirror_service "code.gitea.io/gitea/services/mirror"
3939
pull_service "code.gitea.io/gitea/services/pull"
@@ -134,7 +134,7 @@ func GlobalInit(ctx context.Context) {
134134
} else {
135135
ssh.Unused()
136136
}
137-
sso.Init()
137+
auth.Init()
138138

139139
svg.Init()
140140
}

routers/web/base.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import (
1515
"strings"
1616

1717
"code.gitea.io/gitea/models"
18-
"code.gitea.io/gitea/modules/auth/sso"
1918
"code.gitea.io/gitea/modules/context"
2019
"code.gitea.io/gitea/modules/httpcache"
2120
"code.gitea.io/gitea/modules/log"
2221
"code.gitea.io/gitea/modules/setting"
2322
"code.gitea.io/gitea/modules/storage"
2423
"code.gitea.io/gitea/modules/templates"
2524
"code.gitea.io/gitea/modules/web/middleware"
25+
"code.gitea.io/gitea/services/auth"
2626

2727
"gitea.com/go-chi/session"
2828
)
@@ -158,7 +158,7 @@ func Recovery() func(next http.Handler) http.Handler {
158158
}
159159
if user == nil {
160160
// Get user from session if logged in - do not attempt to sign-in
161-
user = sso.SessionUser(sessionStore)
161+
user = auth.SessionUser(sessionStore)
162162
}
163163
if user != nil {
164164
store["IsSigned"] = true

routers/web/user/oauth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313
"strings"
1414

1515
"code.gitea.io/gitea/models"
16-
"code.gitea.io/gitea/modules/auth/sso"
1716
"code.gitea.io/gitea/modules/base"
1817
"code.gitea.io/gitea/modules/context"
1918
"code.gitea.io/gitea/modules/log"
2019
"code.gitea.io/gitea/modules/setting"
2120
"code.gitea.io/gitea/modules/timeutil"
2221
"code.gitea.io/gitea/modules/web"
22+
"code.gitea.io/gitea/services/auth"
2323
"code.gitea.io/gitea/services/forms"
2424

2525
"gitea.com/go-chi/binding"
@@ -228,7 +228,7 @@ func InfoOAuth(ctx *context.Context) {
228228
ctx.HandleText(http.StatusUnauthorized, "no valid auth token authorization")
229229
return
230230
}
231-
uid := sso.CheckOAuthAccessToken(auths[1])
231+
uid := auth.CheckOAuthAccessToken(auths[1])
232232
if uid == 0 {
233233
handleBearerTokenError(ctx, BearerTokenError{
234234
ErrorCode: BearerTokenErrorCodeInvalidToken,

routers/web/web.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"code.gitea.io/gitea/routers/web/repo"
3232
"code.gitea.io/gitea/routers/web/user"
3333
userSetting "code.gitea.io/gitea/routers/web/user/setting"
34+
"code.gitea.io/gitea/services/auth"
3435
"code.gitea.io/gitea/services/forms"
3536
"code.gitea.io/gitea/services/lfs"
3637
"code.gitea.io/gitea/services/mailer"
@@ -149,6 +150,9 @@ func Routes() *web.Route {
149150
// Removed: toolbox.Toolboxer middleware will provide debug informations which seems unnecessary
150151
common = append(common, context.Contexter())
151152

153+
// Get user from session if logged in.
154+
common = append(common, context.Auth(auth.NewGroup(auth.Methods()...)))
155+
152156
// GetHead allows a HEAD request redirect to GET if HEAD method is not defined for that route
153157
common = append(common, middleware.GetHead)
154158

0 commit comments

Comments
 (0)