Skip to content

Commit 68e385d

Browse files
authored
Added Create and Update APIs for OIDCProviderConfig (#283)
1 parent fa5134f commit 68e385d

File tree

2 files changed

+578
-17
lines changed

2 files changed

+578
-17
lines changed

auth/provider_config.go

Lines changed: 189 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ const (
4040
spEntityIDKey = "spConfig.spEntityId"
4141
callbackURIKey = "spConfig.callbackUri"
4242

43+
clientIDKey = "clientId"
44+
issuerKey = "issuer"
45+
4346
displayNameKey = "displayName"
4447
enabledKey = "enabled"
4548
)
@@ -119,6 +122,129 @@ type OIDCProviderConfig struct {
119122
Issuer string
120123
}
121124

125+
// OIDCProviderConfigToCreate represents the options used to create a new OIDCProviderConfig.
126+
type OIDCProviderConfigToCreate struct {
127+
id string
128+
params nestedMap
129+
}
130+
131+
// ID sets the provider ID of the new config.
132+
func (config *OIDCProviderConfigToCreate) ID(id string) *OIDCProviderConfigToCreate {
133+
config.id = id
134+
return config
135+
}
136+
137+
// ClientID sets the client ID of the new config.
138+
func (config *OIDCProviderConfigToCreate) ClientID(clientID string) *OIDCProviderConfigToCreate {
139+
return config.set(clientIDKey, clientID)
140+
}
141+
142+
// Issuer sets the issuer of the new config.
143+
func (config *OIDCProviderConfigToCreate) Issuer(issuer string) *OIDCProviderConfigToCreate {
144+
return config.set(issuerKey, issuer)
145+
}
146+
147+
// DisplayName sets the DisplayName field of the new config.
148+
func (config *OIDCProviderConfigToCreate) DisplayName(name string) *OIDCProviderConfigToCreate {
149+
return config.set(displayNameKey, name)
150+
}
151+
152+
// Enabled enables or disables the new config.
153+
func (config *OIDCProviderConfigToCreate) Enabled(enabled bool) *OIDCProviderConfigToCreate {
154+
return config.set(enabledKey, enabled)
155+
}
156+
157+
func (config *OIDCProviderConfigToCreate) set(key string, value interface{}) *OIDCProviderConfigToCreate {
158+
if config.params == nil {
159+
config.params = make(nestedMap)
160+
}
161+
162+
config.params.Set(key, value)
163+
return config
164+
}
165+
166+
func (config *OIDCProviderConfigToCreate) buildRequest() (nestedMap, string, error) {
167+
if err := validateOIDCConfigID(config.id); err != nil {
168+
return nil, "", err
169+
}
170+
171+
if len(config.params) == 0 {
172+
return nil, "", errors.New("no parameters specified in the create request")
173+
}
174+
175+
if val, ok := config.params.GetString(clientIDKey); !ok || val == "" {
176+
return nil, "", errors.New("ClientID must not be empty")
177+
}
178+
179+
if val, ok := config.params.GetString(issuerKey); !ok || val == "" {
180+
return nil, "", errors.New("Issuer must not be empty")
181+
} else if _, err := url.ParseRequestURI(val); err != nil {
182+
return nil, "", fmt.Errorf("failed to parse Issuer: %v", err)
183+
}
184+
185+
return config.params, config.id, nil
186+
}
187+
188+
// OIDCProviderConfigToUpdate represents the options used to update an existing OIDCProviderConfig.
189+
type OIDCProviderConfigToUpdate struct {
190+
params nestedMap
191+
}
192+
193+
// ClientID updates the client ID of the config.
194+
func (config *OIDCProviderConfigToUpdate) ClientID(clientID string) *OIDCProviderConfigToUpdate {
195+
return config.set(clientIDKey, clientID)
196+
}
197+
198+
// Issuer updates the issuer of the config.
199+
func (config *OIDCProviderConfigToUpdate) Issuer(issuer string) *OIDCProviderConfigToUpdate {
200+
return config.set(issuerKey, issuer)
201+
}
202+
203+
// DisplayName updates the DisplayName field of the config.
204+
func (config *OIDCProviderConfigToUpdate) DisplayName(name string) *OIDCProviderConfigToUpdate {
205+
var nameOrNil interface{}
206+
if name != "" {
207+
nameOrNil = name
208+
}
209+
210+
return config.set(displayNameKey, nameOrNil)
211+
}
212+
213+
// Enabled enables or disables the config.
214+
func (config *OIDCProviderConfigToUpdate) Enabled(enabled bool) *OIDCProviderConfigToUpdate {
215+
return config.set(enabledKey, enabled)
216+
}
217+
218+
func (config *OIDCProviderConfigToUpdate) set(key string, value interface{}) *OIDCProviderConfigToUpdate {
219+
if config.params == nil {
220+
config.params = make(nestedMap)
221+
}
222+
223+
config.params.Set(key, value)
224+
return config
225+
}
226+
227+
func (config *OIDCProviderConfigToUpdate) buildRequest() (nestedMap, error) {
228+
if len(config.params) == 0 {
229+
return nil, errors.New("no parameters specified in the update request")
230+
}
231+
232+
if val, ok := config.params.GetString(clientIDKey); ok && val == "" {
233+
return nil, errors.New("ClientID must not be empty")
234+
}
235+
236+
if val, ok := config.params.GetString(issuerKey); ok {
237+
if val == "" {
238+
return nil, errors.New("Issuer must not be empty")
239+
}
240+
if _, err := url.ParseRequestURI(val); err != nil {
241+
return nil, fmt.Errorf("failed to parse Issuer: %v", err)
242+
}
243+
}
244+
245+
return config.params, nil
246+
}
247+
122248
// SAMLProviderConfig is the SAML auth provider configuration.
123249
// See http://docs.oasis-open.org/security/saml/Post2.0/sstc-saml-tech-overview-2.0.html.
124250
type SAMLProviderConfig struct {
@@ -292,7 +418,7 @@ func (config *SAMLProviderConfigToUpdate) DisplayName(name string) *SAMLProvider
292418
return config.set(displayNameKey, nameOrNil)
293419
}
294420

295-
// Enabled enables or disables the new config.
421+
// Enabled enables or disables the config.
296422
func (config *SAMLProviderConfigToUpdate) Enabled(enabled bool) *SAMLProviderConfigToUpdate {
297423
return config.set(enabledKey, enabled)
298424
}
@@ -450,6 +576,68 @@ func (c *providerConfigClient) OIDCProviderConfig(ctx context.Context, id string
450576
return result.toOIDCProviderConfig(), nil
451577
}
452578

579+
// CreateOIDCProviderConfig creates a new OIDC provider config from the given parameters.
580+
func (c *providerConfigClient) CreateOIDCProviderConfig(ctx context.Context, config *OIDCProviderConfigToCreate) (*OIDCProviderConfig, error) {
581+
if config == nil {
582+
return nil, errors.New("config must not be nil")
583+
}
584+
585+
body, id, err := config.buildRequest()
586+
if err != nil {
587+
return nil, err
588+
}
589+
590+
req := &internal.Request{
591+
Method: http.MethodPost,
592+
URL: "/oauthIdpConfigs",
593+
Body: internal.NewJSONEntity(body),
594+
Opts: []internal.HTTPOption{
595+
internal.WithQueryParam("oauthIdpConfigId", id),
596+
},
597+
}
598+
var result oidcProviderConfigDAO
599+
if _, err := c.makeRequest(ctx, req, &result); err != nil {
600+
return nil, err
601+
}
602+
603+
return result.toOIDCProviderConfig(), nil
604+
}
605+
606+
// UpdateOIDCProviderConfig updates an existing OIDC provider config with the given parameters.
607+
func (c *providerConfigClient) UpdateOIDCProviderConfig(ctx context.Context, id string, config *OIDCProviderConfigToUpdate) (*OIDCProviderConfig, error) {
608+
if err := validateOIDCConfigID(id); err != nil {
609+
return nil, err
610+
}
611+
if config == nil {
612+
return nil, errors.New("config must not be nil")
613+
}
614+
615+
body, err := config.buildRequest()
616+
if err != nil {
617+
return nil, err
618+
}
619+
620+
mask, err := body.UpdateMask()
621+
if err != nil {
622+
return nil, fmt.Errorf("failed to construct update mask: %v", err)
623+
}
624+
625+
req := &internal.Request{
626+
Method: http.MethodPatch,
627+
URL: fmt.Sprintf("/oauthIdpConfigs/%s", id),
628+
Body: internal.NewJSONEntity(body),
629+
Opts: []internal.HTTPOption{
630+
internal.WithQueryParam("updateMask", strings.Join(mask, ",")),
631+
},
632+
}
633+
var result oidcProviderConfigDAO
634+
if _, err := c.makeRequest(ctx, req, &result); err != nil {
635+
return nil, err
636+
}
637+
638+
return result.toOIDCProviderConfig(), nil
639+
}
640+
453641
// DeleteOIDCProviderConfig deletes the OIDCProviderConfig with the given ID.
454642
func (c *providerConfigClient) DeleteOIDCProviderConfig(ctx context.Context, id string) error {
455643
if err := validateOIDCConfigID(id); err != nil {

0 commit comments

Comments
 (0)