We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 96e4120 + 9780c7d commit a88e269Copy full SHA for a88e269
credential.go
@@ -1,25 +1,25 @@
1
package gitkit
2
3
import (
4
- "fmt"
5
"net/http"
6
)
7
8
type Credential struct {
9
- Username string
10
- Password string
+ Username string
+ Password string
+ Authorization string
11
}
12
13
-func getCredential(req *http.Request) (Credential, error) {
+func getCredential(req *http.Request) Credential {
14
cred := Credential{}
15
16
- user, pass, ok := req.BasicAuth()
17
- if !ok {
18
- return cred, fmt.Errorf("authentication failed")
19
- }
+ user, pass, _ := req.BasicAuth()
+
+ auth := req.Header.Get("Authorization")
20
21
cred.Username = user
22
cred.Password = pass
+ cred.Authorization = auth
23
24
- return cred, nil
+ return cred
25
credential_test.go
@@ -9,15 +9,20 @@ import (
func Test_getCredential(t *testing.T) {
req, _ := http.NewRequest("get", "http://localhost", nil)
- _, err := getCredential(req)
- assert.Error(t, err)
- assert.Equal(t, "authentication failed", err.Error())
+ cred := getCredential(req)
+ assert.Equal(t, cred.Authorization, "")
req, _ = http.NewRequest("get", "http://localhost", nil)
req.SetBasicAuth("Alladin", "OpenSesame")
- cred, err := getCredential(req)
+ cred = getCredential(req)
- assert.NoError(t, err)
assert.Equal(t, "Alladin", cred.Username)
assert.Equal(t, "OpenSesame", cred.Password)
+ assert.Contains(t, cred.Authorization, "Basic ")
+ req, _ = http.NewRequest("get", "http://localhost", nil)
+ req.Header.Add("Authorization", "Bearer VerySecretToken")
26
27
+ assert.Equal(t, "Bearer VerySecretToken", cred.Authorization)
28
http.go
@@ -88,20 +88,14 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
88
return
89
90
91
- authHeader := r.Header.Get("Authorization")
92
- if authHeader == "" {
+ cred := getCredential(r)
+ if cred.Authorization == "" {
93
+ logError("auth", fmt.Errorf("no Authorization header found"))
94
w.Header()["WWW-Authenticate"] = []string{`Basic realm=""`}
95
w.WriteHeader(http.StatusUnauthorized)
96
97
98
- cred, err := getCredential(r)
99
- if err != nil {
100
- logError("auth", err)
101
- w.WriteHeader(http.StatusUnauthorized)
102
- return
103
104
-
105
allow, err := s.AuthFunc(cred, req)
106
if !allow || err != nil {
107
if err != nil {
0 commit comments