Skip to content

Commit

Permalink
Merge pull request #8 from passageidentity/users/cl/smart-links
Browse files Browse the repository at this point in the history
add create magic-link functionality
  • Loading branch information
Chris Loper authored Mar 3, 2022
2 parents b39181e + a26c22c commit d517f2e
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 52 deletions.
53 changes: 53 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,56 @@ func getPublicKey(appID string) (*rsa.PublicKey, error) {

return publicKey, nil
}

type ChannelType string

const (
EmailChannel ChannelType = "email"
PhoneChannel ChannelType = "phone"
)

type CreateMagicLinkBody struct {
UserID string `json:"user_id"`
Email string `json:"email"`
Phone string `json:"phone"`
Channel ChannelType `json:"channel"`
Send bool `json:"send"`
MagicLinkPath string `json:"magic_link_path"`
RedirectURL string `json:"redirect_url"`
}

type MagicLink struct {
ID string `json:"id"`
Secret string `json:"secret"`
Activated bool `json:"activated"`
UserID string `json:"user_id"`
AppID string `json:"app_id"`
Identifier string `json:"identifier"`
Type string `json:"type"`
RedirectURL string `json:"redirect_url"`
URL string `json:"url"`
}

// CreateMagicLink receives a CreateMagicLinkBody struct, creating a magic link with provided values
// returns MagicLink on success, error on failure
func (a *App) CreateMagicLink(createMagicLinkBody CreateMagicLinkBody) (*MagicLink, error) {

type respMagicLink struct {
MagicLink MagicLink `json:"magic_link"`
}
var magicLinkResp respMagicLink

response, err := resty.New().R().
SetResult(&magicLinkResp).
SetBody(&createMagicLinkBody).
SetAuthToken(a.Config.APIKey).
Post(fmt.Sprintf("https://api.passage.id/v1/apps/%v/magic-links/", a.ID))
if err != nil {
return nil, errors.New("network error: could not create Passage Magic Link")
}
if response.StatusCode() != http.StatusCreated {
return nil, fmt.Errorf("failed to create Passage Magic Link. Http Status: %v. Response: %v", response.StatusCode(), response.String())
}

return &magicLinkResp.MagicLink, nil
}
25 changes: 25 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package passage_test

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,
}

magicLink, err := psg.CreateMagicLink(createMagicLinkBody)
require.Nil(t, err)
assert.Equal(t, createMagicLinkBody.Email, magicLink.Identifier)
}
40 changes: 40 additions & 0 deletions passage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package passage_test

import (
"crypto/rand"
"fmt"
"os"
"testing"

"github.com/joho/godotenv"
"github.com/passageidentity/passage-go"
)

var (
PassageAppID string
PassageApiKey string
PassageUserID string
RandomEmail = generateRandomEmail(14)
CreatedUser passage.User
)

func generateRandomEmail(prefixLength int) string {
n := prefixLength
randomChars := make([]byte, n)
if _, err := rand.Read(randomChars); err != nil {
panic(err)
}
email := fmt.Sprintf("%[email protected]", randomChars)
return email
}

func TestMain(m *testing.M) {
godotenv.Load(".env")

PassageAppID = os.Getenv("PASSAGE_APP_ID")
PassageApiKey = os.Getenv("PASSAGE_API_KEY")
PassageUserID = os.Getenv("PASSAGE_USER_ID")

exitVal := m.Run()
os.Exit(exitVal)
}
100 changes: 48 additions & 52 deletions user_test.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,76 @@
package passage_test

