2019-11-23 01:33:31 +02:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-23 01:33:31 +02:00
|
|
|
|
2021-06-10 01:53:16 +08:00
|
|
|
package auth
|
2019-11-23 01:33:31 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-05 21:05:40 +08:00
|
|
|
"net/http"
|
2021-05-15 16:32:09 +01:00
|
|
|
"regexp"
|
2019-11-23 01:33:31 +02:00
|
|
|
"strings"
|
2025-01-25 22:36:47 +08:00
|
|
|
"sync"
|
2019-11-23 01:33:31 +02:00
|
|
|
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-01-14 23:03:31 +08:00
|
|
|
"code.gitea.io/gitea/modules/auth/webauthn"
|
2019-11-23 01:33:31 +02:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-02-04 14:29:09 +01:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2021-12-20 14:12:26 +00:00
|
|
|
"code.gitea.io/gitea/modules/session"
|
2021-05-15 16:32:09 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-30 16:55:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2024-02-27 15:12:22 +08:00
|
|
|
gitea_context "code.gitea.io/gitea/services/context"
|
2024-02-04 14:29:09 +01:00
|
|
|
user_service "code.gitea.io/gitea/services/user"
|
2019-11-23 01:33:31 +02:00
|
|
|
)
|
|
|
|
|
2025-01-25 22:36:47 +08:00
|
|
|
type globalVarsStruct struct {
|
|
|
|
gitRawOrAttachPathRe *regexp.Regexp
|
|
|
|
lfsPathRe *regexp.Regexp
|
|
|
|
archivePathRe *regexp.Regexp
|
2025-01-27 03:07:39 +01:00
|
|
|
feedPathRe *regexp.Regexp
|
|
|
|
feedRefPathRe *regexp.Regexp
|
2025-01-25 22:36:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var globalVars = sync.OnceValue(func() *globalVarsStruct {
|
|
|
|
return &globalVarsStruct{
|
2025-01-27 03:07:39 +01:00
|
|
|
gitRawOrAttachPathRe: regexp.MustCompile(`^/[-.\w]+/[-.\w]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/)|(?:attachments/))`),
|
|
|
|
lfsPathRe: regexp.MustCompile(`^/[-.\w]+/[-.\w]+/info/lfs/`),
|
|
|
|
archivePathRe: regexp.MustCompile(`^/[-.\w]+/[-.\w]+/archive/`),
|
|
|
|
feedPathRe: regexp.MustCompile(`^/[-.\w]+(/[-.\w]+)?\.(rss|atom)$`), // "/owner.rss" or "/owner/repo.atom"
|
|
|
|
feedRefPathRe: regexp.MustCompile(`^/[-.\w]+/[-.\w]+/(rss|atom)/`), // "/owner/repo/rss/branch/..."
|
2025-01-25 22:36:47 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-10 01:53:16 +08:00
|
|
|
// Init should be called exactly once when the application starts to allow plugins
|
2019-11-23 01:33:31 +02:00
|
|
|
// to allocate necessary resources
|
|
|
|
func Init() {
|
2022-01-14 23:03:31 +08:00
|
|
|
webauthn.Init()
|
2019-11-23 01:33:31 +02:00
|
|
|
}
|
|
|
|
|
2025-01-25 22:36:47 +08:00
|
|
|
type authPathDetector struct {
|
|
|
|
req *http.Request
|
|
|
|
vars *globalVarsStruct
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAuthPathDetector(req *http.Request) *authPathDetector {
|
|
|
|
return &authPathDetector{req: req, vars: globalVars()}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isAPIPath returns true if the specified URL is an API path
|
|
|
|
func (a *authPathDetector) isAPIPath() bool {
|
|
|
|
return strings.HasPrefix(a.req.URL.Path, "/api/")
|
|
|
|
}
|
|
|
|
|
2019-11-23 01:33:31 +02:00
|
|
|
// isAttachmentDownload check if request is a file download (GET) with URL to an attachment
|
2025-01-25 22:36:47 +08:00
|
|
|
func (a *authPathDetector) isAttachmentDownload() bool {
|
|
|
|
return strings.HasPrefix(a.req.URL.Path, "/attachments/") && a.req.Method == "GET"
|
2019-11-23 01:33:31 +02:00
|
|
|
}
|
|
|
|
|
2025-01-27 03:07:39 +01:00
|
|
|
func (a *authPathDetector) isFeedRequest(req *http.Request) bool {
|
|
|
|
if !setting.Other.EnableFeed {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if req.Method != "GET" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return a.vars.feedPathRe.MatchString(req.URL.Path) || a.vars.feedRefPathRe.MatchString(req.URL.Path)
|
|
|
|
}
|
|
|
|
|
2022-03-30 10:42:47 +02:00
|
|
|
// isContainerPath checks if the request targets the container endpoint
|
2025-01-25 22:36:47 +08:00
|
|
|
func (a *authPathDetector) isContainerPath() bool {
|
|
|
|
return strings.HasPrefix(a.req.URL.Path, "/v2/")
|
2022-03-30 10:42:47 +02:00
|
|
|
}
|
|
|
|
|
2025-01-25 22:36:47 +08:00
|
|
|
func (a *authPathDetector) isGitRawOrAttachPath() bool {
|
|
|
|
return a.vars.gitRawOrAttachPathRe.MatchString(a.req.URL.Path)
|
2023-10-10 23:33:56 +08:00
|
|
|
}
|
|
|
|
|
2025-01-25 22:36:47 +08:00
|
|
|
func (a *authPathDetector) isGitRawOrAttachOrLFSPath() bool {
|
|
|
|
if a.isGitRawOrAttachPath() {
|
2021-05-15 16:32:09 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if setting.LFS.StartServer {
|
2025-01-25 22:36:47 +08:00
|
|
|
return a.vars.lfsPathRe.MatchString(a.req.URL.Path)
|
2021-05-15 16:32:09 +01:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2025-01-25 22:36:47 +08:00
|
|
|
func (a *authPathDetector) isArchivePath() bool {
|
|
|
|
return a.vars.archivePathRe.MatchString(a.req.URL.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *authPathDetector) isAuthenticatedTokenRequest() bool {
|
|
|
|
switch a.req.URL.Path {
|
|
|
|
case "/login/oauth/userinfo", "/login/oauth/introspect":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2024-02-24 01:49:46 +08:00
|
|
|
}
|
|
|
|
|
2019-11-23 01:33:31 +02:00
|
|
|
// handleSignIn clears existing session variables and stores new ones for the specified user object
|
2021-11-24 17:49:20 +08:00
|
|
|
func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore, user *user_model.User) {
|
2021-12-20 14:12:26 +00:00
|
|
|
// We need to regenerate the session...
|
|
|
|
newSess, err := session.RegenerateSession(resp, req)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Error regenerating session: %v", err))
|
|
|
|
} else {
|
|
|
|
sess = newSess
|
|
|
|
}
|
|
|
|
|
2019-11-23 01:33:31 +02:00
|
|
|
_ = sess.Delete("openid_verified_uri")
|
|
|
|
_ = sess.Delete("openid_signin_remember")
|
|
|
|
_ = sess.Delete("openid_determined_email")
|
|
|
|
_ = sess.Delete("openid_determined_username")
|
|
|
|
_ = sess.Delete("twofaUid")
|
|
|
|
_ = sess.Delete("twofaRemember")
|
2022-01-14 23:03:31 +08:00
|
|
|
_ = sess.Delete("webauthnAssertion")
|
2019-11-23 01:33:31 +02:00
|
|
|
_ = sess.Delete("linkAccount")
|
2021-12-20 14:12:26 +00:00
|
|
|
err = sess.Set("uid", user.ID)
|
2019-11-23 01:33:31 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Error setting session: %v", err))
|
|
|
|
}
|
|
|
|
err = sess.Set("uname", user.Name)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Error setting session: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Language setting of the user overwrites the one previously set
|
|
|
|
// If the user does not have a locale set, we save the current one.
|
|
|
|
if len(user.Language) == 0 {
|
2021-01-30 16:55:53 +08:00
|
|
|
lc := middleware.Locale(resp, req)
|
2024-02-04 14:29:09 +01:00
|
|
|
opts := &user_service.UpdateOptions{
|
|
|
|
Language: optional.Some(lc.Language()),
|
|
|
|
}
|
|
|
|
if err := user_service.UpdateUser(req.Context(), user, opts); err != nil {
|
2019-11-23 01:33:31 +02:00
|
|
|
log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", user.ID, user.Language))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 08:12:43 +00:00
|
|
|
middleware.SetLocaleCookie(resp, user.Language, 0)
|
2019-11-23 01:33:31 +02:00
|
|
|
|
2024-10-10 11:48:21 +08:00
|
|
|
// force to generate a new CSRF token
|
2023-05-23 09:29:15 +08:00
|
|
|
if ctx := gitea_context.GetWebContext(req); ctx != nil {
|
2024-10-10 11:48:21 +08:00
|
|
|
ctx.Csrf.PrepareForSessionUser(ctx)
|
2023-04-14 03:45:33 +08:00
|
|
|
}
|
2019-11-23 01:33:31 +02:00
|
|
|
}
|