From 41bfc79be26f14a1e91bc42618e73e09ac91307e Mon Sep 17 00:00:00 2001 From: Rakesh Patel Date: Thu, 4 Jul 2024 15:15:52 -0700 Subject: [PATCH] Add create oauth method calls --- pkg/oauthcredentials/client.go | 42 ++++++++++++++++++++++-- pkg/oauthcredentials/oauthcredentials.go | 8 +++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/pkg/oauthcredentials/client.go b/pkg/oauthcredentials/client.go index f73e2d93..ddfc28a5 100644 --- a/pkg/oauthcredentials/client.go +++ b/pkg/oauthcredentials/client.go @@ -67,8 +67,8 @@ type OAuthConnectionType string const ( AppleOauth OAuthConnectionType = "AppleOauth" GithubOauth OAuthConnectionType = "GitHubOauth" - GoogleOAuth OAuthConnectionType = "GoogleOAuth" - MicrosoftOAuth OAuthConnectionType = "MicrosoftOAuth" + GoogleOauth OAuthConnectionType = "GoogleOauth" + MicrosoftOauth OAuthConnectionType = "MicrosoftOauth" ) // OAuthConnectionState represents the state of an OAuth Connection. @@ -343,3 +343,41 @@ func (c *Client) UpdateOAuthCredential(ctx context.Context, opts UpdateOAuthCred err = dec.Decode(&body) return body, err } + +type CreateOAuthCredentialOpts struct { + Type OAuthConnectionType `json:"type"` +} + +func (c *Client) CreateOAuthCredential(ctx context.Context, opts CreateOAuthCredentialOpts) (OAuthCredential, error) { + c.once.Do(c.init) + + data, err := c.JSONEncode(opts) + if err != nil { + return OAuthCredential{}, err + } + + endpoint := fmt.Sprintf("%s/oauth-credentials", c.Endpoint) + req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(data)) + if err != nil { + return OAuthCredential{}, err + } + req = req.WithContext(ctx) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+c.APIKey) + req.Header.Set("User-Agent", "workos-go/"+workos.Version) + + res, err := c.HTTPClient.Do(req) + if err != nil { + return OAuthCredential{}, err + } + defer res.Body.Close() + + if err = workos_errors.TryGetHTTPError(res); err != nil { + return OAuthCredential{}, err + } + + var body OAuthCredential + dec := json.NewDecoder(res.Body) + err = dec.Decode(&body) + return body, err +} diff --git a/pkg/oauthcredentials/oauthcredentials.go b/pkg/oauthcredentials/oauthcredentials.go index a1caf185..aba43c4d 100644 --- a/pkg/oauthcredentials/oauthcredentials.go +++ b/pkg/oauthcredentials/oauthcredentials.go @@ -40,3 +40,11 @@ func UpdateOAuthCredential( ) (OAuthCredential, error) { return DefaultClient.UpdateOAuthCredential(ctx, opts) } + +// CreateOAuthCredential creates an OAuthCredential. +func CreateOAuthCredential( + ctx context.Context, + opts CreateOAuthCredentialOpts, +) (OAuthCredential, error) { + return DefaultClient.CreateOAuthCredential(ctx, opts) +}