mirror of
https://github.com/jech/galene.git
synced 2024-11-10 02:35:58 +01:00
Add test for parsing bearer tokens.
This commit is contained in:
parent
f9ef43248b
commit
5c2e5ee5c0
2 changed files with 33 additions and 4 deletions
|
@ -61,6 +61,35 @@ func TestParseWhip(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseBearerToken(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) {
|
func TestFormatICEServer(t *testing.T) {
|
||||||
a := []struct {
|
a := []struct {
|
||||||
s webrtc.ICEServer
|
s webrtc.ICEServer
|
||||||
|
|
|
@ -52,8 +52,7 @@ func canPresent(perms []string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBearerToken(r *http.Request) string {
|
func parseBearerToken(auth string) string {
|
||||||
auth := r.Header.Get("Authorization")
|
|
||||||
auths := strings.Split(auth, ",")
|
auths := strings.Split(auth, ",")
|
||||||
for _, a := range auths {
|
for _, a := range auths {
|
||||||
a = strings.Trim(a, " \t")
|
a = strings.Trim(a, " \t")
|
||||||
|
@ -178,7 +177,8 @@ func whipEndpointHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token := getBearerToken(r)
|
token := parseBearerToken(r.Header.Get("Authorization"))
|
||||||
|
|
||||||
whip := "whip"
|
whip := "whip"
|
||||||
creds := group.ClientCredentials{
|
creds := group.ClientCredentials{
|
||||||
Username: &whip,
|
Username: &whip,
|
||||||
|
@ -258,7 +258,7 @@ func whipResourceHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if t := c.Token(); t != "" {
|
if t := c.Token(); t != "" {
|
||||||
token := getBearerToken(r)
|
token := parseBearerToken(r.Header.Get("Authorization"))
|
||||||
if token != t {
|
if token != t {
|
||||||
http.Error(w, "Forbidden", http.StatusForbidden)
|
http.Error(w, "Forbidden", http.StatusForbidden)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in a new issue