aboutsummaryrefslogtreecommitdiffstats
path: root/modules/auth/httpauth/httpauth.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2025-07-03 11:02:38 +0800
committerGitHub <noreply@github.com>2025-07-03 03:02:38 +0000
commitd6d643fe86f125ee7fdda82264602b7e8db2a36b (patch)
treeb2908dd68401fd62e2b1189c7540acf697d4e2e5 /modules/auth/httpauth/httpauth.go
parent8cbec63cc70f9ebbe1558123d95cbe63b2f31782 (diff)
downloadgitea-main.tar.gz
gitea-main.zip
Fix http auth header parsing (#34936)HEADmain
Using `strings.EqualFold` is wrong in many cases.
Diffstat (limited to 'modules/auth/httpauth/httpauth.go')
-rw-r--r--modules/auth/httpauth/httpauth.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/modules/auth/httpauth/httpauth.go b/modules/auth/httpauth/httpauth.go
new file mode 100644
index 0000000000..7f1f1ee152
--- /dev/null
+++ b/modules/auth/httpauth/httpauth.go
@@ -0,0 +1,47 @@
+// Copyright 2025 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package httpauth
+
+import (
+ "encoding/base64"
+ "strings"
+
+ "code.gitea.io/gitea/modules/util"
+)
+
+type BasicAuth struct {
+ Username, Password string
+}
+
+type BearerToken struct {
+ Token string
+}
+
+type ParsedAuthorizationHeader struct {
+ BasicAuth *BasicAuth
+ BearerToken *BearerToken
+}
+
+func ParseAuthorizationHeader(header string) (ret ParsedAuthorizationHeader, _ bool) {
+ parts := strings.Fields(header)
+ if len(parts) != 2 {
+ return ret, false
+ }
+ if util.AsciiEqualFold(parts[0], "basic") {
+ s, err := base64.StdEncoding.DecodeString(parts[1])
+ if err != nil {
+ return ret, false
+ }
+ u, p, ok := strings.Cut(string(s), ":")
+ if !ok {
+ return ret, false
+ }
+ ret.BasicAuth = &BasicAuth{Username: u, Password: p}
+ return ret, true
+ } else if util.AsciiEqualFold(parts[0], "token") || util.AsciiEqualFold(parts[0], "bearer") {
+ ret.BearerToken = &BearerToken{Token: parts[1]}
+ return ret, true
+ }
+ return ret, false
+}