2019-11-23 05:03:31 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2019-11-23 05:03:31 +05:30
|
|
|
|
2021-06-09 23:23:16 +05:30
|
|
|
package auth
|
2019-11-23 05:03:31 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-05 18:35:40 +05:30
|
|
|
"net/http"
|
2021-05-15 21:02:09 +05:30
|
|
|
"regexp"
|
2019-11-23 05:03:31 +05:30
|
|
|
"strings"
|
|
|
|
|
2021-11-22 20:51:55 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-01-14 20:33:31 +05:30
|
|
|
"code.gitea.io/gitea/modules/auth/webauthn"
|
2019-11-23 05:03:31 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-12-20 19:42:26 +05:30
|
|
|
"code.gitea.io/gitea/modules/session"
|
2021-05-15 21:02:09 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-30 14:25:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2019-11-23 05:03:31 +05:30
|
|
|
)
|
|
|
|
|
2021-06-09 23:23:16 +05:30
|
|
|
// Init should be called exactly once when the application starts to allow plugins
|
2019-11-23 05:03:31 +05:30
|
|
|
// to allocate necessary resources
|
|
|
|
func Init() {
|
2022-01-14 20:33:31 +05:30
|
|
|
webauthn.Init()
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// isAttachmentDownload check if request is a file download (GET) with URL to an attachment
|
2021-01-05 18:35:40 +05:30
|
|
|
func isAttachmentDownload(req *http.Request) bool {
|
|
|
|
return strings.HasPrefix(req.URL.Path, "/attachments/") && req.Method == "GET"
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|
|
|
|
|
2022-03-30 14:12:47 +05:30
|
|
|
// isContainerPath checks if the request targets the container endpoint
|
|
|
|
func isContainerPath(req *http.Request) bool {
|
|
|
|
return strings.HasPrefix(req.URL.Path, "/v2/")
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
var (
|
|
|
|
gitRawReleasePathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/))`)
|
|
|
|
lfsPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`)
|
|
|
|
)
|
2021-05-15 21:02:09 +05:30
|
|
|
|
2021-09-02 21:18:48 +05:30
|
|
|
func isGitRawReleaseOrLFSPath(req *http.Request) bool {
|
|
|
|
if gitRawReleasePathRe.MatchString(req.URL.Path) {
|
2021-05-15 21:02:09 +05:30
|
|
|
return true
|
|
|
|
}
|
|
|
|
if setting.LFS.StartServer {
|
|
|
|
return lfsPathRe.MatchString(req.URL.Path)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-23 05:03:31 +05:30
|
|
|
// handleSignIn clears existing session variables and stores new ones for the specified user object
|
2021-11-24 15:19:20 +05:30
|
|
|
func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore, user *user_model.User) {
|
2021-12-20 19:42:26 +05:30
|
|
|
// 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 05:03:31 +05:30
|
|
|
_ = 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 20:33:31 +05:30
|
|
|
_ = sess.Delete("webauthnAssertion")
|
2019-11-23 05:03:31 +05:30
|
|
|
_ = sess.Delete("linkAccount")
|
2021-12-20 19:42:26 +05:30
|
|
|
err = sess.Set("uid", user.ID)
|
2019-11-23 05:03:31 +05:30
|
|
|
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 14:25:53 +05:30
|
|
|
lc := middleware.Locale(resp, req)
|
2021-01-05 18:35:40 +05:30
|
|
|
user.Language = lc.Language()
|
2021-11-24 15:19:20 +05:30
|
|
|
if err := user_model.UpdateUserCols(db.DefaultContext, user, "language"); err != nil {
|
2019-11-23 05:03:31 +05:30
|
|
|
log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", user.ID, user.Language))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 13:42:43 +05:30
|
|
|
middleware.SetLocaleCookie(resp, user.Language, 0)
|
2019-11-23 05:03:31 +05:30
|
|
|
|
|
|
|
// Clear whatever CSRF has right now, force to generate a new one
|
2021-03-07 13:42:43 +05:30
|
|
|
middleware.DeleteCSRFCookie(resp)
|
2019-11-23 05:03:31 +05:30
|
|
|
}
|