-
Notifications
You must be signed in to change notification settings - Fork 168
/
auth.go
97 lines (80 loc) · 2.4 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package server
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/golang/glog"
"github.com/livepeer/go-livepeer/monitor"
)
const LIVERPEER_TRANSCODE_CONFIG_HEADER = "Livepeer-Transcode-Configuration"
// Call a webhook URL, passing the request URL we received
// Based on the response, we can authenticate and confirm whether to accept an incoming stream
func authenticateStream(authURL *url.URL, incomingRequestURL string) (*authWebhookResponse, error) {
if authURL == nil {
return nil, nil
}
started := time.Now()
jsonValue, err := json.Marshal(map[string]string{"url": incomingRequestURL})
if err != nil {
return nil, err
}
resp, err := http.Post(authURL.String(), "application/json", bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
rbody, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("status=%d error=%s", resp.StatusCode, string(rbody))
}
if len(rbody) == 0 {
return nil, nil
}
var authResp authWebhookResponse
if err = json.Unmarshal(rbody, &authResp); err != nil {
return nil, err
}
if authResp.ManifestID == "" {
return nil, errors.New("empty manifest id not allowed")
}
took := time.Since(started)
glog.Infof("Stream authentication for authURL=%s url=%s dur=%s", authURL, incomingRequestURL, took)
if monitor.Enabled {
monitor.AuthWebhookFinished(took)
}
return &authResp, nil
}
func getTranscodeConfiguration(r *http.Request) (*authWebhookResponse, error) {
transcodeConfigurationHeader := r.Header.Get(LIVERPEER_TRANSCODE_CONFIG_HEADER)
if transcodeConfigurationHeader == "" {
return nil, nil
}
var transcodeConfiguration authWebhookResponse
err := json.Unmarshal([]byte(transcodeConfigurationHeader), &transcodeConfiguration)
return &transcodeConfiguration, err
}
// Compare two sets of profiles. Since there's no deep equality method in Go,
// we marshal to JSON and compare the resulting strings
func (a authWebhookResponse) areProfilesEqual(b authWebhookResponse) bool {
// Return quickly in simple cases without trying to marshal JSON
if len(a.Profiles) != len(b.Profiles) {
return false
}
if len(a.Profiles) == 0 {
return true
}
profilesA, err := json.Marshal(a.Profiles)
if err != nil {
return false
}
profilesB, err := json.Marshal(b.Profiles)
if err != nil {
return false
}
return string(profilesA) == string(profilesB)
}