-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrealms_realms.go
150 lines (124 loc) · 4.24 KB
/
realms_realms.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
package goztl
import (
"context"
"fmt"
"net/http"
)
const rBasePath = "realms/realms/"
// RealmsRealmsService is an interface for interfacing with the realms
// endpoints of the Zentral API
type RealmsRealmsService interface {
List(context.Context, *ListOptions) ([]RealmsRealm, *Response, error)
GetByUUID(context.Context, string) (*RealmsRealm, *Response, error)
GetByName(context.Context, string) (*RealmsRealm, *Response, error)
}
// RealmsRealmsServiceOp handles communication with the realms related
// methods of the Zentral API.
type RealmsRealmsServiceOp struct {
client *Client
}
var _ RealmsRealmsService = &RealmsRealmsServiceOp{}
// LDAPConfig represents a Zentral Realm LDAP config
type LDAPConfig struct {
Host string `json:"host"`
BindDN string `json:"bind_dn"`
BindPassword string `json:"bind_password"`
UsersBaseDN string `json:"users_base_dn"`
}
// OpenIDCConfig represents a Zentral Realm OpenIDC config
type OpenIDCConfig struct {
DiscoveryURL string `json:"discovery_url"`
ClientID string `json:"client_id"`
ClientSecret *string `json:"client_secret"`
ExtraScopes []string `json:"extra_scopes"`
}
// SAMLConfig represents a Zentral Realm SAML config
type SAMLConfig struct {
DefaultRelayState string `json:"default_relay_state"`
IDPMetadata string `json:"idp_metadata"`
}
// RealmsRealm represents a Zentral realm
type RealmsRealm struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Backend string `json:"backend"`
LDAPConfig *LDAPConfig `json:"ldap_config"`
OpenIDCConfig *OpenIDCConfig `json:"openidc_config"`
SAMLConfig *SAMLConfig `json:"saml_config"`
EnabledForLogin bool `json:"enabled_for_login"`
LoginSessionExpiry int `json:"login_session_expiry"`
UsernameClaim string `json:"username_claim"`
EmailClaim string `json:"email_claim"`
FirstNameClaim string `json:"first_name_claim"`
LastNameClaim string `json:"last_name_claim"`
FullNameClaim string `json:"full_name_claim"`
CustomAttr1Claim string `json:"custom_attr_1_claim"`
CustomAttr2Claim string `json:"custom_attr_2_claim"`
SCIMEnabled bool `json:"scim_enabled"`
Created Timestamp `json:"created_at"`
Updated Timestamp `json:"updated_at"`
}
func (r RealmsRealm) String() string {
return Stringify(r)
}
type listROptions struct {
Name string `url:"name,omitempty"`
}
// List lists all the Realms realms.
func (s *RealmsRealmsServiceOp) List(ctx context.Context, opt *ListOptions) ([]RealmsRealm, *Response, error) {
return s.list(ctx, opt, nil)
}
// GetByID retrieves a Realms realm by id.
func (s *RealmsRealmsServiceOp) GetByUUID(ctx context.Context, rUUID string) (*RealmsRealm, *Response, error) {
if len(rUUID) < 1 {
return nil, nil, NewArgError("rUUID", "cannot be empty")
}
path := fmt.Sprintf("%s%s/", rBasePath, rUUID)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
r := new(RealmsRealm)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}
return r, resp, err
}
// GetByName retrieves a Realms realm by name.
func (s *RealmsRealmsServiceOp) GetByName(ctx context.Context, name string) (*RealmsRealm, *Response, error) {
if len(name) < 1 {
return nil, nil, NewArgError("name", "cannot be blank")
}
listROpt := &listROptions{Name: name}
rs, resp, err := s.list(ctx, nil, listROpt)
if err != nil {
return nil, resp, err
}
if len(rs) < 1 {
return nil, resp, nil
}
return &rs[0], resp, err
}
// Helper method for listing Realms realms
func (s *RealmsRealmsServiceOp) list(ctx context.Context, opt *ListOptions, rOpt *listROptions) ([]RealmsRealm, *Response, error) {
path := rBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
path, err = addOptions(path, rOpt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
var rs []RealmsRealm
resp, err := s.client.Do(ctx, req, &rs)
if err != nil {
return nil, resp, err
}
return rs, resp, err
}