Account (Set|Change)Passsword (#267)
This commit is contained in:
1 parent
4655f58365
commit
e14f968fc3
26 files changed
+2078
-518
No files matched your search
@@ -75,6 +75,8 @@ func TestHandleImplementedPaths(t *testing.T) {
|
||||
|
||||
// custom methods
|
||||
"v1/scopes/someid/auth-methods/someid:authenticate",
|
||||
"v1/scopes/someid/auth-methods/someid/accounts/someid:set-password",
|
||||
"v1/scopes/someid/auth-methods/someid/accounts/someid:change-password",
|
||||
"v1/scopes/someid/roles/someid:add-principals",
|
||||
"v1/scopes/someid/roles/someid:set-principals",
|
||||
"v1/scopes/someid/roles/someid:remove-principals",
|
||||
|
||||
@@ -134,6 +134,40 @@ func (s Service) DeleteAccount(ctx context.Context, req *pbs.DeleteAccountReques
|
||||
return &pbs.DeleteAccountResponse{Existed: existed}, nil
|
||||
}
|
||||
|
||||
// ChangePassword implements the interface pbs.AccountServiceServer.
|
||||
func (s Service) ChangePassword(ctx context.Context, req *pbs.ChangePasswordRequest) (*pbs.ChangePasswordResponse, error) {
|
||||
authResults := auth.Verify(ctx)
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
if err := validateChangePasswordRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u, err := s.changePasswordInRepo(ctx, req.GetId(), req.GetVersion(), req.GetOldPassword(), req.GetNewPassword())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
return &pbs.ChangePasswordResponse{Item: u}, nil
|
||||
}
|
||||
|
||||
// SetPassword implements the interface pbs.AccountServiceServer.
|
||||
func (s Service) SetPassword(ctx context.Context, req *pbs.SetPasswordRequest) (*pbs.SetPasswordResponse, error) {
|
||||
authResults := auth.Verify(ctx)
|
||||
if authResults.Error != nil {
|
||||
return nil, authResults.Error
|
||||
}
|
||||
if err := validateSetPasswordRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u, err := s.setPasswordInRepo(ctx, req.GetId(), req.GetVersion(), req.GetPassword())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
return &pbs.SetPasswordResponse{Item: u}, nil
|
||||
}
|
||||
|
||||
func (s Service) getFromRepo(ctx context.Context, id string) (*pb.Account, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
@@ -158,9 +192,6 @@ func (s Service) createInRepo(ctx context.Context, authMethodId string, item *pb
|
||||
return nil, status.Errorf(codes.InvalidArgument, "Provided attributes don't match expected format.")
|
||||
}
|
||||
opts := []password.Option{password.WithLoginName(pwAttrs.GetLoginName())}
|
||||
if pwAttrs.GetPassword() != nil {
|
||||
opts = append(opts, password.WithPassword(pwAttrs.GetPassword().GetValue()))
|
||||
}
|
||||
if item.GetName() != nil {
|
||||
opts = append(opts, password.WithName(item.GetName().GetValue()))
|
||||
}
|
||||
@@ -175,7 +206,12 @@ func (s Service) createInRepo(ctx context.Context, authMethodId string, item *pb
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := repo.CreateAccount(ctx, a)
|
||||
|
||||
var createOpts []password.Option
|
||||
if pwAttrs.GetPassword() != nil {
|
||||
createOpts = append(createOpts, password.WithPassword(pwAttrs.GetPassword().GetValue()))
|
||||
}
|
||||
out, err := repo.CreateAccount(ctx, a, createOpts...)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to create user: %v.", err)
|
||||
}
|
||||
@@ -260,6 +296,33 @@ func (s Service) listFromRepo(ctx context.Context, authMethodId string) ([]*pb.A
|
||||
return outUl, nil
|
||||
}
|
||||
|
||||
func (s Service) changePasswordInRepo(ctx context.Context, id string, version uint32, oldPassword, newPassword string) (*pb.Account, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := repo.ChangePassword(ctx, id, oldPassword, newPassword, version)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to change password: %v.", err)
|
||||
}
|
||||
if out == nil {
|
||||
return nil, status.Errorf(codes.PermissionDenied, "Failed to change password.")
|
||||
}
|
||||
return toProto(out)
|
||||
}
|
||||
|
||||
func (s Service) setPasswordInRepo(ctx context.Context, id string, version uint32, password string) (*pb.Account, error) {
|
||||
repo, err := s.repoFn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out, err := repo.SetPassword(ctx, id, password, version)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "Unable to set password: %v.", err)
|
||||
}
|
||||
return toProto(out)
|
||||
}
|
||||
|
||||
func toProto(in *password.Account) (*pb.Account, error) {
|
||||
out := pb.Account{
|
||||
Id: in.GetPublicId(),
|
||||
@@ -363,6 +426,46 @@ func validateListRequest(req *pbs.ListAccountsRequest) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateChangePasswordRequest(req *pbs.ChangePasswordRequest) error {
|
||||
badFields := map[string]string{}
|
||||
if !validId(req.GetId(), password.AccountPrefix+"_") {
|
||||
badFields["id"] = "Improperly formatted identifier."
|
||||
}
|
||||
if !validId(req.GetAuthMethodId(), password.AuthMethodPrefix+"_") {
|
||||
badFields["auth_method_id"] = "Invalid formatted identifier."
|
||||
}
|
||||
if req.GetVersion() == 0 {
|
||||
badFields["version"] = "Existing resource version is required for an update."
|
||||
}
|
||||
if req.GetNewPassword() == "" {
|
||||
badFields["new_password"] = "This is a required field."
|
||||
}
|
||||
if req.GetOldPassword() == "" {
|
||||
badFields["old_password"] = "This is a required field."
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateSetPasswordRequest(req *pbs.SetPasswordRequest) error {
|
||||
badFields := map[string]string{}
|
||||
if !validId(req.GetId(), password.AccountPrefix+"_") {
|
||||
badFields["id"] = "Improperly formatted identifier."
|
||||
}
|
||||
if !validId(req.GetAuthMethodId(), password.AuthMethodPrefix+"_") {
|
||||
badFields["auth_method_id"] = "Invalid formatted identifier."
|
||||
}
|
||||
if req.GetVersion() == 0 {
|
||||
badFields["version"] = "Existing resource version is required for an update."
|
||||
}
|
||||
if len(badFields) > 0 {
|
||||
return handlers.InvalidArgumentErrorf("Improperly formatted identifier.", badFields)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateUpdateRequest(req *pbs.UpdateAccountRequest) error {
|
||||
badFields := map[string]string{}
|
||||
if !validId(req.GetId(), password.AccountPrefix+"_") {
|
||||
|
||||
@@ -824,3 +824,272 @@ func TestUpdate(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetPassword(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrap := db.TestWrapper(t)
|
||||
repoFn := func() (*password.Repository, error) {
|
||||
return password.NewRepository(rw, rw, wrap)
|
||||
}
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
tested, err := accounts.NewService(repoFn)
|
||||
require.NoError(t, err, "Error when getting new auth_method service.")
|
||||
|
||||
createAccount := func(t *testing.T, pw string) *pb.Account {
|
||||
am := password.TestAuthMethods(t, conn, o.GetPublicId(), 1)[0]
|
||||
pwAttrs := &pb.PasswordAccountAttributes{
|
||||
LoginName: "testusername",
|
||||
}
|
||||
if pw != "" {
|
||||
pwAttrs.Password = wrapperspb.String(pw)
|
||||
}
|
||||
attrs, err := handlers.ProtoToStruct(pwAttrs)
|
||||
createResp, err := tested.CreateAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.CreateAccountRequest{
|
||||
AuthMethodId: am.GetPublicId(),
|
||||
Item: &pb.Account{
|
||||
Type: "password",
|
||||
Attributes: attrs,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, createResp)
|
||||
require.NotNil(t, createResp.GetItem())
|
||||
return createResp.GetItem()
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
oldPw string
|
||||
newPw string
|
||||
}{
|
||||
{
|
||||
name: "has old set new",
|
||||
oldPw: "originalpassword",
|
||||
newPw: "a different password",
|
||||
},
|
||||
{
|
||||
name: "has old unset new",
|
||||
oldPw: "originalpassword",
|
||||
newPw: "",
|
||||
},
|
||||
{
|
||||
name: "no old password set new",
|
||||
oldPw: "",
|
||||
newPw: "a different password",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range cases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
acct := createAccount(t, tt.oldPw)
|
||||
|
||||
setResp, err := tested.SetPassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.SetPasswordRequest{
|
||||
AuthMethodId: acct.GetAuthMethodId(),
|
||||
Id: acct.GetId(),
|
||||
Version: acct.GetVersion(),
|
||||
Password: tt.newPw,
|
||||
})
|
||||
require.NoError(err)
|
||||
assert.Equal(acct.GetVersion()+1, setResp.GetItem().GetVersion())
|
||||
// clear uncomparable fields
|
||||
acct.Version, setResp.GetItem().Version = 0, 0
|
||||
acct.UpdatedTime, setResp.GetItem().UpdatedTime = nil, nil
|
||||
|
||||
assert.Empty(cmp.Diff(acct, setResp.GetItem(), protocmp.Transform()))
|
||||
})
|
||||
}
|
||||
|
||||
defaultAcct := createAccount(t, "")
|
||||
badRequestCases := []struct {
|
||||
name string
|
||||
authMethodId string
|
||||
accountId string
|
||||
version uint32
|
||||
password string
|
||||
}{
|
||||
{
|
||||
name: "empty auth method",
|
||||
authMethodId: "",
|
||||
accountId: defaultAcct.GetId(),
|
||||
version: defaultAcct.GetVersion(),
|
||||
password: "somepassword",
|
||||
},
|
||||
{
|
||||
name: "empty account id",
|
||||
authMethodId: defaultAcct.GetAuthMethodId(),
|
||||
accountId: "",
|
||||
version: defaultAcct.GetVersion(),
|
||||
password: "somepassword",
|
||||
},
|
||||
{
|
||||
name: "unset version",
|
||||
authMethodId: defaultAcct.GetAuthMethodId(),
|
||||
accountId: defaultAcct.GetId(),
|
||||
version: 0,
|
||||
password: "somepassword",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range badRequestCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
setResp, err := tested.SetPassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.SetPasswordRequest{
|
||||
AuthMethodId: tt.authMethodId,
|
||||
Id: tt.accountId,
|
||||
Version: tt.version,
|
||||
Password: tt.password,
|
||||
})
|
||||
assert.Error(err)
|
||||
assert.Nil(setResp)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestChangePassword(t *testing.T) {
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
rw := db.New(conn)
|
||||
wrap := db.TestWrapper(t)
|
||||
repoFn := func() (*password.Repository, error) {
|
||||
return password.NewRepository(rw, rw, wrap)
|
||||
}
|
||||
|
||||
o, _ := iam.TestScopes(t, conn)
|
||||
tested, err := accounts.NewService(repoFn)
|
||||
require.NoError(t, err, "Error when getting new auth_method service.")
|
||||
|
||||
createAccount := func(t *testing.T, pw string) *pb.Account {
|
||||
am := password.TestAuthMethods(t, conn, o.GetPublicId(), 1)[0]
|
||||
pwAttrs := &pb.PasswordAccountAttributes{
|
||||
LoginName: "testusername",
|
||||
}
|
||||
if pw != "" {
|
||||
pwAttrs.Password = wrapperspb.String(pw)
|
||||
}
|
||||
attrs, err := handlers.ProtoToStruct(pwAttrs)
|
||||
createResp, err := tested.CreateAccount(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.CreateAccountRequest{
|
||||
AuthMethodId: am.GetPublicId(),
|
||||
Item: &pb.Account{
|
||||
Type: "password",
|
||||
Attributes: attrs,
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, createResp)
|
||||
require.NotNil(t, createResp.GetItem())
|
||||
return createResp.GetItem()
|
||||
}
|
||||
|
||||
t.Run("valid update", func(t *testing.T) {
|
||||
assert, require := assert.New(t), require.New(t)
|
||||
acct := createAccount(t, "originalpassword")
|
||||
|
||||
changeResp, err := tested.ChangePassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.ChangePasswordRequest{
|
||||
AuthMethodId: acct.GetAuthMethodId(),
|
||||
Id: acct.GetId(),
|
||||
Version: acct.GetVersion(),
|
||||
OldPassword: "originalpassword",
|
||||
NewPassword: "a different password",
|
||||
})
|
||||
require.NoError(err)
|
||||
assert.Equal(acct.GetVersion()+1, changeResp.GetItem().GetVersion())
|
||||
// clear uncomparable fields
|
||||
acct.Version, changeResp.GetItem().Version = 0, 0
|
||||
acct.UpdatedTime, changeResp.GetItem().UpdatedTime = nil, nil
|
||||
|
||||
assert.Empty(cmp.Diff(acct, changeResp.GetItem(), protocmp.Transform()))
|
||||
})
|
||||
|
||||
t.Run("unauthenticated update", func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
acct := createAccount(t, "originalpassword")
|
||||
|
||||
changeResp, err := tested.ChangePassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.ChangePasswordRequest{
|
||||
AuthMethodId: acct.GetAuthMethodId(),
|
||||
Id: acct.GetId(),
|
||||
Version: acct.GetVersion(),
|
||||
OldPassword: "thewrongpassword",
|
||||
NewPassword: "a different password",
|
||||
})
|
||||
assert.Error(err)
|
||||
assert.Nil(changeResp)
|
||||
})
|
||||
|
||||
defaultAcct := createAccount(t, "")
|
||||
badRequestCases := []struct {
|
||||
name string
|
||||
authMethodId string
|
||||
accountId string
|
||||
version uint32
|
||||
oldPW string
|
||||
newPW string
|
||||
}{
|
||||
{
|
||||
name: "empty auth method",
|
||||
authMethodId: "",
|
||||
accountId: defaultAcct.GetId(),
|
||||
version: defaultAcct.GetVersion(),
|
||||
oldPW: "somepassword",
|
||||
newPW: "anewpassword",
|
||||
},
|
||||
{
|
||||
name: "empty account id",
|
||||
authMethodId: defaultAcct.GetAuthMethodId(),
|
||||
accountId: "",
|
||||
version: defaultAcct.GetVersion(),
|
||||
oldPW: "somepassword",
|
||||
newPW: "anewpassword",
|
||||
},
|
||||
{
|
||||
name: "unset version",
|
||||
authMethodId: defaultAcct.GetAuthMethodId(),
|
||||
accountId: defaultAcct.GetId(),
|
||||
version: 0,
|
||||
oldPW: "somepassword",
|
||||
newPW: "anewpassword",
|
||||
},
|
||||
{
|
||||
name: "no old password",
|
||||
authMethodId: defaultAcct.GetAuthMethodId(),
|
||||
accountId: defaultAcct.GetId(),
|
||||
version: defaultAcct.GetVersion(),
|
||||
oldPW: "",
|
||||
newPW: "anewpassword",
|
||||
},
|
||||
{
|
||||
name: "no new password",
|
||||
authMethodId: defaultAcct.GetAuthMethodId(),
|
||||
accountId: defaultAcct.GetId(),
|
||||
version: defaultAcct.GetVersion(),
|
||||
oldPW: "somepassword",
|
||||
newPW: "",
|
||||
},
|
||||
{
|
||||
name: "matching old and new passwords",
|
||||
authMethodId: defaultAcct.GetAuthMethodId(),
|
||||
accountId: defaultAcct.GetId(),
|
||||
version: defaultAcct.GetVersion(),
|
||||
oldPW: "somepassword",
|
||||
newPW: "somepassword",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range badRequestCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
changeResp, err := tested.ChangePassword(auth.DisabledAuthTestContext(auth.WithScopeId(o.GetPublicId())), &pbs.ChangePasswordRequest{
|
||||
AuthMethodId: tt.authMethodId,
|
||||
Id: tt.accountId,
|
||||
Version: tt.version,
|
||||
OldPassword: tt.oldPW,
|
||||
NewPassword: tt.newPW,
|
||||
})
|
||||
assert.Error(err)
|
||||
assert.Nil(changeResp)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user