2019-11-23 07:33:31 +08:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-06-10 01:53:16 +08:00
|
|
|
package auth
|
2019-11-23 07:33:31 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-05 21:05:40 +08:00
|
|
|
"net/http"
|
2019-11-23 07:33:31 +08:00
|
|
|
"reflect"
|
2021-05-15 23:32:09 +08:00
|
|
|
"regexp"
|
2019-11-23 07:33:31 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2021-05-15 23:32:09 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-01-30 16:55:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web/middleware"
|
2019-11-23 07:33:31 +08:00
|
|
|
)
|
|
|
|
|
2021-06-10 01:53:16 +08:00
|
|
|
// authMethods contains the list of authentication plugins in the order they are expected to be
|
2019-11-23 07:33:31 +08:00
|
|
|
// executed.
|
|
|
|
//
|
|
|
|
// The OAuth2 plugin is expected to be executed first, as it must ignore the user id stored
|
|
|
|
// in the session (if there is a user id stored in session other plugins might return the user
|
|
|
|
// object for that id).
|
|
|
|
//
|
|
|
|
// The Session plugin is expected to be executed second, in order to skip authentication
|
|
|
|
// for users that have already signed in.
|
2021-07-24 18:16:34 +08:00
|
|
|
var authMethods = []Method{
|
2019-11-23 07:33:31 +08:00
|
|
|
&OAuth2{},
|
2021-05-15 23:32:09 +08:00
|
|
|
&Basic{},
|
2019-11-23 07:33:31 +08:00
|
|
|
&Session{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// The purpose of the following three function variables is to let the linter know that
|
|
|
|
// those functions are not dead code and are actually being used
|
|
|
|
var (
|
|
|
|
_ = handleSignIn
|
|
|
|
)
|
|
|
|
|
2021-06-10 01:53:16 +08:00
|
|
|
// Methods returns the instances of all registered methods
|
2021-07-24 18:16:34 +08:00
|
|
|
func Methods() []Method {
|
2021-06-10 01:53:16 +08:00
|
|
|
return authMethods
|
2019-11-23 07:33:31 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 01:53:16 +08:00
|
|
|
// Register adds the specified instance to the list of available methods
|
2021-07-24 18:16:34 +08:00
|
|
|
func Register(method Method) {
|
2021-06-10 01:53:16 +08:00
|
|
|
authMethods = append(authMethods, method)
|
2019-11-23 07:33:31 +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 07:33:31 +08:00
|
|
|
// to allocate necessary resources
|
|
|
|
func Init() {
|
2021-06-10 01:53:16 +08:00
|
|
|
if setting.Service.EnableReverseProxyAuth {
|
|
|
|
Register(&ReverseProxy{})
|
|
|
|
}
|
|
|
|
specialInit()
|
2019-11-23 07:33:31 +08:00
|
|
|
for _, method := range Methods() {
|
2021-07-24 18:16:34 +08:00
|
|
|
initializable, ok := method.(Initializable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := initializable.Init()
|
2019-11-23 07:33:31 +08:00
|
|
|
if err != nil {
|
2021-06-10 01:53:16 +08:00
|
|
|
log.Error("Could not initialize '%s' auth method, error: %s", reflect.TypeOf(method).String(), err)
|
2019-11-23 07:33:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 01:53:16 +08:00
|
|
|
// Free should be called exactly once when the application is terminating to allow Auth plugins
|
2019-11-23 07:33:31 +08:00
|
|
|
// to release necessary resources
|
|
|
|
func Free() {
|
|
|
|
for _, method := range Methods() {
|
2021-07-24 18:16:34 +08:00
|
|
|
freeable, ok := method.(Freeable)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err := freeable.Free()
|
2019-11-23 07:33:31 +08:00
|
|
|
if err != nil {
|
2021-06-10 01:53:16 +08:00
|
|
|
log.Error("Could not free '%s' auth method, error: %s", reflect.TypeOf(method).String(), err)
|
2019-11-23 07:33:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isAttachmentDownload check if request is a file download (GET) with URL to an attachment
|
2021-01-05 21:05:40 +08:00
|
|
|
func isAttachmentDownload(req *http.Request) bool {
|
|
|
|
return strings.HasPrefix(req.URL.Path, "/attachments/") && req.Method == "GET"
|
2019-11-23 07:33:31 +08:00
|
|
|
}
|
|
|
|
|
2021-05-28 02:46:11 +08:00
|
|
|
var gitRawPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|raw/)`)
|
2021-05-15 23:32:09 +08:00
|
|
|
var lfsPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`)
|
|
|
|
|
2021-05-28 02:46:11 +08:00
|
|
|
func isGitRawOrLFSPath(req *http.Request) bool {
|
|
|
|
if gitRawPathRe.MatchString(req.URL.Path) {
|
2021-05-15 23:32:09 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if setting.LFS.StartServer {
|
|
|
|
return lfsPathRe.MatchString(req.URL.Path)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-23 07:33:31 +08:00
|
|
|
// handleSignIn clears existing session variables and stores new ones for the specified user object
|
2021-01-05 21:05:40 +08:00
|
|
|
func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore, user *models.User) {
|
2019-11-23 07:33:31 +08: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")
|
|
|
|
_ = sess.Delete("u2fChallenge")
|
|
|
|
_ = sess.Delete("linkAccount")
|
|
|
|
err := sess.Set("uid", user.ID)
|
|
|
|
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)
|
2021-01-05 21:05:40 +08:00
|
|
|
user.Language = lc.Language()
|
2019-11-23 07:33:31 +08:00
|
|
|
if err := models.UpdateUserCols(user, "language"); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", user.ID, user.Language))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-07 16:12:43 +08:00
|
|
|
middleware.SetLocaleCookie(resp, user.Language, 0)
|
2019-11-23 07:33:31 +08:00
|
|
|
|
|
|
|
// Clear whatever CSRF has right now, force to generate a new one
|
2021-03-07 16:12:43 +08:00
|
|
|
middleware.DeleteCSRFCookie(resp)
|
2019-11-23 07:33:31 +08:00
|
|
|
}
|