Skip to content

Commit

Permalink
Add create oauth method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
rocpatel committed Jul 4, 2024
1 parent 5df6851 commit 41bfc79
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
42 changes: 40 additions & 2 deletions pkg/oauthcredentials/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions pkg/oauthcredentials/oauthcredentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 41bfc79

Please sign in to comment.