This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
157 lines (114 loc) · 3.01 KB
/
auth_test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package connect
import (
"fmt"
"net"
"net/url"
"os"
"testing"
"time"
"github.com/nats-io/nkeys"
)
var tokenExchangeURLs = []string{
"http://api-server:8080/api",
"http://localhost:8080/api",
}
func TestBasicTokenClient(t *testing.T) {
var c TokenClient
keys, err := nkeys.CreateUser()
if err != nil {
t.Fatal(err)
}
c = NewBasicTokenClient("tokeny_mc_tokenface", keys)
var token string
token, err = c.GetJWT()
if err != nil {
t.Error(err)
}
if token != "tokeny_mc_tokenface" {
t.Error("token mismatch")
}
data := []byte{1, 156, 230, 4, 23, 175, 11}
signed, err := c.Sign(data)
if err != nil {
t.Fatal(err)
}
err = keys.Verify(data, signed)
if err != nil {
t.Error(err)
}
}
func GetTestOAuthTokenClient(t *testing.T) *OAuthTokenClient {
var domain string
var clientID string
var clientSecret string
var exists bool
errorFormat := "environment variable %v not found. Set up your test environment first. See: https://github.com/overmindtech/auth0-test-data"
// Read secrets form the environment
if domain, exists = os.LookupEnv("OVERMIND_NTE_ALLPERMS_DOMAIN"); !exists || domain == "" {
t.Errorf(errorFormat, "OVERMIND_NTE_ALLPERMS_DOMAIN")
t.Skip("Skipping due to missing environment setup")
}
if clientID, exists = os.LookupEnv("OVERMIND_NTE_ALLPERMS_CLIENT_ID"); !exists || clientID == "" {
t.Errorf(errorFormat, "OVERMIND_NTE_ALLPERMS_CLIENT_ID")
t.Skip("Skipping due to missing environment setup")
}
if clientSecret, exists = os.LookupEnv("OVERMIND_NTE_ALLPERMS_CLIENT_SECRET"); !exists || clientSecret == "" {
t.Errorf(errorFormat, "OVERMIND_NTE_ALLPERMS_CLIENT_SECRET")
t.Skip("Skipping due to missing environment setup")
}
exchangeURL, err := GetWorkingTokenExchange()
if err != nil {
t.Fatal(err)
}
flowConfig := ClientCredentialsConfig{
ClientID: clientID,
ClientSecret: clientSecret,
Account: "org_hdeUXbB55sMMvJLa",
}
return NewOAuthTokenClient(
fmt.Sprintf("https://%v/oauth/token", domain),
exchangeURL,
flowConfig,
)
}
func TestOAuthTokenClient(t *testing.T) {
c := GetTestOAuthTokenClient(t)
var err error
_, err = c.GetJWT()
if err != nil {
t.Error(err)
}
// Make sure it can sign
data := []byte{1, 156, 230, 4, 23, 175, 11}
_, err = c.Sign(data)
if err != nil {
t.Fatal(err)
}
}
func GetWorkingTokenExchange() (string, error) {
var err error
errMap := make(map[string]error)
for _, url := range tokenExchangeURLs {
if err = testURL(url); err == nil {
return url, nil
}
errMap[url] = err
}
var errString string
for url, err := range errMap {
errString = errString + fmt.Sprintf(" %v: %v\n", url, err.Error())
}
return "", fmt.Errorf("no working token exchanges found:\n%v", err)
}
func testURL(testURL string) error {
url, err := url.Parse(testURL)
if err != nil {
return fmt.Errorf("could not parse NATS URL: %v. Error: %v", testURL, err)
}
conn, err := net.DialTimeout("tcp", net.JoinHostPort(url.Hostname(), url.Port()), time.Second)
if err == nil {
conn.Close()
return nil
}
return err
}