Skip to content

Commit b46cf4a

Browse files
committed
feat: add webhook signing
1 parent bc9f729 commit b46cf4a

File tree

6 files changed

+23
-12
lines changed

6 files changed

+23
-12
lines changed

cmd/livepeer/starter/flags.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func NewLivepeerConfig(fs *flag.FlagSet) LivepeerConfig {
6666
// Live AI:
6767
cfg.MediaMTXApiPassword = fs.String("mediaMTXApiPassword", "", "HTTP basic auth password for MediaMTX API requests")
6868
cfg.LiveAITrickleHostForRunner = fs.String("liveAITrickleHostForRunner", "", "Trickle Host used by AI Runner; It's used to overwrite the publicly available Trickle Host")
69-
cfg.LiveAIAuthApiKey = fs.String("liveAIAuthApiKey", "", "API key to use for Live AI authentication requests")
69+
cfg.LiveAIAuthSecret = fs.String("liveAIAuthSecret", "", "HMAC secret for Live AI auth signing")
7070
cfg.LiveAIHeartbeatURL = fs.String("liveAIHeartbeatURL", "", "Base URL for Live AI heartbeat requests")
7171
cfg.LiveAIHeartbeatHeaders = fs.String("liveAIHeartbeatHeaders", "", "Map of headers to use for Live AI heartbeat requests. e.g. 'header:val,header2:val2'")
7272
cfg.LiveAIHeartbeatInterval = fs.Duration("liveAIHeartbeatInterval", *cfg.LiveAIHeartbeatInterval, "Interval to send Live AI heartbeat requests")
@@ -173,4 +173,4 @@ func UpdateNilsForUnsetFlags(cfg LivepeerConfig) LivepeerConfig {
173173
}
174174

175175
return res
176-
}
176+
}

cmd/livepeer/starter/starter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ type LivepeerConfig struct {
176176
KafkaPassword *string
177177
KafkaGatewayTopic *string
178178
MediaMTXApiPassword *string
179-
LiveAIAuthApiKey *string
179+
LiveAIAuthSecret *string
180180
LiveAIHeartbeatURL *string
181181
LiveAIHeartbeatHeaders *string
182182
LiveAIHeartbeatInterval *time.Duration
@@ -1676,8 +1676,8 @@ func StartLivepeer(ctx context.Context, cfg LivepeerConfig) {
16761676
if cfg.MediaMTXApiPassword != nil {
16771677
n.MediaMTXApiPassword = *cfg.MediaMTXApiPassword
16781678
}
1679-
if cfg.LiveAIAuthApiKey != nil {
1680-
n.LiveAIAuthApiKey = *cfg.LiveAIAuthApiKey
1679+
if cfg.LiveAIAuthSecret != nil {
1680+
n.LiveAIAuthSecret = *cfg.LiveAIAuthSecret
16811681
}
16821682
if cfg.LiveAIHeartbeatURL != nil {
16831683
n.LiveAIHeartbeatURL = *cfg.LiveAIHeartbeatURL

core/livepeernode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ type LivepeerNode struct {
159159

160160
MediaMTXApiPassword string
161161
LiveAITrickleHostForRunner string
162-
LiveAIAuthApiKey string
162+
LiveAIAuthSecret string
163163
LiveAIHeartbeatURL string
164164
LiveAIHeartbeatHeaders map[string]string
165165
LiveAIHeartbeatInterval time.Duration

server/ai_mediaserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func (ls *LivepeerServer) StartLiveVideo() http.Handler {
513513
}
514514
}
515515
if authURL != nil {
516-
authResp, err := authenticateAIStream(authURL, ls.liveAIAuthApiKey, AIAuthRequest{
516+
authResp, err := authenticateAIStream(authURL, ls.liveAIAuthSecret, AIAuthRequest{
517517
Stream: streamName,
518518
Type: sourceTypeStr,
519519
QueryParams: queryParams,
@@ -992,7 +992,7 @@ func (ls *LivepeerServer) CreateWhip(server *media.WHIPServer) http.Handler {
992992
}
993993
}
994994
if authURL != nil {
995-
authResp, err := authenticateAIStream(authURL, ls.liveAIAuthApiKey, AIAuthRequest{
995+
authResp, err := authenticateAIStream(authURL, ls.liveAIAuthSecret, AIAuthRequest{
996996
Stream: streamName,
997997
Type: sourceTypeStr,
998998
QueryParams: queryParams,

server/auth.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import (
1010
"net/url"
1111
"time"
1212

13+
"crypto/hmac"
14+
"crypto/sha256"
15+
"encoding/hex"
16+
1317
"github.com/golang/glog"
1418
"github.com/livepeer/go-livepeer/monitor"
1519
)
@@ -133,7 +137,7 @@ type AIAuthResponse struct {
133137
paramsMap map[string]interface{} // unmarshaled params
134138
}
135139

136-
func authenticateAIStream(authURL *url.URL, apiKey string, req AIAuthRequest) (*AIAuthResponse, error) {
140+
func authenticateAIStream(authURL *url.URL, secret string, req AIAuthRequest) (*AIAuthResponse, error) {
137141
req.StreamKey = req.Stream
138142
if authURL == nil {
139143
return nil, fmt.Errorf("No auth URL configured")
@@ -151,7 +155,14 @@ func authenticateAIStream(authURL *url.URL, apiKey string, req AIAuthRequest) (*
151155
}
152156

153157
request.Header.Set("Content-Type", "application/json")
154-
request.Header.Set("x-api-key", apiKey)
158+
159+
ts := time.Now().UTC().Format(time.RFC3339Nano)
160+
m := hmac.New(sha256.New, []byte(secret))
161+
m.Write([]byte(ts))
162+
m.Write(jsonValue)
163+
sig := hex.EncodeToString(m.Sum(nil))
164+
request.Header.Set("x-timestamp", ts)
165+
request.Header.Set("x-signature", sig)
155166

156167
resp, err := http.DefaultClient.Do(request)
157168
if err != nil {

server/mediaserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type LivepeerServer struct {
127127
serverLock *sync.RWMutex
128128

129129
mediaMTXApiPassword string
130-
liveAIAuthApiKey string
130+
liveAIAuthSecret string
131131
livePaymentInterval time.Duration
132132
outSegmentTimeout time.Duration
133133
}
@@ -196,7 +196,7 @@ func NewLivepeerServer(ctx context.Context, rtmpAddr string, lpNode *core.Livepe
196196
recordingsAuthResponses: cache.New(time.Hour, 2*time.Hour),
197197
AISessionManager: NewAISessionManager(lpNode, AISessionManagerTTL),
198198
mediaMTXApiPassword: lpNode.MediaMTXApiPassword,
199-
liveAIAuthApiKey: lpNode.LiveAIAuthApiKey,
199+
liveAIAuthSecret: lpNode.LiveAIAuthSecret,
200200
livePaymentInterval: lpNode.LivePaymentInterval,
201201
outSegmentTimeout: lpNode.LiveOutSegmentTimeout,
202202
}

0 commit comments

Comments
 (0)