You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.5 KiB
131 lines
3.5 KiB
5 years ago
|
// 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.
|
||
|
|
||
4 years ago
|
package auth
|
||
5 years ago
|
|
||
|
import (
|
||
4 years ago
|
"net/http"
|
||
5 years ago
|
"strings"
|
||
|
|
||
|
"code.gitea.io/gitea/models"
|
||
|
"code.gitea.io/gitea/modules/base"
|
||
|
"code.gitea.io/gitea/modules/log"
|
||
|
"code.gitea.io/gitea/modules/setting"
|
||
|
"code.gitea.io/gitea/modules/timeutil"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/web/middleware"
|
||
5 years ago
|
)
|
||
|
|
||
|
// Ensure the struct implements the interface.
|
||
|
var (
|
||
4 years ago
|
_ Auth = &Basic{}
|
||
5 years ago
|
)
|
||
|
|
||
4 years ago
|
// Basic implements the Auth interface and authenticates requests (API requests
|
||
5 years ago
|
// only) by looking for Basic authentication data or "x-oauth-basic" token in the "Authorization"
|
||
|
// header.
|
||
|
type Basic struct {
|
||
|
}
|
||
|
|
||
4 years ago
|
// Name represents the name of auth method
|
||
|
func (b *Basic) Name() string {
|
||
|
return "basic"
|
||
|
}
|
||
|
|
||
5 years ago
|
// Init does nothing as the Basic implementation does not need to allocate any resources
|
||
|
func (b *Basic) Init() error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Free does nothing as the Basic implementation does not have to release any resources
|
||
|
func (b *Basic) Free() error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
// Verify extracts and validates Basic data (username and password/token) from the
|
||
5 years ago
|
// "Authorization" header of the request and returns the corresponding user object for that
|
||
|
// name/token on successful validation.
|
||
|
// Returns nil if header is empty or validation fails.
|
||
4 years ago
|
func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) *models.User {
|
||
4 years ago
|
// Basic authentication should only fire on API, Download or on Git or LFSPaths
|
||
4 years ago
|
if !middleware.IsAPIPath(req) && !isAttachmentDownload(req) && !isGitRawOrLFSPath(req) {
|
||
4 years ago
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
baHead := req.Header.Get("Authorization")
|
||
5 years ago
|
if len(baHead) == 0 {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
4 years ago
|
auths := strings.SplitN(baHead, " ", 2)
|
||
4 years ago
|
if len(auths) != 2 || (auths[0] != "Basic" && auths[0] != "basic") {
|
||
5 years ago
|
return nil
|
||
|
}
|
||
|
|
||
|
uname, passwd, _ := base.BasicAuthDecode(auths[1])
|
||
|
|
||
|
// Check if username or password is a token
|
||
|
isUsernameToken := len(passwd) == 0 || passwd == "x-oauth-basic"
|
||
|
// Assume username is token
|
||
|
authToken := uname
|
||
|
if !isUsernameToken {
|
||
4 years ago
|
log.Trace("Basic Authorization: Attempting login for: %s", uname)
|
||
5 years ago
|
// Assume password is token
|
||
|
authToken = passwd
|
||
4 years ago
|
} else {
|
||
|
log.Trace("Basic Authorization: Attempting login with username as token")
|
||
5 years ago
|
}
|
||
|
|
||
|
uid := CheckOAuthAccessToken(authToken)
|
||
|
if uid != 0 {
|
||
4 years ago
|
log.Trace("Basic Authorization: Valid OAuthAccessToken for user[%d]", uid)
|
||
5 years ago
|
|
||
4 years ago
|
u, err := models.GetUserByID(uid)
|
||
5 years ago
|
if err != nil {
|
||
|
log.Error("GetUserByID: %v", err)
|
||
|
return nil
|
||
|
}
|
||
4 years ago
|
|
||
|
store.GetData()["IsApiToken"] = true
|
||
|
return u
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
5 years ago
|
token, err := models.GetAccessTokenBySHA(authToken)
|
||
|
if err == nil {
|
||
4 years ago
|
log.Trace("Basic Authorization: Valid AccessToken for user[%d]", uid)
|
||
4 years ago
|
u, err := models.GetUserByID(token.UID)
|
||
5 years ago
|
if err != nil {
|
||
|
log.Error("GetUserByID: %v", err)
|
||
|
return nil
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
token.UpdatedUnix = timeutil.TimeStampNow()
|
||
|
if err = models.UpdateAccessToken(token); err != nil {
|
||
|
log.Error("UpdateAccessToken: %v", err)
|
||
|
}
|
||
4 years ago
|
|
||
|
store.GetData()["IsApiToken"] = true
|
||
|
return u
|
||
5 years ago
|
} else if !models.IsErrAccessTokenNotExist(err) && !models.IsErrAccessTokenEmpty(err) {
|
||
|
log.Error("GetAccessTokenBySha: %v", err)
|
||
|
}
|
||
|
|
||
4 years ago
|
if !setting.Service.EnableBasicAuth {
|
||
|
return nil
|
||
|
}
|
||
4 years ago
|
|
||
4 years ago
|
log.Trace("Basic Authorization: Attempting SignIn for %s", uname)
|
||
|
u, err := models.UserSignIn(uname, passwd)
|
||
|
if err != nil {
|
||
|
if !models.IsErrUserNotExist(err) {
|
||
|
log.Error("UserSignIn: %v", err)
|
||
5 years ago
|
}
|
||
4 years ago
|
return nil
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
log.Trace("Basic Authorization: Logged in user %-v", u)
|
||
|
|
||
5 years ago
|
return u
|
||
|
}
|