webrtc: add whepBearerTokenParameter (#3796) (#5486)

this allows to pass Authorization: Bearer to servers that require it.
This commit is contained in:
Alessandro Ros
2026-02-19 11:57:39 +01:00
committed by GitHub
parent d492db6114
commit 87fbfebf06
6 changed files with 87 additions and 0 deletions
+2
View File
@@ -431,6 +431,8 @@ components:
type: string
# WHEP source
whepBearerToken:
type: string
whepSTUNGatherTimeout:
type: string
whepHandshakeTimeout:
+1
View File
@@ -229,6 +229,7 @@ type Path struct {
RTPUDPReadBufferSize *uint `json:"rtpUDPReadBufferSize,omitempty"` // deprecated
// WHEP source
WHEPBearerToken string `json:"whepBearerToken"`
WHEPSTUNGatherTimeout Duration `json:"whepSTUNGatherTimeout"`
WHEPHandshakeTimeout Duration `json:"whepHandshakeTimeout"`
WHEPTrackGatherTimeout Duration `json:"whepTrackGatherTimeout"`
+17
View File
@@ -24,6 +24,7 @@ type Client struct {
Publish bool
OutgoingTracks []*webrtc.OutgoingTrack
HTTPClient *http.Client
BearerToken string
UDPReadBufferSize uint
STUNGatherTimeout time.Duration
HandshakeTimeout time.Duration
@@ -197,6 +198,10 @@ func (c *Client) optionsICEServers(
return nil, err
}
if c.BearerToken != "" {
req.Header.Set("Authorization", "Bearer "+c.BearerToken)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
@@ -225,6 +230,10 @@ func (c *Client) postOffer(
return nil, err
}
if c.BearerToken != "" {
req.Header.Set("Authorization", "Bearer "+c.BearerToken)
}
req.Header.Set("Content-Type", "application/sdp")
res, err := c.HTTPClient.Do(req)
@@ -288,6 +297,10 @@ func (c *Client) patchCandidate(
return err
}
if c.BearerToken != "" {
req.Header.Set("Authorization", "Bearer "+c.BearerToken)
}
req.Header.Set("Content-Type", "application/trickle-ice-sdpfrag")
req.Header.Set("If-Match", etag)
@@ -312,6 +325,10 @@ func (c *Client) deleteSession(
return err
}
if c.BearerToken != "" {
req.Header.Set("Authorization", "Bearer "+c.BearerToken)
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return err
+64
View File
@@ -435,3 +435,67 @@ func TestClientPublish(t *testing.T) {
})
}
}
func TestClientBearerToken(t *testing.T) {
pc := &webrtc.PeerConnection{
LocalRandomUDP: true,
IPsFromInterfaces: true,
Log: test.NilLogger,
}
err := pc.Start()
require.NoError(t, err)
defer pc.Close()
httpServ := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "Bearer my_secret_token", r.Header.Get("Authorization"))
switch r.Method {
case http.MethodOptions:
w.WriteHeader(http.StatusNoContent)
case http.MethodPost:
body, err2 := io.ReadAll(r.Body)
require.NoError(t, err2)
offer := whipOffer(body)
answer, err2 := pc.CreateFullAnswer(offer)
require.NoError(t, err2)
w.Header().Set("Content-Type", "application/sdp")
w.Header().Set("ETag", "test_etag")
w.WriteHeader(http.StatusCreated)
w.Write([]byte(answer.SDP))
}
}),
}
ln, err := net.Listen("tcp", "localhost:9005")
require.NoError(t, err)
go httpServ.Serve(ln)
defer httpServ.Shutdown(context.Background())
u, err := url.Parse("http://localhost:9005/my/resource")
require.NoError(t, err)
outgoingTracks := []*webrtc.OutgoingTrack{{
Caps: pwebrtc.RTPCodecCapability{
MimeType: "audio/opus",
ClockRate: 48000,
Channels: 2,
},
}}
cl := &Client{
URL: u,
HTTPClient: &http.Client{},
BearerToken: "my_secret_token",
Log: test.NilLogger,
Publish: true,
OutgoingTracks: outgoingTracks,
}
err = cl.Initialize(context.Background())
require.NoError(t, err)
defer cl.Close() //nolint:errcheck
}
+1
View File
@@ -59,6 +59,7 @@ func (s *Source) Run(params defs.StaticSourceRunParams) error {
Timeout: time.Duration(s.ReadTimeout),
Transport: tr,
},
BearerToken: params.Conf.WHEPBearerToken,
UDPReadBufferSize: s.UDPReadBufferSize,
STUNGatherTimeout: time.Duration(params.Conf.WHEPSTUNGatherTimeout),
HandshakeTimeout: time.Duration(params.Conf.WHEPHandshakeTimeout),
+2
View File
@@ -573,6 +573,8 @@ pathDefaults:
###############################################
# Default path settings -> WebRTC / WHEP source (when source is WHEP)
# Token to insert in the Authorization: Bearer header.
whepBearerToken: ''
# Maximum time to gather STUN candidates.
whepSTUNGatherTimeout: 5s
# Time to wait for the WebRTC handshake to complete.