Add an API to reset the password
This commit is contained in:
parent
a631d5dbfc
commit
6e1edec413
@ -92,6 +92,15 @@ func (a *AccountsServer) ResetPassword(ctx context.Context, in *accounts.Account
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (acc *AccountsServer) NewPassword(ctx context.Context, in *accounts.AccountWithPasswordAndCode) (*empty.Empty, error) {
|
||||
data := populateData(in.Data.GetName(), in.Password.GetPassword(), in.Data.GetEmail())
|
||||
account := populateAccount(data, acc.Controller)
|
||||
if err := account.NewPassword(ctx, in.GetCode()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func populateData(username, password, email string) *controllers.AccountData {
|
||||
return &controllers.AccountData{
|
||||
Username: username,
|
||||
|
@ -265,6 +265,44 @@ func (acc *Account) ResetPassword(ctx context.Context, emailConfig email.EmailCo
|
||||
return number, nil
|
||||
}
|
||||
|
||||
func (acc *Account) NewPassword(ctx context.Context, code string) error {
|
||||
clientset, err := kubernetes.NewForConfig(acc.Controller.GetConfig())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
userdata, err := clientset.CoreV1().Secrets("softplayer-accounts").Get(ctx, acc.Data.Username, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
acc.Data.UUID = string(userdata.Data["uuid"])
|
||||
secretName := "password-reset-code"
|
||||
sec, err := clientset.CoreV1().Secrets(acc.Data.UUID).Get(ctx, secretName, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if realCode, ok := sec.Data["code"]; ok {
|
||||
if string(realCode) != code {
|
||||
return errors.New("wrong code")
|
||||
}
|
||||
} else {
|
||||
return errors.New("secret not found")
|
||||
}
|
||||
|
||||
passwordHash, err := hash.HashPassword(acc.Data.Password, int(acc.Params.HashCost))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
userdata.Data["password"] = []byte(passwordHash)
|
||||
_, err = clientset.CoreV1().Secrets(acc.Data.UUID).Update(ctx, userdata, metav1.UpdateOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (acc *Account) getToken(ctx context.Context, saSec *corev1.Secret) (string, error) {
|
||||
client := acc.Controller.GetClient()
|
||||
if err := client.Get(ctx, types.NamespacedName{
|
||||
|
Loading…
Reference in New Issue
Block a user