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 2fcc202
Show file tree
Hide file tree
Showing 5 changed files with 415 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.3.0

- Add support for the OAuth Applications API. Added the oauthapplication package for API operations and a clerk.OAuthApplication type.

## 2.2.0

- Add support for bulk invitation creation with the `invitation.BulkCreate` method.
Expand Down
25 changes: 25 additions & 0 deletions oauth_application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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"`
ClientSecret *string `json:"client_secret,omitempty"`
}

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.

115 changes: 115 additions & 0 deletions oauthapplication/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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),
}
}

// Get 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 {
return params.ListParams.ToQuery()
}

// List 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 CreateParams struct {
clerk.APIParams
Name string `json:"name"`
CallbackURL string `json:"callback_url"`
Scopes string `json:"scopes"`
Public bool `json:"public"`
}

// Create creates a new OAuth application with the given parameters.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.OAuthApplication, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)
authApplication := &clerk.OAuthApplication{}
err := c.Backend.Call(ctx, req, authApplication)
return authApplication, err
}

type UpdateParams struct {
clerk.APIParams
Name *string `json:"name,omitempty"`
CallbackURL *string `json:"callback_url,omitempty"`
Scopes *string `json:"scopes,omitempty"`
}

// Update updates an existing OAuth application.
func (c *Client) Update(ctx context.Context, id string, params *UpdateParams) (*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
}

// Delete 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
}

// RotateClientSecret rotates the OAuth application's client secret
func (c *Client) RotateClientSecret(ctx context.Context, id string) (*clerk.OAuthApplication, error) {
path, err := clerk.JoinPath(path, id, "rotate_secret")
if err != nil {
return nil, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
authApplication := &clerk.OAuthApplication{}
err = c.Backend.Call(ctx, req, authApplication)
return authApplication, err
}
Loading

0 comments on commit 2fcc202

Please sign in to comment.