Skip to content

Commit

Permalink
add sign-in token api (#340)
Browse files Browse the repository at this point in the history
* add sign in token api

* bad copy paste ):

* address PR feedback

- rename package
- use pointer for URL as it can be null
- remove references to 'session'
  • Loading branch information
PhilippBuschhaus authored Oct 23, 2024
1 parent 12b399e commit d818ba3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
13 changes: 13 additions & 0 deletions sign_in_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package clerk

type SignInToken struct {
APIResource
Object string `json:"object"`
ID string `json:"id"`
Status string `json:"status"`
UserID string `json:"user_id"`
Token string `json:"token,omitempty"`
URL *string `json:"url,omitempty"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
25 changes: 25 additions & 0 deletions signintoken/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions signintoken/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Package signintoken provides the Sign-In Token API.
package signintoken

import (
"context"
"net/http"

"github.com/clerk/clerk-sdk-go/v2"
)

//go:generate go run ../cmd/gen/main.go

const path = "/sign_in_tokens"

// Client is used to invoke the Sign-In Token API.
type Client struct {
Backend clerk.Backend
}

func NewClient(config *clerk.ClientConfig) *Client {
return &Client{
Backend: clerk.NewBackend(&config.BackendConfig),
}
}

type CreateParams struct {
clerk.APIParams
UserID *string `json:"user_id,omitempty"`
ExpiresInSeconds *int64 `json:"expires_in_seconds,omitempty"`
}

// Create creates a new sign-in token.
func (c *Client) Create(ctx context.Context, params *CreateParams) (*clerk.SignInToken, error) {
req := clerk.NewAPIRequest(http.MethodPost, path)
req.SetParams(params)
token := &clerk.SignInToken{}
err := c.Backend.Call(ctx, req, token)
return token, err
}

// Revoke revokes a pending sign-in token.
func (c *Client) Revoke(ctx context.Context, id string) (*clerk.SignInToken, error) {
token := &clerk.SignInToken{}
path, err := clerk.JoinPath(path, id, "revoke")
if err != nil {
return token, err
}
req := clerk.NewAPIRequest(http.MethodPost, path)
err = c.Backend.Call(ctx, req, token)
return token, err
}
55 changes: 55 additions & 0 deletions signintoken/sign_in_tokens_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package signintoken

import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"

"github.com/clerk/clerk-sdk-go/v2"
"github.com/clerk/clerk-sdk-go/v2/clerktest"
"github.com/stretchr/testify/require"
)

func TestSignInTokenTokenCreate(t *testing.T) {
userID := "usr_123"
id := "sign_123"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
In: json.RawMessage(fmt.Sprintf(`{"user_id":"%s"}`, userID)),
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","user_id":"%s"}`, id, userID)),
Path: "/v1/sign_in_tokens",
Method: http.MethodPost,
},
},
}))

signInToken, err := Create(context.Background(), &CreateParams{
UserID: clerk.String(userID),
})
require.NoError(t, err)
require.Equal(t, id, signInToken.ID)
require.Equal(t, userID, signInToken.UserID)
}

func TestSignInTokenRevoke(t *testing.T) {
id := "sign_456"
clerk.SetBackend(clerk.NewBackend(&clerk.BackendConfig{
HTTPClient: &http.Client{
Transport: &clerktest.RoundTripper{
T: t,
Out: json.RawMessage(fmt.Sprintf(`{"id":"%s","status":"revoked"}`, id)),
Path: fmt.Sprintf("/v1/sign_in_tokens/%s/revoke", id),
Method: http.MethodPost,
},
},
}))

signInToken, err := Revoke(context.Background(), id)
require.NoError(t, err)
require.Equal(t, id, signInToken.ID)
require.Equal(t, "revoked", signInToken.Status)
}

0 comments on commit d818ba3

Please sign in to comment.