Skip to content

Commit d4beef1

Browse files
committed
Add create oauth method calls
1 parent 84a9662 commit d4beef1

2 files changed

Lines changed: 48 additions & 2 deletions

File tree

pkg/oauthcredentials/client.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ type OAuthConnectionType string
6767
const (
6868
AppleOauth OAuthConnectionType = "AppleOauth"
6969
GithubOauth OAuthConnectionType = "GitHubOauth"
70-
GoogleOAuth OAuthConnectionType = "GoogleOAuth"
71-
MicrosoftOAuth OAuthConnectionType = "MicrosoftOAuth"
70+
GoogleOauth OAuthConnectionType = "GoogleOauth"
71+
MicrosoftOauth OAuthConnectionType = "MicrosoftOauth"
7272
)
7373

7474
// OAuthConnectionState represents the state of an OAuth Connection.
@@ -343,3 +343,41 @@ func (c *Client) UpdateOAuthCredential(ctx context.Context, opts UpdateOAuthCred
343343
err = dec.Decode(&body)
344344
return body, err
345345
}
346+
347+
type CreateOAuthCredentialOpts struct {
348+
Type OAuthConnectionType `json:"type"`
349+
}
350+
351+
func (c *Client) CreateOAuthCredential(ctx context.Context, opts CreateOAuthCredentialOpts) (OAuthCredential, error) {
352+
c.once.Do(c.init)
353+
354+
data, err := c.JSONEncode(opts)
355+
if err != nil {
356+
return OAuthCredential{}, err
357+
}
358+
359+
endpoint := fmt.Sprintf("%s/oauth-credentials", c.Endpoint)
360+
req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(data))
361+
if err != nil {
362+
return OAuthCredential{}, err
363+
}
364+
req = req.WithContext(ctx)
365+
req.Header.Set("Content-Type", "application/json")
366+
req.Header.Set("Authorization", "Bearer "+c.APIKey)
367+
req.Header.Set("User-Agent", "workos-go/"+workos.Version)
368+
369+
res, err := c.HTTPClient.Do(req)
370+
if err != nil {
371+
return OAuthCredential{}, err
372+
}
373+
defer res.Body.Close()
374+
375+
if err = workos_errors.TryGetHTTPError(res); err != nil {
376+
return OAuthCredential{}, err
377+
}
378+
379+
var body OAuthCredential
380+
dec := json.NewDecoder(res.Body)
381+
err = dec.Decode(&body)
382+
return body, err
383+
}

pkg/oauthcredentials/oauthcredentials.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,11 @@ func UpdateOAuthCredential(
4040
) (OAuthCredential, error) {
4141
return DefaultClient.UpdateOAuthCredential(ctx, opts)
4242
}
43+
44+
// CreateOAuthCredential creates an OAuthCredential.
45+
func CreateOAuthCredential(
46+
ctx context.Context,
47+
opts CreateOAuthCredentialOpts,
48+
) (OAuthCredential, error) {
49+
return DefaultClient.CreateOAuthCredential(ctx, opts)
50+
}

0 commit comments

Comments
 (0)