open-vault/builtin/credential/userpass/path_user_password.go

81 lines
2 KiB
Go
Raw Normal View History

package userpass
import (
"fmt"
2016-03-16 17:39:20 +00:00
"golang.org/x/crypto/bcrypt"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathUserPassword(b *backend) *framework.Path {
return &framework.Path{
2016-03-15 21:47:13 +00:00
Pattern: "users/" + framework.GenericNameRegex("username") + "/password$",
Fields: map[string]*framework.FieldSchema{
2016-03-15 21:47:13 +00:00
"username": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Username for this user.",
},
"password": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Password for this user.",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathUserPasswordUpdate,
},
HelpSynopsis: pathUserPasswordHelpSyn,
HelpDescription: pathUserPasswordHelpDesc,
}
}
func (b *backend) pathUserPasswordUpdate(
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
2016-03-16 17:39:20 +00:00
username := d.Get("username").(string)
userEntry, err := b.user(req.Storage, username)
if err != nil {
return nil, err
}
2016-03-16 18:21:14 +00:00
if userEntry == nil {
return nil, fmt.Errorf("username does not exist")
}
2016-03-16 17:39:20 +00:00
userErr, intErr := b.updateUserPassword(req, d, userEntry)
if intErr != nil {
2016-03-16 17:39:20 +00:00
return nil, err
}
if userErr != nil {
return logical.ErrorResponse(userErr.Error()), logical.ErrInvalidRequest
}
2016-03-16 17:39:20 +00:00
return nil, b.setUser(req.Storage, username, userEntry)
}
func (b *backend) updateUserPassword(req *logical.Request, d *framework.FieldData, userEntry *UserEntry) (error, error) {
password := d.Get("password").(string)
if password == "" {
return fmt.Errorf("missing password"), nil
2016-03-16 17:39:20 +00:00
}
// Generate a hash of the password
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return nil, err
}
2016-03-16 17:39:20 +00:00
userEntry.PasswordHash = hash
return nil, nil
}
const pathUserPasswordHelpSyn = `
Reset user's password.
`
const pathUserPasswordHelpDesc = `
This endpoint allows resetting the user's password.
`