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

feat: Add authentication to TURN #331

Merged
merged 1 commit into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions internal/cmd/tunnel.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,18 @@ type tunnneler struct {
}

func (c *tunnneler) start(ctx context.Context) error {
username, password, err := wsnet.TURNCredentials(c.token)
if err != nil {
return xerrors.Errorf("failed to parse credentials from token")
}
server := webrtc.ICEServer{
URLs: []string{wsnet.TURNEndpoint(c.brokerAddr)},
Username: "insecure",
Credential: "pass",
Username: username,
Credential: password,
CredentialType: webrtc.ICECredentialTypePassword,
}

err := wsnet.DialICE(server, nil)
err = wsnet.DialICE(server, nil)
if errors.Is(err, wsnet.ErrInvalidCredentials) {
return xerrors.Errorf("failed to authenticate your user for this workspace")
}
Expand Down
21 changes: 21 additions & 0 deletions wsnet/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package wsnet

import (
"crypto/sha256"
"errors"
"strings"
)

// TURNCredentials returns a username and password pair
// for a Coder token.
func TURNCredentials(token string) (username, password string, err error) {
str := strings.SplitN(token, "-", 2)
if len(str) != 2 {
err = errors.New("invalid token format")
return
}
username = str[0]
hash := sha256.Sum256([]byte(str[1]))
password = string(hash[:])
return
}