Skip to content

Commit

Permalink
Add events and API changes for invitations and Magic Auth (#332)
Browse files Browse the repository at this point in the history
* Add events and API changes for invitations and Magic Auth

* Update deprecation message

* Update comment
  • Loading branch information
blairworkos authored May 3, 2024
1 parent 4c919b6 commit 4fcee1b
Show file tree
Hide file tree
Showing 5 changed files with 432 additions and 97 deletions.
2 changes: 2 additions & 0 deletions pkg/events/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
OrganizationMembershipUpdated = "organization_membership.updated"
OrganizationMembershipRemoved = "organization_membership.removed" // Deprecated: use OrganizationMembershipDeleted instead
SessionCreated = "session.created"
InvitationCreated = "invitation.created"
MagicAuthCreated = "magic_auth.created"
)

// Client represents a client that performs Event requests to the WorkOS API.
Expand Down
112 changes: 101 additions & 11 deletions pkg/usermanagement/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,27 @@ const (
)

type Invitation struct {
ID string `json:"id"`
Email string `json:"email"`
State InvitationState `json:"state"`
AcceptedAt string `json:"accepted_at,omitempty"`
RevokedAt string `json:"revoked_at,omitempty"`
Token string `json:"token"`
OrganizationID string `json:"organization_id,omitempty"`
ExpiresAt string `json:"expires_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ID string `json:"id"`
Email string `json:"email"`
State InvitationState `json:"state"`
AcceptedAt string `json:"accepted_at,omitempty"`
RevokedAt string `json:"revoked_at,omitempty"`
Token string `json:"token"`
AcceptInvitationUrl string `json:"accept_invitation_url`
OrganizationID string `json:"organization_id,omitempty"`
ExpiresAt string `json:"expires_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}

type MagicAuth struct {
ID string `json:"id"`
UserId string `json:"user_id"`
Email string `json:"email"`
ExpiresAt string `json:"expires_at"`
Code string `json:"code"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}

// Organization contains data about a particular Organization.
Expand Down Expand Up @@ -334,6 +345,16 @@ type UserResponse struct {
User User `json:"user"`
}

type GetMagicAuthOpts struct {
MagicAuth string
}

type CreateMagicAuthOpts struct {
// The email address the one-time code is for.
Email string `json:"email"`
InvitationToken string `json:"invitation_token,omitempty"`
}

type SendMagicAuthCodeOpts struct {
// The email address the one-time code will be sent to.
Email string `json:"email"`
Expand Down Expand Up @@ -1291,7 +1312,76 @@ func (c *Client) ResetPassword(ctx context.Context, opts ResetPasswordOpts) (Use
return body, err
}

// SendMagicAuthCode creates a one-time Magic Auth code and emails it to the user.
// GetMagicAuth fetches a Magic Auth object by its ID.
func (c *Client) GetMagicAuth(ctx context.Context, opts GetMagicAuthOpts) (MagicAuth, error) {
endpoint := fmt.Sprintf("%s/user_management/magic_auth/%s", c.Endpoint, opts.MagicAuth)

req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return MagicAuth{}, err
}
req = req.WithContext(ctx)
req.Header.Set("User-Agent", "workos-go/"+workos.Version)
req.Header.Set("Authorization", "Bearer "+c.APIKey)
req.Header.Set("Content-Type", "application/json")

res, err := c.HTTPClient.Do(req)
if err != nil {
return MagicAuth{}, err
}
defer res.Body.Close()

if err = workos_errors.TryGetHTTPError(res); err != nil {
return MagicAuth{}, err
}

var body MagicAuth
dec := json.NewDecoder(res.Body)
err = dec.Decode(&body)

return body, err
}

// CreateMagicAuth creates a one-time Magic Auth code that can be emailed it to the user.
func (c *Client) CreateMagicAuth(ctx context.Context, opts CreateMagicAuthOpts) (MagicAuth, error) {
endpoint := fmt.Sprintf("%s/user_management/magic_auth", c.Endpoint)

data, err := json.Marshal(opts)
if err != nil {
return MagicAuth{}, err
}

req, err := http.NewRequest(
http.MethodPost,
endpoint,
bytes.NewBuffer(data),
)
if err != nil {
return MagicAuth{}, err
}
req = req.WithContext(ctx)
req.Header.Set("User-Agent", "workos-go/"+workos.Version)
req.Header.Set("Authorization", "Bearer "+c.APIKey)
req.Header.Set("Content-Type", "application/json")

res, err := c.HTTPClient.Do(req)
if err != nil {
return MagicAuth{}, err
}
defer res.Body.Close()

if err = workos_errors.TryGetHTTPError(res); err != nil {
return MagicAuth{}, err
}

var body MagicAuth
dec := json.NewDecoder(res.Body)
err = dec.Decode(&body)

return body, err
}

// Deprecated: Use CreateMagicAuth instead. This method will be removed in a future major version.
func (c *Client) SendMagicAuthCode(ctx context.Context, opts SendMagicAuthCodeOpts) error {
endpoint := fmt.Sprintf(
"%s/user_management/magic_auth/send",
Expand Down
Loading

0 comments on commit 4fcee1b

Please sign in to comment.