-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathoidc_grpc_test.go
More file actions
136 lines (110 loc) · 3.7 KB
/
oidc_grpc_test.go
File metadata and controls
136 lines (110 loc) · 3.7 KB
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
// Copyright AGNTCY Contributors (https://github.com/agntcy)
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_serverNameFromAddr(t *testing.T) {
t.Parallel()
assert.Equal(t, "dev.gateway.ads.outshift.io", serverNameFromAddr("dev.gateway.ads.outshift.io:443"))
assert.Equal(t, "localhost", serverNameFromAddr("localhost:9999"))
assert.Equal(t, "badaddr", serverNameFromAddr("badaddr"))
}
func TestWithAuth_OIDC_WithOIDCToken(t *testing.T) {
t.Parallel()
opts := &options{
config: &Config{
ServerAddress: "gateway.example.com:443",
AuthMode: "oidc",
OIDCToken: "test-access-token",
},
}
ctx := context.Background()
opt := withAuth(ctx)
err := opt(opts)
require.NoError(t, err)
assert.NotEmpty(t, opts.authOpts)
assert.Nil(t, opts.authClient)
}
func TestOIDCBearerCredentials_GetRequestMetadata(t *testing.T) {
t.Parallel()
c := newOIDCBearerCredentials("mytoken")
md, err := c.GetRequestMetadata(context.Background())
require.NoError(t, err)
assert.Equal(t, "Bearer mytoken", md["authorization"])
assert.True(t, c.RequireTransportSecurity())
}
func TestSetupOIDCAuth_WithMachineCredentials_MintsAndCachesToken(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
mux := http.NewServeMux()
srv := httptest.NewServer(mux)
defer srv.Close()
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(map[string]string{
"token_endpoint": srv.URL + "/oauth/v2/token",
})
})
mux.HandleFunc("/oauth/v2/token", func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, testMaxFormBodyBytes)
if err := r.ParseForm(); err != nil {
t.Errorf("parse form: %v", err)
http.Error(w, "bad form", http.StatusBadRequest)
return
}
assert.Equal(t, "client_credentials", r.Form.Get("grant_type"))
assert.Equal(t, "machine-client", r.Form.Get("client_id"))
assert.Equal(t, "machine-secret", r.Form.Get("client_secret"))
assert.Equal(t, "openid profile", r.Form.Get("scope"))
_ = json.NewEncoder(w).Encode(map[string]any{
"access_token": makeTestJWT("machine-sub", srv.URL),
"token_type": "Bearer",
"expires_in": 3600,
})
})
opts := &options{
config: &Config{
ServerAddress: "gateway.example.com:443",
AuthMode: "oidc",
OIDCIssuer: srv.URL,
OIDCMachineClientID: "machine-client",
OIDCMachineClientSecret: "machine-secret",
OIDCMachineScopes: []string{"openid", "profile"},
OIDCMachineTokenEndpoint: "",
},
}
err := opts.setupOIDCAuth(context.Background())
require.NoError(t, err)
assert.NotEmpty(t, opts.authOpts)
cache := NewTokenCache()
tok, err := cache.GetValidToken()
require.NoError(t, err)
require.NotNil(t, tok)
assert.Equal(t, "oidc", tok.Provider)
assert.Equal(t, "machine-client", tok.User)
assert.Equal(t, "machine-sub", tok.UserID)
}
func TestSetupOIDCAuth_NoTokenAndNoMachineConfig(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
opts := &options{
config: &Config{
ServerAddress: "gateway.example.com:443",
AuthMode: "oidc",
},
}
err := opts.setupOIDCAuth(context.Background())
require.Error(t, err)
assert.Contains(t, err.Error(), "dirctl auth machine")
}
func makeTestJWT(sub, iss string) string {
header := base64.RawURLEncoding.EncodeToString([]byte(`{"alg":"none","typ":"JWT"}`))
payload := base64.RawURLEncoding.EncodeToString([]byte(`{"sub":"` + sub + `","iss":"` + iss + `"}`))
return strings.Join([]string{header, payload, "sig"}, ".")
}