/
client.go
582 lines (507 loc) · 14.6 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// Package iam provides support for interacting with HSDP IAM and IDM services
package iam
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/philips-software/go-hsdp-api/internal"
"github.com/go-playground/validator/v10"
"github.com/google/go-querystring/query"
autoconf "github.com/philips-software/go-hsdp-api/config"
hsdpsigner "github.com/philips-software/go-nih-signer"
)
type tokenType int
type ContextKey string
const (
userAgent = "go-hsdp-api/iam/" + internal.LibraryVersion
loginAPIVersion = "2"
)
type tokenResponse struct {
Scope string `json:"scope"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int64 `json:"expires_in"`
TokenType string `json:"token_type"`
IDToken string `json:"id_token"`
}
const (
OAuthToken tokenType = iota
JWTToken tokenType = 1
)
// OptionFunc is the function signature function for options
type OptionFunc func(*http.Request) error
// A Client manages communication with HSDP IAM API
type Client struct {
// HTTP client used to communicate with the API.
*http.Client
config *Config
signer *hsdpsigner.Signer
validate *validator.Validate
baseIAMURL *url.URL
baseIDMURL *url.URL
// token type used to make authenticated API calls.
tokenType tokenType
// tokens used to make authenticated API calls.
token string
refreshToken string
idToken string
expiresAt time.Time
service Service
// scope holds the client scope
scopes []string
// User agent used when communicating with the HSDP IAM API.
UserAgent string
Organizations *OrganizationsService
Groups *GroupsService
Permissions *PermissionsService
Roles *RolesService
Users *UsersService
Applications *ApplicationsService
Propositions *PropositionsService
Clients *ClientsService
Services *ServicesService
MFAPolicies *MFAPoliciesService
PasswordPolicies *PasswordPoliciesService
Devices *DevicesService
EmailTemplates *EmailTemplatesService
SMSGateways *SMSGatewaysService
SMSTemplates *SMSTemplatesService
sync.Mutex
}
// NewClient returns a new HSDP IAM API client. If a nil httpClient is
// provided, http.DefaultClient will be used. To use API methods which require
// authentication, provide a valid oAuth bearer token.
func NewClient(httpClient *http.Client, config *Config) (*Client, error) {
return newClient(httpClient, config)
}
func newClient(httpClient *http.Client, config *Config) (*Client, error) {
if httpClient == nil {
httpClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
}
}
doAutoconf(config)
c := &Client{Client: httpClient, config: config, UserAgent: userAgent}
if err := c.SetBaseIAMURL(c.config.IAMURL); err != nil {
return nil, err
}
if err := c.SetBaseIDMURL(c.config.IDMURL); err != nil {
return nil, err
}
if config.Signer == nil {
signer, err := hsdpsigner.New(c.config.SharedKey, c.config.SecretKey)
if err != nil { // Allow nil signer
signer = nil
}
c.signer = signer
} else {
c.signer = config.Signer
}
if config.DebugLog != nil {
httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, config.DebugLog)
}
c.validate = validator.New()
c.Organizations = &OrganizationsService{client: c}
c.Groups = &GroupsService{client: c}
c.Permissions = &PermissionsService{client: c}
c.Roles = &RolesService{client: c}
c.Users = &UsersService{client: c, validate: validator.New()}
c.Applications = &ApplicationsService{client: c}
c.Propositions = &PropositionsService{client: c}
c.Clients = &ClientsService{client: c, validate: validator.New()}
c.Services = &ServicesService{client: c}
c.MFAPolicies = &MFAPoliciesService{client: c, validate: validator.New()}
c.PasswordPolicies = &PasswordPoliciesService{client: c, validate: validator.New()}
devicesValidator := validator.New()
_ = devicesValidator.RegisterValidation("reserved-strings", internal.ValidateNoReservedStrings)
c.Devices = &DevicesService{client: c, validate: devicesValidator}
c.EmailTemplates = &EmailTemplatesService{client: c, validate: validator.New()}
c.SMSGateways = &SMSGatewaysService{client: c, validate: validator.New()}
c.SMSTemplates = &SMSTemplatesService{client: c, validate: validator.New()}
return c, nil
}
func doAutoconf(config *Config) {
if config.Region != "" && config.Environment != "" {
c, err := autoconf.New(
autoconf.WithRegion(config.Region),
autoconf.WithEnv(config.Environment))
if err == nil {
iamService := c.Service("iam")
idmService := c.Service("idm")
if iamService.URL != "" && config.IAMURL == "" {
config.IAMURL = iamService.URL
}
if idmService.URL != "" && config.IDMURL == "" {
config.IDMURL = idmService.URL
}
}
}
}
func (c *Client) validSigner() bool {
return c.signer != nil
}
// Close releases allocated resources of clients
func (c *Client) Close() {
}
// HttpClient returns the http Client used for connections
func (c *Client) HttpClient() *http.Client {
return c.Client
}
// WithToken returns a cloned client with the token set
func (c *Client) WithToken(token string) *Client {
client, _ := NewClient(c.Client, c.config)
client.SetToken(token)
return client
}
// WithLogin returns a cloned client with new login
func (c *Client) WithLogin(username, password string) (*Client, error) {
client, err := NewClient(c.Client, c.config)
if err != nil {
return nil, err
}
err = client.Login(username, password)
return client, err
}
func (c *Client) accessTokenEndpoint() string {
if c.baseIAMURL != nil {
return c.baseIAMURL.String() + "oauth2/access_token"
}
return ""
}
// Token returns the current token
func (c *Client) Token() (string, error) {
now := time.Now().Unix()
expires := c.expiresAt.Unix()
if expires-now < 60 {
if err := c.TokenRefresh(); err != nil {
return "", err
}
}
c.Lock()
defer c.Unlock()
return c.token, nil
}
// ExpireToken expires the token immediately
func (c *Client) ExpireToken() {
c.Lock()
defer c.Unlock()
c.expiresAt = time.Now()
}
// TokenRefresh forces a token refresh
func (c *Client) TokenRefresh() error {
c.Lock()
defer c.Unlock()
if c.refreshToken == "" {
if c.service.Valid() { // Possible service
return c.ServiceLogin(c.service)
}
return ErrMissingRefreshToken
}
u := *c.baseIAMURL
u.Opaque = c.baseIAMURL.Path + "authorize/oauth2/token"
req := &http.Request{
Method: "POST",
URL: &u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Host: u.Host,
}
form := url.Values{}
form.Add("grant_type", "refresh_token")
form.Add("refresh_token", c.refreshToken)
if len(c.config.Scopes) > 0 {
scopes := strings.Join(c.config.Scopes, " ")
form.Add("scope", scopes)
}
if !c.HasOAuth2Credentials() {
return ErrMissingOAuth2Credentials
}
req.SetBasicAuth(c.config.OAuth2ClientID, c.config.OAuth2Secret)
req.Body = io.NopCloser(strings.NewReader(form.Encode()))
req.ContentLength = int64(len(form.Encode()))
return c.doTokenRequest(req)
}
// HasOAuth2Credentials returns true if the client is configured with OAuth2 credentials
func (c *Client) HasOAuth2Credentials() bool {
return c.config.OAuth2ClientID != "" && c.config.OAuth2Secret != ""
}
// HasScopes returns true of all scopes are there for the client
func (c *Client) HasScopes(scopes ...string) bool {
for _, s := range scopes {
found := false
for _, t := range c.scopes {
if t == s {
found = true
break
}
}
if !found {
return false
}
}
return true
}
// HasSigningKeys returns true if this client is configured with IAM signing keys
func (c *Client) HasSigningKeys() bool {
if c.config == nil {
return false
}
return c.config.SharedKey != "" && c.config.SecretKey != ""
}
// HasPermissions returns true if all permissions are there for the client
func (c *Client) HasPermissions(orgID string, permissions ...string) bool {
introspect, _, err := c.Introspect(WithOrgContext(orgID))
if err != nil {
return false
}
foundOrg := false
for _, org := range introspect.Organizations.OrganizationList {
if org.OrganizationID != orgID {
continue
}
foundOrg = true
// Search in the organization effective permissions list
for _, p := range permissions {
found := false
for _, q := range org.EffectivePermissions {
if p == q {
found = true
continue
}
}
if !found {
// Permission is missing so return false
return false
}
}
}
return foundOrg
}
// SetToken sets the token
func (c *Client) SetToken(token string) {
c.token = token
c.expiresAt = time.Now().Add(86400 * time.Second)
c.tokenType = OAuthToken
}
// SetTokens sets the token
func (c *Client) SetTokens(accessToken, refreshToken, idToken string, expiresAt int64) {
c.Lock()
defer c.Unlock()
c.token = accessToken
c.refreshToken = refreshToken
c.idToken = idToken
c.expiresAt = time.Unix(expiresAt, 0)
c.tokenType = OAuthToken
}
// RefreshToken returns the refresh token
func (c *Client) RefreshToken() string {
return c.refreshToken
}
// IDToken returns the ID token
func (c *Client) IDToken() string {
return c.idToken
}
// Expires returns the expiry time (Unix) of the access token
func (c *Client) Expires() int64 {
return c.expiresAt.Unix()
}
// BaseIAMURL return a copy of the baseIAMURL.
func (c *Client) BaseIAMURL() *url.URL {
u := *c.baseIAMURL
return &u
}
// BaseIDMURL return a copy of the baseIAMURL.
func (c *Client) BaseIDMURL() *url.URL {
u := *c.baseIDMURL
return &u
}
// SetBaseIAMURL sets the base URL for API requests to a custom endpoint. urlStr
// should always be specified with a trailing slash.
func (c *Client) SetBaseIAMURL(urlStr string) error {
if urlStr == "" {
return ErrBaseIAMCannotBeEmpty
}
// Make sure the given URL ends with a slash
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
var err error
c.baseIAMURL, err = url.Parse(urlStr)
return err
}
// SetBaseIDMURL sets the base URL for API requests to a custom endpoint. urlStr
// should always be specified with a trailing slash.
func (c *Client) SetBaseIDMURL(urlStr string) error {
if urlStr == "" {
return ErrBaseIDMCannotBeEmpty
}
// Make sure the given URL ends with a slash
if !strings.HasSuffix(urlStr, "/") {
urlStr += "/"
}
var err error
c.baseIDMURL, err = url.Parse(urlStr)
return err
}
// Endpoint type
type Endpoint string
// Constants
const (
IAM = "IAM"
IDM = "IDM"
)
// newRequest creates an API request. A relative URL path can be provided in
// urlStr, in which case it is resolved relative to the base URL of the Client.
// Relative URL paths should always be specified without a preceding slash. If
// specified, the value pointed to by body is JSON encoded and included as the
// request body.
func (c *Client) newRequest(endpoint, method, path string, opt interface{}, options []OptionFunc) (*http.Request, error) {
var u url.URL
switch endpoint {
case IDM:
u = *c.baseIDMURL
u.Opaque = c.baseIDMURL.Path + path
case IAM:
u = *c.baseIAMURL
u.Opaque = c.baseIAMURL.Path + path
default:
return nil, fmt.Errorf("unknown endpoint: `%s`", endpoint)
}
if opt != nil {
q, err := query.Values(opt)
if err != nil {
return nil, err
}
u.RawQuery = q.Encode()
}
req := &http.Request{
Method: method,
URL: &u,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Host: u.Host,
}
req.Header.Set("User-Agent", userAgent)
if method == "POST" || method == "PUT" {
bodyBytes, err := json.Marshal(opt)
if err != nil {
return nil, err
}
bodyReader := bytes.NewReader(bodyBytes)
u.RawQuery = ""
req.Body = io.NopCloser(bodyReader)
req.ContentLength = int64(bodyReader.Len())
req.Header.Set("Content-Type", "application/json")
}
req.Header.Set("Accept", "application/json")
switch c.tokenType {
case OAuthToken:
if token, err := c.Token(); err == nil {
req.Header.Set("Authorization", "Bearer "+token)
} else {
req.Header.Set("X-Token-Error", fmt.Sprintf("%v", err))
}
}
for _, fn := range options {
if fn == nil {
continue
}
if err := fn(req); err != nil {
return nil, err
}
}
return req, nil
}
// Response is a HSDP IAM API response. This wraps the standard http.Response
// returned from HSDP IAM and provides convenient access to things like errors
type Response struct {
*http.Response
}
func (r *Response) StatusCode() int {
if r.Response != nil {
return r.Response.StatusCode
}
return 0
}
// newResponse creates a new Response for the provided http.Response.
func newResponse(r *http.Response) *Response {
response := &Response{Response: r}
return response
}
// doSigned performs a signed API request
func (c *Client) doSigned(req *http.Request, v interface{}) (*Response, error) {
if c.signer == nil {
return nil, ErrNoValidSignerAvailable
}
if err := c.signer.SignRequest(req); err != nil {
return nil, err
}
return c.do(req, v)
}
func (c *Client) do(req *http.Request, v interface{}) (*Response, error) {
resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp != nil {
defer func() {
_ = resp.Body.Close()
}()
}
response := newResponse(resp)
err = internal.CheckResponse(resp)
if err != nil {
// even though there was an error, we still return the response
// in case the caller wants to inspect it further
return response, err
}
if v != nil && response.StatusCode() != http.StatusNoContent {
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, resp.Body)
} else {
err = json.NewDecoder(resp.Body).Decode(v)
}
}
return response, err
}
// WithContext runs the request with the provided context
func WithContext(ctx context.Context) OptionFunc {
return func(req *http.Request) error {
*req = *req.WithContext(ctx)
return nil
}
}
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string {
p := new(string)
*p = v
return p
}
// ErrorResponse represents an IAM errors response
// containing a code and a human-readable message
type ErrorResponse struct {
Response *http.Response `json:"-"`
Code string `json:"responseCode,omitempty"`
Message string `json:"responseMessage,omitempty"`
ErrorString string `json:"error,omitempty"`
ErrorDescription string `json:"error_description,omitempty"`
}
func (e *ErrorResponse) Error() string {
path, _ := url.QueryUnescape(e.Response.Request.URL.Opaque)
u := fmt.Sprintf("%s://%s%s", e.Response.Request.URL.Scheme, e.Response.Request.URL.Host, path)
return fmt.Sprintf("%s %s: %d %s", e.Response.Request.Method, u, e.Response.StatusCode, e.Message)
}