import (
"crypto/rand"
"fmt"
"os"
"testing"

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

// these vars are required as environment variables for testing
var (
passageAppID string
passageApiKey string
passageUserID string
randomEmail = generateRandomEmail(14)
createdUser passage.User
)

func generateRandomEmail(prefixLength int) string {
n := prefixLength
randomChars := make([]byte, n)
if _, err := rand.Read(randomChars); err != nil {
panic(err)
}
email := fmt.Sprintf("%[email protected]", randomChars)
return email
}

func TestMain(t *testing.T) {
godotenv.Load(".env")

passageAppID = os.Getenv("PASSAGE_APP_ID")
passageApiKey = os.Getenv("PASSAGE_API_KEY")
passageUserID = os.Getenv("PASSAGE_USER_ID")
}
// var (
// passageAppID string
// passageApiKey string
// passageUserID string
// randomEmail = generateRandomEmail(14)
// createdUser passage.User
// )

// func generateRandomEmail(prefixLength int) string {
// n := prefixLength
// randomChars := make([]byte, n)
// if _, err := rand.Read(randomChars); err != nil {
// panic(err)
// }
// email := fmt.Sprintf("%[email protected]", randomChars)
// return email
// }

// func TestMain(t *testing.T) {
// godotenv.Load(".env")

// passageAppID = os.Getenv("PASSAGE_APP_ID")
// passageApiKey = os.Getenv("PASSAGE_API_KEY")
// passageUserID = os.Getenv("PASSAGE_USER_ID")
// }

func TestGetUserInfo(t *testing.T) {
psg, err := passage.New(passageAppID, &passage.Config{
APIKey: passageApiKey,
psg, err := passage.New(PassageAppID, &passage.Config{
APIKey: PassageApiKey,
})
require.Nil(t, err)

user, err := psg.GetUser(passageUserID)
user, err := psg.GetUser(PassageUserID)
require.Nil(t, err)
assert.Equal(t, passageUserID, user.ID)
assert.Equal(t, PassageUserID, user.ID)
}
func TestActivateUser(t *testing.T) {
psg, err := passage.New(passageAppID, &passage.Config{
APIKey: passageApiKey, // An API_KEY environment variable is required for testing.
psg, err := passage.New(PassageAppID, &passage.Config{
APIKey: PassageApiKey, // An API_KEY environment variable is required for testing.
})
require.Nil(t, err)

user, err := psg.ActivateUser(passageUserID)
user, err := psg.ActivateUser(PassageUserID)
require.Nil(t, err)
assert.Equal(t, passageUserID, user.ID)
assert.Equal(t, PassageUserID, user.ID)
assert.Equal(t, passage.StatusActive, user.Status)
}
func TestDeactivateUser(t *testing.T) {
psg, err := passage.New(passageAppID, &passage.Config{
APIKey: passageApiKey, // An API_KEY environment variable is required for testing.
psg, err := passage.New(PassageAppID, &passage.Config{
APIKey: PassageApiKey, // An API_KEY environment variable is required for testing.
})
require.Nil(t, err)

user, err := psg.DeactivateUser(passageUserID)
user, err := psg.DeactivateUser(PassageUserID)
require.Nil(t, err)
assert.Equal(t, passageUserID, user.ID)
assert.Equal(t, PassageUserID, user.ID)
assert.Equal(t, passage.StatusInactive, user.Status)
}

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

Expand All @@ -83,36 +79,36 @@ func TestUpdateUser(t *testing.T) {
Phone: "+15005550006",
}

user, err := psg.UpdateUser(passageUserID, updateBody)
user, err := psg.UpdateUser(PassageUserID, updateBody)
require.Nil(t, err)
assert.Equal(t, "[email protected]", user.Email)
assert.Equal(t, "+15005550006", user.Phone)
}

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

createUserBody := passage.CreateUserBody{
Email: randomEmail,
Email: RandomEmail,
}

user, err := psg.CreateUser(createUserBody)
require.Nil(t, err)
assert.Equal(t, randomEmail, user.Email)
assert.Equal(t, RandomEmail, user.Email)

createdUser = *user
CreatedUser = *user
}

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

result, err := psg.DeleteUser(createdUser.ID)
result, err := psg.DeleteUser(CreatedUser.ID)
require.Nil(t, err)
assert.Equal(t, result, true)
}

0 comments on commit d517f2e

Please sign in to comment.