Skip to content

Commit

Permalink
feat!: remove deprecated code and rename structs and files (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ctran88 authored Dec 30, 2024
1 parent 20c0f1d commit 6445861
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 1,064 deletions.
97 changes: 17 additions & 80 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package passage

import (
"context"
"errors"
"fmt"

"github.com/lestrrat-go/jwx/v2/jwk"
"net/http"
)

const jwksUrl = "https://auth.passage.id/v1/apps/%v/.well-known/jwks.json"

type Passage = App

// Config holds the configuration for the Passage SDK.
Expand All @@ -24,13 +20,8 @@ type Config struct {
//
// Deprecated: will be renamed to `Passage` in v2.
type App struct {
// Deprecated: will be removed in v2.
ID string
// Deprecated: will be removed in v2.
Config *Config
Auth *auth
User *user
client *ClientWithResponses
Auth *auth
User *user
}

// New creates a new Passage instance.
Expand All @@ -43,23 +34,13 @@ func New(appID string, config *Config) (*Passage, error) {

client, err := NewClientWithResponses(
"https://api.passage.id/v1/",
withPassageVersion,
withPassageVersion(),
withAPIKey(config.APIKey),
)
if err != nil {
return nil, err
}

url := fmt.Sprintf(jwksUrl, appID)
cache := jwk.NewCache(context.Background())
if err := cache.Register(url); err != nil {
return nil, err
}

if _, err = cache.Refresh(context.Background(), url); err != nil {
return nil, Error{Message: "failed to fetch jwks"}
}

auth, err := newAuth(appID, client)
if err != nil {
return nil, err
Expand All @@ -68,67 +49,23 @@ func New(appID string, config *Config) (*Passage, error) {
user := newUser(appID, client)

return &App{
ID: appID,
Config: config,
client: client,
User: user,
Auth: auth,
User: user,
Auth: auth,
}, nil
}

// GetApp fetches the Passage app info.
//
// Deprecated: will be removed in v2.
func (a *App) GetApp() (*AppInfo, error) {
res, err := a.client.GetAppWithResponse(context.Background(), a.ID)
if err != nil {
return nil, Error{Message: "network error: failed to get Passage App Info"}
}

if res.JSON200 != nil {
return &res.JSON200.App, nil
}

var errorText string
var errorCode string
switch {
case res.JSON401 != nil:
errorText = res.JSON401.Error
errorCode = string(res.JSON401.Code)
case res.JSON404 != nil:
errorText = res.JSON404.Error
errorCode = string(res.JSON404.Code)
case res.JSON500 != nil:
errorText = res.JSON500.Error
errorCode = string(res.JSON500.Code)
}

return nil, Error{
Message: "failed to get Passage App Info",
StatusCode: res.StatusCode(),
StatusText: res.Status(),
ErrorText: errorText,
ErrorCode: errorCode,
}
func withPassageVersion() ClientOption {
return WithRequestEditorFn(func(_ context.Context, req *http.Request) error {
req.Header.Set("Passage-Version", fmt.Sprintf("passage-go %s", version))
return nil
})
}

// CreateMagicLink creates a Magic Link for your app.
//
// Deprecated: use `Passage.Auth.CreateMagicLinkWithEmail`, `Passage.Auth.CreateMagicLinkWithPhone`,
// or `Passage.Auth.CreateMagicLinkWithUser` instead.
func (a *App) CreateMagicLink(createMagicLinkBody CreateMagicLinkBody) (*MagicLink, error) {
magicLink, err := a.Auth.createMagicLink(createMagicLinkBody, nil)
if err != nil {
var passageError PassageError
if errors.As(err, &passageError) {
return magicLink, Error{
ErrorText: passageError.Message,
ErrorCode: passageError.ErrorCode,
Message: passageError.Message,
StatusCode: passageError.StatusCode,
}
func withAPIKey(apiKey string) ClientOption {
return WithRequestEditorFn(func(_ context.Context, req *http.Request) error {
if apiKey != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey))
}
}

return magicLink, err
return nil
})
}
32 changes: 0 additions & 32 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,9 @@ import (
"testing"

"github.com/passageidentity/passage-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCreateMagicLink(t *testing.T) {
psg, err := passage.New(PassageAppID, &passage.Config{
APIKey: PassageApiKey, // An API_KEY environment variable is required for testing.
})
require.Nil(t, err)

createMagicLinkBody := passage.CreateMagicLinkBody{
Email: "[email protected]",
Channel: passage.EmailChannel,
TTL: 12,
Type: passage.LoginType,
}

magicLink, err := psg.CreateMagicLink(createMagicLinkBody)
require.Nil(t, err)
assert.Equal(t, createMagicLinkBody.Email, magicLink.Identifier)
assert.Equal(t, createMagicLinkBody.TTL, magicLink.TTL)
}

func TestGetApp(t *testing.T) {
psg, err := passage.New(PassageAppID, &passage.Config{
APIKey: PassageApiKey, // An API_KEY environment variable is required for testing.
})
require.Nil(t, err)

appInfo, err := psg.GetApp()
assert.Nil(t, err)
assert.Equal(t, PassageAppID, appInfo.ID)

}

// should be run with the -race flag, i.e. `go test -race -run TestAppJWKSCacheWriteConcurrency`
func TestAppJWKSCacheWriteConcurrency(t *testing.T) {
goRoutineCount := 2
Expand Down
219 changes: 0 additions & 219 deletions app_user.go

This file was deleted.

Loading

0 comments on commit 6445861

Please sign in to comment.