Skip to content

Commit bc8b63a

Browse files
committed
feat: add support for bearer tokens in websocket protocols
1 parent 8551694 commit bc8b63a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

internal/request/http.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package request
55

66
import (
7+
"encoding/base64"
78
"fmt"
89
h "net/http"
910
"regexp"
@@ -148,8 +149,29 @@ func (h http) processBearerToken() (username string, groups []string, err error)
148149
return tr.Status.User.Username, tr.Status.User.Groups, nil
149150
}
150151

152+
// Get the JWT from headers
153+
// If there is no Authorizaion Bearer, then try finding the Bearer in Websocket Protocols header. This is for browser support.
151154
func (h http) bearerToken() string {
152-
return strings.ReplaceAll(h.Header.Get("Authorization"), "Bearer ", "")
155+
tradBearer := strings.ReplaceAll(h.Header.Get("Authorization"), "Bearer ", "")
156+
wsHeader := h.Header.Get("Sec-Websocket-Protocol")
157+
if tradBearer != "" {
158+
return tradBearer
159+
} else if wsHeader != "" {
160+
re := regexp.MustCompile(`(base64url\.bearer\.authorization\.k8s\.io\.)([^,]*)`)
161+
match := re.FindStringSubmatch(wsHeader)
162+
// our token is base64 encoded without padding
163+
b64decode, err := base64.RawStdEncoding.DecodeString(match[2])
164+
if err != nil {
165+
fmt.Println("failed to decode websocket auth bearer:", err)
166+
}
167+
if match[2] != "" {
168+
return string(b64decode)
169+
} else {
170+
return ""
171+
}
172+
} else {
173+
return ""
174+
}
153175
}
154176

155177
type authenticationFn func() (username string, groups []string, err error)

0 commit comments

Comments
 (0)