diff --git a/app.go b/app.go index d51e2df..d79ea4a 100644 --- a/app.go +++ b/app.go @@ -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 +} diff --git a/app_test.go b/app_test.go new file mode 100644 index 0000000..257df3c --- /dev/null +++ b/app_test.go @@ -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: "chris@passage.id", + Channel: passage.EmailChannel, + } + + magicLink, err := psg.CreateMagicLink(createMagicLinkBody) + require.Nil(t, err) + assert.Equal(t, createMagicLinkBody.Email, magicLink.Identifier) +} diff --git a/passage_test.go b/passage_test.go new file mode 100644 index 0000000..a2521e3 --- /dev/null +++ b/passage_test.go @@ -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("%X@email.com", 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) +} diff --git a/user_test.go b/user_test.go index c17f275..f7fdf2d 100644 --- a/user_test.go +++ b/user_test.go @@ -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("%X@email.com", 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("%X@email.com", 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) @@ -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, "updatedEmail@123.com", 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) }