Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit e140b59

Browse files
authored
feat: Add authentication to TURN (#331)
1 parent fa33bde commit e140b59

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

internal/cmd/tunnel.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,18 @@ type tunnneler struct {
104104
}
105105

106106
func (c *tunnneler) start(ctx context.Context) error {
107+
username, password, err := wsnet.TURNCredentials(c.token)
108+
if err != nil {
109+
return xerrors.Errorf("failed to parse credentials from token")
110+
}
107111
server := webrtc.ICEServer{
108112
URLs: []string{wsnet.TURNEndpoint(c.brokerAddr)},
109-
Username: "insecure",
110-
Credential: "pass",
113+
Username: username,
114+
Credential: password,
111115
CredentialType: webrtc.ICECredentialTypePassword,
112116
}
113117

114-
err := wsnet.DialICE(server, nil)
118+
err = wsnet.DialICE(server, nil)
115119
if errors.Is(err, wsnet.ErrInvalidCredentials) {
116120
return xerrors.Errorf("failed to authenticate your user for this workspace")
117121
}

wsnet/auth.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package wsnet
2+
3+
import (
4+
"crypto/sha256"
5+
"errors"
6+
"strings"
7+
)
8+
9+
// TURNCredentials returns a username and password pair
10+
// for a Coder token.
11+
func TURNCredentials(token string) (username, password string, err error) {
12+
str := strings.SplitN(token, "-", 2)
13+
if len(str) != 2 {
14+
err = errors.New("invalid token format")
15+
return
16+
}
17+
username = str[0]
18+
hash := sha256.Sum256([]byte(str[1]))
19+
password = string(hash[:])
20+
return
21+
}

0 commit comments

Comments
 (0)