forked from zitadel/zitadel-go
/
client.go
176 lines (154 loc) · 5.13 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package zitadel
import (
"crypto/x509"
"strings"
"github.com/zitadel/oidc/pkg/client/profile"
"golang.org/x/oauth2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/zitadel/zitadel-go/v2/pkg/client/middleware"
)
type Connection struct {
issuer string
api string
jwtProfileTokenSource middleware.JWTProfileTokenSource
scopes []string
orgID string
insecure bool
unaryInterceptors []grpc.UnaryClientInterceptor
streamInterceptors []grpc.StreamClientInterceptor
*grpc.ClientConn
}
func NewConnection(issuer, api string, scopes []string, options ...Option) (*Connection, error) {
c := &Connection{
issuer: issuer,
api: api,
jwtProfileTokenSource: middleware.JWTProfileFromPath(middleware.OSKeyPath()),
scopes: scopes,
}
for _, option := range options {
if err := option(c); err != nil {
return nil, err
}
}
err := c.setInterceptors(c.issuer, c.orgID, c.scopes, c.jwtProfileTokenSource)
if err != nil {
return nil, err
}
dialOptions := []grpc.DialOption{
grpc.WithChainUnaryInterceptor(
c.unaryInterceptors...,
),
grpc.WithChainStreamInterceptor(
c.streamInterceptors...,
),
}
opt, err := transportOption(c.api, c.insecure)
if err != nil {
return nil, err
}
dialOptions = append(dialOptions, opt)
if strings.Split(api, ":"); len(s) > 1 && s[1] != "443" {
dialOptions = append(dialOptions, grpc.WithAuthority(api))
}
conn, err := grpc.Dial(c.api, dialOptions...)
if err != nil {
return nil, err
}
c.ClientConn = conn
return c, nil
}
func (c *Connection) setInterceptors(issuer, orgID string, scopes []string, jwtProfileTokenSource middleware.JWTProfileTokenSource) error {
auth, err := middleware.NewAuthenticator(issuer, jwtProfileTokenSource, scopes...)
if err != nil {
return err
}
c.unaryInterceptors = append(c.unaryInterceptors, auth.Unary())
c.streamInterceptors = append(c.streamInterceptors, auth.Stream())
if orgID != "" {
org := middleware.NewOrgInterceptor(orgID)
c.unaryInterceptors = append(c.unaryInterceptors, org.Unary())
c.streamInterceptors = append(c.streamInterceptors, org.Stream())
}
return nil
}
func transportOption(api string, insecure bool) (grpc.DialOption, error) {
if insecure {
return grpc.WithInsecure(), nil
}
certs, err := transportCredentials(api)
if err != nil {
return nil, err
}
return grpc.WithTransportCredentials(certs), nil
}
func transportCredentials(api string) (credentials.TransportCredentials, error) {
ca, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
if ca == nil {
ca = x509.NewCertPool()
}
servernameWithoutPort := strings.Split(api, ":")[0]
return credentials.NewClientTLSFromCert(ca, servernameWithoutPort), nil
}
type Option func(*Connection) error
//WithCustomURL replaces the standard issuer (https://issuer.zitadel.ch) and api endpoint (api.zitadel.ch:443)
func WithCustomURL(issuer, api string) func(*Connection) error {
return func(client *Connection) error {
client.issuer = issuer
client.api = api
return nil
}
}
//WithKeyPath sets the path to the key.json used for the authentication
//if not set env var ZITADEL_KEY_PATH will be used
//
//Deprecated: use WithJWTProfileTokenSource(middleware.JWTProfileFromPath(keyPath)) instead
func WithKeyPath(keyPath string) func(*Connection) error {
return func(client *Connection) error {
client.jwtProfileTokenSource = func(issuer string, scopes []string) (oauth2.TokenSource, error) {
return profile.NewJWTProfileTokenSourceFromKeyFile(issuer, keyPath, scopes)
}
return nil
}
}
//WithJWTProfileTokenSource sets the provider used for the authentication
//if not set, the key file will be read from the path set in env var ZITADEL_KEY_PATH
func WithJWTProfileTokenSource(provider middleware.JWTProfileTokenSource) func(*Connection) error {
return func(client *Connection) error {
client.jwtProfileTokenSource = provider
return nil
}
}
//WithOrgID sets the organization context (where the api calls are executed)
//if not set the resource owner (organisation) of the calling user will be used
func WithOrgID(orgID string) func(*Connection) error {
return func(client *Connection) error {
client.orgID = orgID
return nil
}
}
//WithInsecure disables transport security for the client connection
//use only when absolutely necessary (local development)
func WithInsecure() func(*Connection) error {
return func(client *Connection) error {
client.insecure = true
return nil
}
}
//WithUnaryInterceptors adds non ZITADEL specific interceptors to the connection
func WithUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) func(*Connection) error {
return func(client *Connection) error {
client.unaryInterceptors = append(client.unaryInterceptors, interceptors...)
return nil
}
}
//WithStreamInterceptors adds non ZITADEL specific interceptors to the connection
func WithStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) func(*Connection) error {
return func(client *Connection) error {
client.streamInterceptors = append(client.streamInterceptors, interceptors...)
return nil
}
}