forked from jcelliott/turnpike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
51 lines (45 loc) · 1.11 KB
/
client.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
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
"time"
"github.com/awakenetworks/turnpike"
"github.com/howeyc/gopass"
)
var password []byte
func exampleAuthFunc(hello map[string]interface{}, c map[string]interface{}) (string, map[string]interface{}, error) {
challenge, ok := c["challenge"].(string)
if !ok {
log.Fatal("no challenge data recieved")
}
mac := hmac.New(sha256.New, password)
mac.Write([]byte(challenge))
signature := mac.Sum(nil)
return base64.StdEncoding.EncodeToString(signature), nil, nil
}
func main() {
turnpike.Debug()
fmt.Println("Hint: the password is 'password'")
fmt.Print("Password: ")
var err error
password, err = gopass.GetPasswd()
if err != nil {
log.Fatal(err)
}
c, err := turnpike.NewWebsocketClient(turnpike.JSON, "ws://localhost:8000/ws")
if err != nil {
log.Fatal(err)
}
c.Auth = map[string]turnpike.AuthFunc{"example-auth": exampleAuthFunc}
_, err = c.JoinRealm("turnpike.examples", nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected with auth")
time.Sleep(3 * time.Second)
fmt.Println("Disconnecting")
c.Close()
}