mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	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
This commit is contained in:
		@@ -14,11 +14,11 @@ import (
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
	"code.gitea.io/gitea/modules/auth/sso"
 | 
			
		||||
	"code.gitea.io/gitea/modules/git"
 | 
			
		||||
	"code.gitea.io/gitea/modules/log"
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"code.gitea.io/gitea/modules/web/middleware"
 | 
			
		||||
	"code.gitea.io/gitea/services/auth"
 | 
			
		||||
 | 
			
		||||
	"gitea.com/go-chi/session"
 | 
			
		||||
)
 | 
			
		||||
@@ -217,6 +217,26 @@ func (ctx *APIContext) CheckForOTP() {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// APIAuth converts auth.Auth as a middleware
 | 
			
		||||
func APIAuth(authMethod auth.Auth) func(*APIContext) {
 | 
			
		||||
	return func(ctx *APIContext) {
 | 
			
		||||
		// Get user from session if logged in.
 | 
			
		||||
		ctx.User = authMethod.Verify(ctx.Req, ctx.Resp, ctx, ctx.Session)
 | 
			
		||||
		if ctx.User != nil {
 | 
			
		||||
			ctx.IsBasicAuth = ctx.Data["AuthedMethod"].(string) == new(auth.Basic).Name()
 | 
			
		||||
			ctx.IsSigned = true
 | 
			
		||||
			ctx.Data["IsSigned"] = ctx.IsSigned
 | 
			
		||||
			ctx.Data["SignedUser"] = ctx.User
 | 
			
		||||
			ctx.Data["SignedUserID"] = ctx.User.ID
 | 
			
		||||
			ctx.Data["SignedUserName"] = ctx.User.Name
 | 
			
		||||
			ctx.Data["IsAdmin"] = ctx.User.IsAdmin
 | 
			
		||||
		} else {
 | 
			
		||||
			ctx.Data["SignedUserID"] = int64(0)
 | 
			
		||||
			ctx.Data["SignedUserName"] = ""
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// APIContexter returns apicontext as middleware
 | 
			
		||||
func APIContexter() func(http.Handler) http.Handler {
 | 
			
		||||
	var csrfOpts = getCsrfOpts()
 | 
			
		||||
@@ -250,20 +270,6 @@ func APIContexter() func(http.Handler) http.Handler {
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			// Get user from session if logged in.
 | 
			
		||||
			ctx.User, ctx.IsBasicAuth = sso.SignedInUser(ctx.Req, ctx.Resp, &ctx, ctx.Session)
 | 
			
		||||
			if ctx.User != nil {
 | 
			
		||||
				ctx.IsSigned = true
 | 
			
		||||
				ctx.Data["IsSigned"] = ctx.IsSigned
 | 
			
		||||
				ctx.Data["SignedUser"] = ctx.User
 | 
			
		||||
				ctx.Data["SignedUserID"] = ctx.User.ID
 | 
			
		||||
				ctx.Data["SignedUserName"] = ctx.User.Name
 | 
			
		||||
				ctx.Data["IsAdmin"] = ctx.User.IsAdmin
 | 
			
		||||
			} else {
 | 
			
		||||
				ctx.Data["SignedUserID"] = int64(0)
 | 
			
		||||
				ctx.Data["SignedUserName"] = ""
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			ctx.Resp.Header().Set(`X-Frame-Options`, `SAMEORIGIN`)
 | 
			
		||||
 | 
			
		||||
			ctx.Data["CsrfToken"] = html.EscapeString(ctx.csrf.GetToken())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user