|
| 1 | +// Copyright 2024 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package auth |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + auth_model "code.gitea.io/gitea/models/auth" |
| 11 | + user_model "code.gitea.io/gitea/models/user" |
| 12 | + "code.gitea.io/gitea/modules/base" |
| 13 | + "code.gitea.io/gitea/modules/log" |
| 14 | + "code.gitea.io/gitea/modules/timeutil" |
| 15 | + "code.gitea.io/gitea/modules/web/middleware" |
| 16 | +) |
| 17 | + |
| 18 | +// Ensure the struct implements the interface. |
| 19 | +var ( |
| 20 | + _ Method = &AccessToken{} |
| 21 | +) |
| 22 | + |
| 23 | +// BasicMethodName is the constant name of the basic authentication method |
| 24 | +const ( |
| 25 | + AccessTokenMethodName = "access_token" |
| 26 | +) |
| 27 | + |
| 28 | +// AccessToken implements the Auth interface and authenticates requests (API requests |
| 29 | +// only) by looking for access token |
| 30 | +type AccessToken struct{} |
| 31 | + |
| 32 | +// Name represents the name of auth method |
| 33 | +func (b *AccessToken) Name() string { |
| 34 | + return AccessTokenMethodName |
| 35 | +} |
| 36 | + |
| 37 | +// Match returns true if the request matched AccessToken requirements |
| 38 | +// TODO: remove path check once AccessToken will not be a global middleware but only |
| 39 | +// for specific routes |
| 40 | +func (b *AccessToken) Match(req *http.Request) bool { |
| 41 | + if !middleware.IsAPIPath(req) && !isContainerPath(req) && !isAttachmentDownload(req) && !isGitRawOrAttachOrLFSPath(req) { |
| 42 | + return false |
| 43 | + } |
| 44 | + baHead := req.Header.Get("Authorization") |
| 45 | + if baHead == "" { |
| 46 | + return false |
| 47 | + } |
| 48 | + auths := strings.SplitN(baHead, " ", 2) |
| 49 | + if len(auths) != 2 || (strings.ToLower(auths[0]) != "basic") { |
| 50 | + return false |
| 51 | + } |
| 52 | + return true |
| 53 | +} |
| 54 | + |
| 55 | +// Verify extracts and validates Basic data (username and password/token) from the |
| 56 | +// "Authorization" header of the request and returns the corresponding user object for that |
| 57 | +// name/token on successful validation. |
| 58 | +// Returns nil if header is empty or validation fails. |
| 59 | +func (b *AccessToken) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) { |
| 60 | + // Basic authentication should only fire on API, Download or on Git or LFSPaths |
| 61 | + if !middleware.IsAPIPath(req) && !isContainerPath(req) && !isAttachmentDownload(req) && !isGitRawOrAttachOrLFSPath(req) { |
| 62 | + return nil, nil |
| 63 | + } |
| 64 | + |
| 65 | + baHead := req.Header.Get("Authorization") |
| 66 | + if len(baHead) == 0 { |
| 67 | + return nil, nil |
| 68 | + } |
| 69 | + |
| 70 | + auths := strings.SplitN(baHead, " ", 2) |
| 71 | + if len(auths) != 2 || (strings.ToLower(auths[0]) != "basic") { |
| 72 | + return nil, nil |
| 73 | + } |
| 74 | + |
| 75 | + uname, passwd, _ := base.BasicAuthDecode(auths[1]) |
| 76 | + |
| 77 | + // Check if username or password is a token |
| 78 | + isUsernameToken := len(passwd) == 0 || passwd == "x-oauth-basic" |
| 79 | + // Assume username is token |
| 80 | + authToken := uname |
| 81 | + if !isUsernameToken { |
| 82 | + log.Trace("Basic Authorization: Attempting login for: %s", uname) |
| 83 | + // Assume password is token |
| 84 | + authToken = passwd |
| 85 | + } else { |
| 86 | + log.Trace("Basic Authorization: Attempting login with username as token") |
| 87 | + } |
| 88 | + |
| 89 | + // check personal access token |
| 90 | + token, err := auth_model.GetAccessTokenBySHA(req.Context(), authToken) |
| 91 | + if err == nil { |
| 92 | + log.Trace("Basic Authorization: Valid AccessToken for user[%d]", token.UID) |
| 93 | + u, err := user_model.GetUserByID(req.Context(), token.UID) |
| 94 | + if err != nil { |
| 95 | + log.Error("GetUserByID: %v", err) |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + token.UpdatedUnix = timeutil.TimeStampNow() |
| 100 | + if err = auth_model.UpdateAccessToken(req.Context(), token); err != nil { |
| 101 | + log.Error("UpdateAccessToken: %v", err) |
| 102 | + } |
| 103 | + |
| 104 | + store.GetData()["LoginMethod"] = AccessTokenMethodName |
| 105 | + store.GetData()["IsApiToken"] = true |
| 106 | + store.GetData()["ApiTokenScope"] = token.Scope |
| 107 | + return u, nil |
| 108 | + } else if !auth_model.IsErrAccessTokenNotExist(err) && !auth_model.IsErrAccessTokenEmpty(err) { |
| 109 | + log.Error("GetAccessTokenBySha: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + return nil, nil |
| 113 | +} |
0 commit comments