-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from 3scale/oidc-configuration-endpoints
Oidc configuration endpoints
- Loading branch information
Showing
5 changed files
with
191 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
const ( | ||
oidcResourceEndpoint = "/admin/api/services/%d/proxy/oidc_configuration.json" | ||
) | ||
|
||
// OIDCConfiguration fetches 3scale product oidc configuration | ||
func (c *ThreeScaleClient) OIDCConfiguration(productID int64) (*OIDCConfiguration, error) { | ||
endpoint := fmt.Sprintf(oidcResourceEndpoint, productID) | ||
req, err := c.buildGetReq(endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
oidcConf := &OIDCConfiguration{} | ||
err = handleJsonResp(resp, http.StatusOK, oidcConf) | ||
return oidcConf, err | ||
} | ||
|
||
// UpdateOIDCConfiguration Update 3scale product oidc configuration | ||
func (c *ThreeScaleClient) UpdateOIDCConfiguration(productID int64, oidcConf *OIDCConfiguration) (*OIDCConfiguration, error) { | ||
endpoint := fmt.Sprintf(oidcResourceEndpoint, productID) | ||
|
||
bodyArr, err := json.Marshal(oidcConf) | ||
if err != nil { | ||
return nil, err | ||
} | ||
body := bytes.NewReader(bodyArr) | ||
req, err := c.buildPatchJSONReq(endpoint, body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.httpClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
newConf := &OIDCConfiguration{} | ||
err = handleJsonResp(resp, http.StatusOK, newConf) | ||
return newConf, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestOIDCConfiguration(t *testing.T) { | ||
var ( | ||
productID int64 = 97 | ||
endpoint = fmt.Sprintf(oidcResourceEndpoint, productID) | ||
oidcConf = &OIDCConfiguration{ | ||
Element: OIDCConfigurationItem{ | ||
StandardFlowEnabled: false, | ||
ImplicitFlowEnabled: true, | ||
ServiceAccountsEnabled: false, | ||
DirectAccessGrantsEnabled: true, | ||
}, | ||
} | ||
) | ||
|
||
httpClient := NewTestClient(func(req *http.Request) *http.Response { | ||
if req.URL.Path != endpoint { | ||
t.Fatalf("Path does not match. Expected [%s]; got [%s]", endpoint, req.URL.Path) | ||
} | ||
|
||
if req.Method != http.MethodGet { | ||
t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodGet, req.Method) | ||
} | ||
|
||
responseBodyBytes, err := json.Marshal(oidcConf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewBuffer(responseBodyBytes)), | ||
Header: make(http.Header), | ||
} | ||
}) | ||
|
||
credential := "someAccessToken" | ||
c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient) | ||
obj, err := c.OIDCConfiguration(productID) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if obj == nil { | ||
t.Fatal("response was nil") | ||
} | ||
|
||
if *obj != *oidcConf { | ||
t.Fatalf("Expected %v; got %v", *oidcConf, *obj) | ||
} | ||
} | ||
|
||
func TestUpdateOIDCConfiguration(t *testing.T) { | ||
var ( | ||
productID int64 = 98765 | ||
endpoint = fmt.Sprintf(oidcResourceEndpoint, productID) | ||
oidcConf = &OIDCConfiguration{ | ||
Element: OIDCConfigurationItem{ | ||
StandardFlowEnabled: false, | ||
ImplicitFlowEnabled: true, | ||
ServiceAccountsEnabled: false, | ||
DirectAccessGrantsEnabled: true, | ||
}, | ||
} | ||
) | ||
|
||
httpClient := NewTestClient(func(req *http.Request) *http.Response { | ||
if req.URL.Path != endpoint { | ||
t.Fatalf("Path does not match. Expected [%s]; got [%s]", endpoint, req.URL.Path) | ||
} | ||
|
||
if req.Method != http.MethodPatch { | ||
t.Fatalf("Method does not match. Expected [%s]; got [%s]", http.MethodPatch, req.Method) | ||
} | ||
|
||
responseBodyBytes, err := json.Marshal(*oidcConf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewBuffer(responseBodyBytes)), | ||
Header: make(http.Header), | ||
} | ||
}) | ||
|
||
credential := "someAccessToken" | ||
c := NewThreeScale(NewTestAdminPortal(t), credential, httpClient) | ||
obj, err := c.UpdateOIDCConfiguration(productID, oidcConf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if obj == nil { | ||
t.Fatal("resp returned nil") | ||
} | ||
|
||
if *obj != *oidcConf { | ||
t.Fatalf("Expected %v; got %v", *oidcConf, *obj) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters