-
Notifications
You must be signed in to change notification settings - Fork 272
/
iceauth.go
52 lines (44 loc) · 1.12 KB
/
iceauth.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
package server
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"time"
)
func GetICEAuthServers(servers []ICEServer) (result []ICEAuthServer) {
for _, server := range servers {
result = append(result, newICEServer(server))
}
return
}
func newICEServer(server ICEServer) ICEAuthServer {
switch server.AuthType {
case AuthTypeSecret:
return getICEStaticAuthSecretCredentials(server)
case AuthTypeNone:
fallthrough
default:
return ICEAuthServer{
URLs: server.URLs,
Credential: "",
Username: "",
}
}
}
const oneHourSeconds = 24 * 3600
func getICEStaticAuthSecretCredentials(server ICEServer) ICEAuthServer {
timestamp := time.Now().Unix() + oneHourSeconds
username := fmt.Sprintf("%d:%s", timestamp, server.AuthSecret.Username)
h := hmac.New(sha1.New, []byte(server.AuthSecret.Secret))
if _, err := h.Write([]byte(username)); err != nil {
// Should never happen.
panic(fmt.Sprintf("write to hmac failed: %+v", err))
}
credential := base64.StdEncoding.EncodeToString(h.Sum(nil))
return ICEAuthServer{
URLs: server.URLs,
Username: username,
Credential: credential,
}
}