This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement /admin/sso/providers endpoints
- Loading branch information
1 parent
8feb4de
commit 9b06acb
Showing
4 changed files
with
285 additions
and
47 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
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,184 @@ | ||
package endpoints | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/kwoodhouse93/gotrue-go/types" | ||
) | ||
|
||
const adminSSOPath = "/admin/sso/providers" | ||
|
||
// GET /admin/sso/providers | ||
// | ||
// Get a list of all SAML SSO Identity Providers in the system. | ||
func (c *Client) AdminListSSOProviders() (*types.AdminListSSOProvidersResponse, error) { | ||
r, err := c.newRequest(adminSSOPath, http.MethodGet, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.client.Do(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
fullBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("response status code %d", resp.StatusCode) | ||
} | ||
return nil, fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody) | ||
} | ||
|
||
var res types.AdminListSSOProvidersResponse | ||
err = json.NewDecoder(resp.Body).Decode(&res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &res, nil | ||
} | ||
|
||
// POST /admin/sso/providers | ||
// | ||
// Create a new SAML SSO Identity Provider. | ||
func (c *Client) AdminCreateSSOProvider(req types.AdminCreateSSOProviderRequest) (*types.AdminCreateSSOProviderResponse, error) { | ||
body, err := json.Marshal(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r, err := c.newRequest(adminSSOPath, http.MethodPost, bytes.NewBuffer(body)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.client.Do(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
fullBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("response status code %d", resp.StatusCode) | ||
} | ||
return nil, fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody) | ||
} | ||
|
||
var res types.AdminCreateSSOProviderResponse | ||
err = json.NewDecoder(resp.Body).Decode(&res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &res, nil | ||
} | ||
|
||
// GET /admin/sso/providers/{idp_id} | ||
// | ||
// Get a SAML SSO Identity Provider by ID. | ||
func (c *Client) AdminGetSSOProvider(req types.AdminGetSSOProviderRequest) (*types.AdminGetSSOProviderResponse, error) { | ||
r, err := c.newRequest(fmt.Sprintf("%s/%s", adminSSOPath, req.ProviderID), http.MethodGet, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.client.Do(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
fullBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("response status code %d", resp.StatusCode) | ||
} | ||
return nil, fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody) | ||
} | ||
|
||
var res types.AdminGetSSOProviderResponse | ||
err = json.NewDecoder(resp.Body).Decode(&res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &res, nil | ||
} | ||
|
||
// PUT /admin/sso/providers/{idp_id} | ||
// | ||
// Update a SAML SSO Identity Provider by ID. | ||
func (c *Client) AdminUpdateSSOProvider(req types.AdminUpdateSSOProviderRequest) (*types.AdminUpdateSSOProviderResponse, error) { | ||
body, err := json.Marshal(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
r, err := c.newRequest(fmt.Sprintf("%s/%s", adminSSOPath, req.ProviderID), http.MethodPut, bytes.NewBuffer(body)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.client.Do(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
fullBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("response status code %d", resp.StatusCode) | ||
} | ||
return nil, fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody) | ||
} | ||
|
||
var res types.AdminUpdateSSOProviderResponse | ||
err = json.NewDecoder(resp.Body).Decode(&res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &res, nil | ||
} | ||
|
||
// DELETE /admin/sso/providers/{idp_id} | ||
// | ||
// Delete a SAML SSO Identity Provider by ID. | ||
func (c *Client) AdminDeleteSSOProvider(req types.AdminDeleteSSOProviderRequest) (*types.AdminDeleteSSOProviderResponse, error) { | ||
path := fmt.Sprintf("%s/%s", adminSSOPath, req.ProviderID) | ||
r, err := c.newRequest(path, http.MethodDelete, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := c.client.Do(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
fullBody, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("response status code %d", resp.StatusCode) | ||
} | ||
return nil, fmt.Errorf("response status code %d: %s", resp.StatusCode, fullBody) | ||
} | ||
|
||
var res types.AdminDeleteSSOProviderResponse | ||
err = json.NewDecoder(resp.Body).Decode(&res) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &res, nil | ||
} |
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