-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for OAuth applications
In this commit we are adding the CRUD methods and the rotate client secret for for OAuth Applications, the documentation for those API can be found at https://clerk.com/docs/reference/backend-api/tag/OAuth-Applications
- Loading branch information
Showing
4 changed files
with
417 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package clerk | ||
|
||
type OAuthApplication struct { | ||
APIResource | ||
Object string `json:"object"` | ||
ID string `json:"id"` | ||
InstanceID string `json:"instance_id"` | ||
Name string `json:"name"` | ||
ClientID string `json:"client_id"` | ||
Public bool `json:"public"` | ||
Scopes string `json:"scopes"` | ||
CallbackURL string `json:"callback_url"` | ||
AuthorizeURL string `json:"authorize_url"` | ||
TokenFetchURL string `json:"token_fetch_url"` | ||
UserInfoURL string `json:"user_info_url"` | ||
CreatedAt int64 `json:"created_at"` | ||
UpdatedAt int64 `json:"updated_at"` | ||
} | ||
|
||
type OAuthApplicationWithClientSecret struct { | ||
APIResource | ||
OAuthApplication | ||
ClientSecret string `json:"client_secret"` | ||
} | ||
|
||
type OAuthApplicationList struct { | ||
APIResource | ||
OAuthApplications []*OAuthApplication `json:"data"` | ||
TotalCount int64 `json:"total_count"` | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,116 @@ | ||
// Package oauthapplication provides the OAuth applications API. | ||
package oauthapplication | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/clerk/clerk-sdk-go/v2" | ||
) | ||
|
||
//go:generate go run ../cmd/gen/main.go | ||
|
||
const path = "/oauth_applications" | ||
|
||
type Client struct { | ||
Backend clerk.Backend | ||
} | ||
|
||
func NewClient(config *clerk.ClientConfig) *Client { | ||
return &Client{ | ||
Backend: clerk.NewBackend(&config.BackendConfig), | ||
} | ||
} | ||
|
||
// fetches a single OAuth application by its ID. | ||
func (c *Client) Get(ctx context.Context, id string) (*clerk.OAuthApplication, error) { | ||
path, err := clerk.JoinPath(path, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodGet, path) | ||
resource := &clerk.OAuthApplication{} | ||
err = c.Backend.Call(ctx, req, resource) | ||
return resource, err | ||
} | ||
|
||
type ListParams struct { | ||
clerk.APIParams | ||
clerk.ListParams | ||
} | ||
|
||
func (params *ListParams) ToQuery() url.Values { | ||
q := params.ListParams.ToQuery() | ||
return q | ||
} | ||
|
||
// Retrieves all OAuth applications. | ||
func (c *Client) List(ctx context.Context, params *ListParams) (*clerk.OAuthApplicationList, error) { | ||
req := clerk.NewAPIRequest(http.MethodGet, path) | ||
req.SetParams(params) | ||
list := &clerk.OAuthApplicationList{} | ||
err := c.Backend.Call(ctx, req, list) | ||
return list, err | ||
} | ||
|
||
type OAuthApplicationCreateParams struct { | ||
clerk.APIParams | ||
Name string `json:"name"` | ||
CallbackURL string `json:"callback_url"` | ||
Scopes string `json:"scopes"` | ||
Public bool `json:"public"` | ||
} | ||
|
||
// Creates a new OAuth application with the given parameters. | ||
func (c *Client) Create(ctx context.Context, params *OAuthApplicationCreateParams) (*clerk.OAuthApplicationWithClientSecret, error) { | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
req.SetParams(params) | ||
authApplication := &clerk.OAuthApplicationWithClientSecret{} | ||
err := c.Backend.Call(ctx, req, authApplication) | ||
return authApplication, err | ||
} | ||
|
||
type OAuthApplicationUpdateParams struct { | ||
clerk.APIParams | ||
Name *string `json:"name,omitempty"` | ||
CallbackURL *string `json:"callback_url,omitempty"` | ||
Scopes *string `json:"scopes,omitempty"` | ||
} | ||
|
||
// Updates an existing OAuth application. | ||
func (c *Client) Update(ctx context.Context, id string, params *OAuthApplicationUpdateParams) (*clerk.OAuthApplication, error) { | ||
path, err := clerk.JoinPath(path, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodPatch, path) | ||
req.SetParams(params) | ||
authApplication := &clerk.OAuthApplication{} | ||
err = c.Backend.Call(ctx, req, authApplication) | ||
return authApplication, err | ||
} | ||
|
||
// Deletes the given OAuth application | ||
func (c *Client) DeleteOAuthApplication(ctx context.Context, id string) (*clerk.DeletedResource, error) { | ||
path, err := clerk.JoinPath(path, id) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodDelete, path) | ||
authApplication := &clerk.DeletedResource{} | ||
err = c.Backend.Call(ctx, req, authApplication) | ||
return authApplication, err | ||
} | ||
|
||
// Rotates the OAuth application's client secret | ||
func (c *Client) RotateClientSecret(ctx context.Context, id string) (*clerk.OAuthApplicationWithClientSecret, error) { | ||
path, err := clerk.JoinPath(path, id, "rotate_secret") | ||
if err != nil { | ||
return nil, err | ||
} | ||
req := clerk.NewAPIRequest(http.MethodPost, path) | ||
authApplication := &clerk.OAuthApplicationWithClientSecret{} | ||
err = c.Backend.Call(ctx, req, authApplication) | ||
return authApplication, err | ||
} |
Oops, something went wrong.