Skip to content

Commit f4b2b8a

Browse files
committed
add support for bearer token auth
Signed-off-by: Sanskar Jaiswal <[email protected]>
1 parent 7c42de9 commit f4b2b8a

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

credential.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,29 @@ package gitkit
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67
)
78

89
type Credential struct {
9-
Username string
10-
Password string
10+
Username string
11+
Password string
12+
BearerToken string
1113
}
1214

1315
func getCredential(req *http.Request) (Credential, error) {
1416
cred := Credential{}
1517

1618
user, pass, ok := req.BasicAuth()
17-
if !ok {
19+
20+
auth := req.Header.Get("Authorization")
21+
if auth == "" && !ok {
1822
return cred, fmt.Errorf("authentication failed")
1923
}
24+
bearerToken := strings.TrimLeft(auth, "Bearer ")
2025

2126
cred.Username = user
2227
cred.Password = pass
28+
cred.BearerToken = bearerToken
2329

2430
return cred, nil
2531
}

credential_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,11 @@ func Test_getCredential(t *testing.T) {
2020
assert.NoError(t, err)
2121
assert.Equal(t, "Alladin", cred.Username)
2222
assert.Equal(t, "OpenSesame", cred.Password)
23+
24+
req, _ = http.NewRequest("get", "http://localhost", nil)
25+
req.Header.Add("Authorization", "Bearer VerySecretToken")
26+
cred, err = getCredential(req)
27+
28+
assert.NoError(t, err)
29+
assert.Equal(t, "VerySecretToken", cred.BearerToken)
2330
}

0 commit comments

Comments
 (0)