Skip to content

Commit

Permalink
feat: Add support for OAuth applications
Browse files Browse the repository at this point in the history
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
AlexNti committed Nov 14, 2024
1 parent fe4b64f commit b13cd71
Show file tree
Hide file tree
Showing 4 changed files with 417 additions and 0 deletions.
30 changes: 30 additions & 0 deletions oauth_application.go
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"`
}
45 changes: 45 additions & 0 deletions oauthapplication/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions oauthapplication/client.go
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
}
Loading

0 comments on commit b13cd71

Please sign in to comment.