-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Psg 5154 introduce new method names (#98)
* user methods added * deprecate getApp * revert go version to 1.16 * feat: add method validate jwt * docs: deprecate ValidateAuthToken * docs: deprecation notice on apps * fix: error check * style: typo * test error check * feat: private app user * test: adjust app user * chore: remove unused err * docs: update change log * builds: mod tidy * revert: change log + version * docs: update deprecated comments * feat: appUser private * feat: app user constructor * feat: ValidateJWT * style: format * docs: move where deprecated text is in doc string
- Loading branch information
Showing
5 changed files
with
351 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package passage | ||
|
||
type PassageUser = User | ||
type appUser struct { | ||
app App | ||
} | ||
|
||
const ( | ||
UserIDDoesNotExist string = "passage User with ID \"%v\" does not exist" | ||
IdentifierDoesNotExist string = "passage User with Identifier \"%v\" does not exist" | ||
) | ||
|
||
func newAppUser(app App) *appUser { | ||
appUser := appUser{ | ||
app: app, | ||
} | ||
|
||
return &appUser | ||
} | ||
|
||
// Get gets a user using their userID | ||
// returns user on success, error on failure | ||
func (a *appUser) Get(userID string) (*PassageUser, error) { | ||
return a.app.GetUser(userID) | ||
} | ||
|
||
// GetByIdentifier gets a user using their identifier | ||
// returns user on success, error on failure | ||
func (a *appUser) GetByIdentifier(identifier string) (*PassageUser, error) { | ||
return a.app.GetUserByIdentifier(identifier) | ||
} | ||
|
||
// Activate activates a user using their userID | ||
// returns user on success, error on failure | ||
func (a *appUser) Activate(userID string) (*PassageUser, error) { | ||
return a.app.ActivateUser(userID) | ||
} | ||
|
||
// Deactivate deactivates a user using their userID | ||
// returns user on success, error on failure | ||
func (a *appUser) Deactivate(userID string) (*PassageUser, error) { | ||
return a.app.DeactivateUser(userID) | ||
} | ||
|
||
// Update receives an UpdateBody struct, updating the corresponding user's attribute(s) | ||
// returns user on success, error on failure | ||
func (a *appUser) Update(userID string, updateBody UpdateBody) (*PassageUser, error) { | ||
return a.app.UpdateUser(userID, updateBody) | ||
} | ||
|
||
// Delete deletes a user by their user string | ||
// returns true on success, false and error on failure (bool, err) | ||
func (a *appUser) Delete(userID string) (bool, error) { | ||
return a.app.DeleteUser(userID) | ||
} | ||
|
||
// Create receives a CreateUserBody struct, creating a user with provided values | ||
// returns user on success, error on failure | ||
func (a *appUser) Create(createUserBody CreateUserBody) (*PassageUser, error) { | ||
return a.app.CreateUser(createUserBody) | ||
} | ||
|
||
// ListDevices lists a user's devices | ||
// returns a list of devices on success, error on failure | ||
func (a *appUser) ListDevices(userID string) ([]WebAuthnDevices, error) { | ||
return a.app.ListUserDevices(userID) | ||
} | ||
|
||
// RevokeDevice gets a user using their userID | ||
// returns a true success, error on failure | ||
func (a *appUser) RevokeDevice(userID, deviceID string) (bool, error) { | ||
return a.app.RevokeUserDevice(userID, deviceID) | ||
} | ||
|
||
// Signout revokes a users refresh tokens | ||
// returns true on success, error on failure | ||
func (a *appUser) SignOut(userID string) (bool, error) { | ||
return a.app.SignOut(userID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
package passage_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/passageidentity/passage-go" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetInfo(t *testing.T) { | ||
psg, err := passage.New(PassageAppID, &passage.Config{ | ||
APIKey: PassageApiKey, | ||
}) | ||
require.Nil(t, err) | ||
|
||
user, err := psg.User.Get(PassageUserID) | ||
require.Nil(t, err) | ||
assert.Equal(t, PassageUserID, user.ID) | ||
} | ||
|
||
func TestGetInfoByIdentifier(t *testing.T) { | ||
psg, err := passage.New(PassageAppID, &passage.Config{ | ||
APIKey: PassageApiKey, | ||
}) | ||
require.Nil(t, err) | ||
|
||
createUserBody := passage.CreateUserBody{ | ||
Email: RandomEmail, | ||
} | ||
|
||
user, err := psg.User.Create(createUserBody) | ||
require.Nil(t, err) | ||
assert.Equal(t, RandomEmail, user.Email) | ||
|
||
userByIdentifier, err := psg.User.GetByIdentifier(RandomEmail) | ||
require.Nil(t, err) | ||
|
||
userById, err := psg.User.Get(user.ID) | ||
require.Nil(t, err) | ||
|
||
assert.Equal(t, user.ID, userById.ID) | ||
|
||
assert.Equal(t, userById, userByIdentifier) | ||
} | ||
|
||
func TestGetInfoByIdentifierEmailUpperCase(t *testing.T) { | ||
psg, err := passage.New(PassageAppID, &passage.Config{ | ||
APIKey: PassageApiKey, | ||
}) | ||
require.Nil(t, err) | ||
|
||
createUserBody := passage.CreateUserBody{ | ||
Email: RandomEmail, | ||
} | ||
|
||
user, err := psg.User.Create(createUserBody) | ||
require.Nil(t, err) | ||
assert.Equal(t, RandomEmail, user.Email) | ||
|
||
userByIdentifier, err := psg.User.GetByIdentifier(strings.ToUpper(RandomEmail)) | ||
require.Nil(t, err) | ||
|
||
assert.Equal(t, user.ID, userByIdentifier.ID) | ||
} | ||
|
||
func TestGetInfoByIdentifierPhone(t *testing.T) { | ||
psg, err := passage.New(PassageAppID, &passage.Config{ | ||
APIKey: PassageApiKey, | ||
}) | ||
require.Nil(t, err) | ||
|
||
phone := "+15005550007" | ||
createUserBody := passage.CreateUserBody{ | ||
Phone: phone, | ||
} | ||
|
||
user, err := psg.User.Create(createUserBody) | ||
require.Nil(t, err) | ||
assert.Equal(t, phone, user.Phone) | ||
|
||
userByIdentifier, err := psg.User.GetByIdentifier(phone) | ||
require.Nil(t, err) | ||
|
||
userById, err := psg.User.Get(user.ID) | ||
require.Nil(t, err) | ||
|
||
assert.Equal(t, user.ID, userById.ID) | ||
|
||
assert.Equal(t, userById, userByIdentifier) | ||
} | ||
|
||
func TestGetInfoByIdentifierError(t *testing.T) { | ||
psg, err := passage.New(PassageAppID, &passage.Config{ | ||
APIKey: PassageApiKey, | ||
}) | ||
require.Nil(t, err) | ||
|
||
_, err = psg.User.GetByIdentifier("[email protected]") | ||
require.NotNil(t, err) | ||
|
||
expectedMessage := "passage User with Identifier \"[email protected]\" does not exist" | ||
assert.Contains(t, err.Error(), expectedMessage) | ||
} | ||
|
||
func TestActivate(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) | ||
|
||
user, err := psg.User.Activate(PassageUserID) | ||
require.Nil(t, err) | ||
assert.Equal(t, PassageUserID, user.ID) | ||
assert.Equal(t, passage.StatusActive, user.Status) | ||
} | ||
func TestDeactivate(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) | ||
|
||
user, err := psg.User.Deactivate(PassageUserID) | ||
require.Nil(t, err) | ||
assert.Equal(t, PassageUserID, user.ID) | ||
assert.Equal(t, passage.StatusInactive, user.Status) | ||
} | ||
|
||
func TestUpdate(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) | ||
|
||
updateBody := passage.UpdateBody{ | ||
Email: "[email protected]", | ||
Phone: "+15005550012", | ||
UserMetadata: map[string]interface{}{ | ||
"example1": "123", | ||
}, | ||
} | ||
user, err := psg.User.Update(PassageUserID, updateBody) | ||
require.Nil(t, err) | ||
assert.Equal(t, "[email protected]", user.Email) | ||
assert.Equal(t, "+15005550012", user.Phone) | ||
assert.Equal(t, "123", user.UserMetadata["example1"]) | ||
|
||
secondUpdateBody := passage.UpdateBody{ | ||
Email: "[email protected]", | ||
Phone: "+15005550012", | ||
UserMetadata: map[string]interface{}{ | ||
"example1": "456", | ||
}, | ||
} | ||
user, err = psg.User.Update(PassageUserID, secondUpdateBody) | ||
require.Nil(t, err) | ||
assert.Equal(t, "[email protected]", user.Email) | ||
assert.Equal(t, "+15005550012", user.Phone) | ||
assert.Equal(t, "456", user.UserMetadata["example1"]) | ||
} | ||
|
||
func TestCreate(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) | ||
|
||
createUserBody := passage.CreateUserBody{ | ||
Email: RandomEmail, | ||
} | ||
|
||
user, err := psg.User.Create(createUserBody) | ||
require.Nil(t, err) | ||
assert.Equal(t, RandomEmail, user.Email) | ||
|
||
CreatedUser = *user | ||
} | ||
|
||
func TestCreateWithMetadata(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) | ||
|
||
createUserBody := passage.CreateUserBody{ | ||
Email: fmt.Sprintf("1%v", RandomEmail), | ||
UserMetadata: map[string]interface{}{ | ||
"example1": "test", | ||
}, | ||
} | ||
|
||
user, err := psg.User.Create(createUserBody) | ||
require.Nil(t, err) | ||
assert.Equal(t, "1"+RandomEmail, user.Email) | ||
assert.Equal(t, "test", user.UserMetadata["example1"].(string)) | ||
|
||
CreatedUser = *user | ||
} | ||
|
||
func TestDelete(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) | ||
|
||
result, err := psg.User.Delete(CreatedUser.ID) | ||
require.Nil(t, err) | ||
assert.Equal(t, result, true) | ||
} | ||
|
||
func TestListDevices(t *testing.T) { | ||
psg, err := passage.New(PassageAppID, &passage.Config{ | ||
APIKey: PassageApiKey, | ||
}) | ||
require.Nil(t, err) | ||
|
||
devices, err := psg.User.ListDevices(PassageUserID) | ||
require.Nil(t, err) | ||
assert.Equal(t, 2, len(devices)) | ||
} | ||
|
||
// NOTE RevokeUserDevice is not tested because it is impossible to spoof webauthn to create a device to then revoke | ||
|
||
func TestSignOut(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) | ||
|
||
result, err := psg.User.SignOut(PassageUserID) | ||
require.Nil(t, err) | ||
assert.Equal(t, result, true) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.