Skip to content

Commit

Permalink
Add test for parsing bearer tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
jech committed Dec 9, 2023
1 parent f9ef432 commit 488b962
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
29 changes: 29 additions & 0 deletions webserver/webserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@ func TestParseWhip(t *testing.T) {
}
}

func TestGetBearerToken(t *testing.T) {
a := []struct{ a, b string }{
{"", ""},
{"foo", ""},
{"foo bar", ""},
{" foo bar", ""},
{"foo bar ", ""},
{"Bearer", ""},
{"Bearer ", ""},
{"Bearer foo", "foo"},
{"bearer foo", "foo"},
{" Bearer foo", "foo"},
{"Bearer foo ", "foo"},
{" Bearer foo ", "foo"},
{"Bearer foo bar", ""},
}

for _, ab := range a {
t.Run(ab.a, func(t *testing.T) {
b := parseBearerToken(ab.a)
if b != ab.b {
t.Errorf("Bearer token %v, got %v, expected %v",
ab.a, b, ab.b,
)
}
})
}
}

func TestFormatICEServer(t *testing.T) {
a := []struct {
s webrtc.ICEServer
Expand Down
8 changes: 4 additions & 4 deletions webserver/whip.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ func canPresent(perms []string) bool {
return false
}

func getBearerToken(r *http.Request) string {
auth := r.Header.Get("Authorization")
func parseBearerToken(auth string) string {
auths := strings.Split(auth, ",")
for _, a := range auths {
a = strings.Trim(a, " \t")
Expand Down Expand Up @@ -178,7 +177,8 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
return
}

token := getBearerToken(r)
token := parseBearerToken(r.Header.Get("Authorization"))

whip := "whip"
creds := group.ClientCredentials{
Username: &whip,
Expand Down Expand Up @@ -258,7 +258,7 @@ func whipResourceHandler(w http.ResponseWriter, r *http.Request) {
}

if t := c.Token(); t != "" {
token := getBearerToken(r)
token := parseBearerToken(r.Header.Get("Authorization"))
if token != t {
http.Error(w, "Forbidden", http.StatusForbidden)
return
Expand Down

0 comments on commit 488b962

Please sign in to comment.