Skip to main content

clerk

import "github.com/Ask-Atlas/AskAtlas/api/internal/clerk"

Package clerk provides integration with the Clerk authentication and user management webhook events.

Index

Constants

Constants representing metadata keys used for mapping Clerk data.

const (
MetadataKeyProfileImageURL = "profile_image_url"
MetadataKeyImageURL = "image_url"
MetadataKeyHasImage = "has_image"
MetadataKeyClerkCreatedAt = "clerk_created_at"
MetadataKeyClerkUpdatedAt = "clerk_updated_at"
MetadataKeyLastSignInAt = "last_sign_in_at"
MetadataKeyLastActiveAt = "last_active_at"
)

Variables

ErrUserNotFound is returned when attempting to act on a Clerk user that does not exist locally.

var ErrUserNotFound = errors.New("user not found")

func ToUpsertUserPayload

func ToUpsertUserPayload(clerkUser ClerkUser) (user.UpsertUserPayload, error)

ToUpsertUserPayload converts a ClerkUser webhook payload into an application-specific user UpsertUserPayload. It extracts the primary email and maps various specific Clerk fields into a generic metadata map.

type BaseEvent

BaseEvent provides the common fields shared by all Clerk webhook events.

type BaseEvent struct {
Object string `json:"object"`
Type EventType `json:"type"`
Timestamp int64 `json:"timestamp"`
EventAttributes EventAttributes `json:"data"`
}

func (BaseEvent) GetObject

func (e BaseEvent) GetObject() string

GetObject returns the object type of the event.

func (BaseEvent) GetTimestamp

func (e BaseEvent) GetTimestamp() int64

GetTimestamp returns the Unix timestamp of the event.

func (BaseEvent) GetType

func (e BaseEvent) GetType() EventType

GetType returns the event type.

type ClerkUser

User represents a full Clerk user object

type ClerkUser struct {
ID string `json:"id"`
Object string `json:"object"`
ExternalID *string `json:"external_id"`
PrimaryEmailAddressID *string `json:"primary_email_address_id"`
PrimaryPhoneNumberID *string `json:"primary_phone_number_id"`
PrimaryWeb3WalletID *string `json:"primary_web3_wallet_id"`
Username *string `json:"username"`
FirstName *string `json:"first_name"`
LastName *string `json:"last_name"`
ProfileImageURL string `json:"profile_image_url"`
ImageURL string `json:"image_url"`
HasImage bool `json:"has_image"`
PublicMetadata map[string]any `json:"public_metadata"`
PrivateMetadata map[string]any `json:"private_metadata"`
UnsafeMetadata map[string]any `json:"unsafe_metadata"`
EmailAddresses []EmailAddress `json:"email_addresses"`
PhoneNumbers []any `json:"phone_numbers"`
Web3Wallets []any `json:"web3_wallets"`
ExternalAccounts []any `json:"external_accounts"`
SamlAccounts []any `json:"saml_accounts"`
Passkeys []any `json:"passkeys"`
EnterpriseAccounts []any `json:"enterprise_accounts"`
PasswordEnabled bool `json:"password_enabled"`
TwoFactorEnabled bool `json:"two_factor_enabled"`
TOTPEnabled bool `json:"totp_enabled"`
BackupCodeEnabled bool `json:"backup_code_enabled"`
MFAEnabledAt *int64 `json:"mfa_enabled_at"`
MFADisabledAt *int64 `json:"mfa_disabled_at"`
CreateOrganizationEnabled bool `json:"create_organization_enabled"`
CreateOrganizationsLimit *int `json:"create_organizations_limit"`
DeleteSelfEnabled bool `json:"delete_self_enabled"`
Banned bool `json:"banned"`
Locked bool `json:"locked"`
LockoutExpiresInSeconds *int `json:"lockout_expires_in_seconds"`
VerificationAttemptsRemaining *int `json:"verification_attempts_remaining"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
LastSignInAt *int64 `json:"last_sign_in_at"`
LastActiveAt *int64 `json:"last_active_at"`
LegalAcceptedAt *int64 `json:"legal_accepted_at"`
}

func (*ClerkUser) GetPrimaryOrFirstEmailAddress

func (cl *ClerkUser) GetPrimaryOrFirstEmailAddress() *EmailAddress

Clerk dashboard is setup to require an email however it is possible to not set a primary email address so we will return the first email address if the primary is not set

type DeletedUser

DeletedUser represents a tombstone object sent by Clerk when a user is deleted.

type DeletedUser struct {
ID string `json:"id"`
Object string `json:"object"`
Deleted bool `json:"deleted"`
}

type EmailAddress

EmailAddress represents a user's email address object from Clerk.

type EmailAddress struct {
ID string `json:"id"`
EmailAddress string `json:"email_address"`
Reserved bool `json:"reserved"`
Verification Verification `json:"verification"`
LinkedTo []any `json:"linked_to"`
Object string `json:"object"`
}

type Event

Event defines the standard interface for all Clerk webhook events.

type Event interface {
GetType() EventType
GetTimestamp() int64
GetObject() string
}

func ParseWebhookEvent

func ParseWebhookEvent(event []byte) (Event, error)

ParseWebhookEvent parses the raw JSON byte array into a typed Clerk Event. It inspects the event type and returns the corresponding concrete event struct.

type EventAttributes

EventAttributes contains the contextual metadata for a Clerk event.

type EventAttributes struct {
HTTPRequest HTTPRequest `json:"request"`
}

type EventType

EventType is a type of webhook event.

type EventType string
const (
UserCreated EventType = "user.created"
UserUpdated EventType = "user.updated"
UserDeleted EventType = "user.deleted"
)

type HTTPRequest

HTTPRequest contains the request information that triggered the event.

type HTTPRequest struct {
ClientIP string `json:"client_ip"`
UserAgent string `json:"user_agent"`
}

type UserCreatedEvent

UserCreatedEvent represents the "user.created" webhook event payload.

type UserCreatedEvent struct {
BaseEvent
Data ClerkUser `json:"data"`
}

type UserDeletedEvent

UserDeletedEvent represents the "user.deleted" webhook event payload.

type UserDeletedEvent struct {
BaseEvent
Data DeletedUser `json:"data"`
}

type UserService

UserService defines the required capabilities expected from the local user service.

type UserService interface {
UpsertClerkUser(ctx context.Context, arg user.UpsertUserPayload) (user.User, error)
SoftDeleteUserByClerkID(ctx context.Context, clerkID string) error
}

type UserUpdateEvent

UserUpdateEvent represents the "user.updated" webhook event payload.

type UserUpdateEvent struct {
BaseEvent
Data ClerkUser `json:"data"`
}

type Verification

Verification describes the status of an external verification strategy.

type Verification struct {
Status string `json:"status"`
Strategy string `json:"strategy"`
Attempts *int `json:"attempts"`
ExpireAt *int64 `json:"expire_at"`
}

Generated by gomarkdoc