Input validations and field renaming
This commit is contained in:
parent
a0958c9359
commit
1e889bc08c
|
@ -2,6 +2,7 @@ package userpass
|
|||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/vault/logical"
|
||||
|
@ -11,9 +12,9 @@ import (
|
|||
|
||||
func pathLogin(b *backend) *framework.Path {
|
||||
return &framework.Path{
|
||||
Pattern: "login/" + framework.GenericNameRegex("name"),
|
||||
Pattern: "login/" + framework.GenericNameRegex("username"),
|
||||
Fields: map[string]*framework.FieldSchema{
|
||||
"name": &framework.FieldSchema{
|
||||
"username": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: "Username of the user.",
|
||||
},
|
||||
|
@ -35,8 +36,15 @@ func pathLogin(b *backend) *framework.Path {
|
|||
|
||||
func (b *backend) pathLogin(
|
||||
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
||||
username := strings.ToLower(d.Get("name").(string))
|
||||
username := strings.ToLower(d.Get("username").(string))
|
||||
if username == "" {
|
||||
return nil, fmt.Errorf("missing username")
|
||||
}
|
||||
|
||||
password := d.Get("password").(string)
|
||||
if password == "" {
|
||||
return nil, fmt.Errorf("missing password")
|
||||
}
|
||||
|
||||
// Get the user and validate auth
|
||||
user, err := b.user(req.Storage, username)
|
||||
|
|
|
@ -9,9 +9,9 @@ import (
|
|||
|
||||
func pathUserPassword(b *backend) *framework.Path {
|
||||
return &framework.Path{
|
||||
Pattern: "users/" + framework.GenericNameRegex("name") + "/password$",
|
||||
Pattern: "users/" + framework.GenericNameRegex("username") + "/password$",
|
||||
Fields: map[string]*framework.FieldSchema{
|
||||
"name": &framework.FieldSchema{
|
||||
"username": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: "Username for this user.",
|
||||
},
|
||||
|
|
|
@ -7,9 +7,9 @@ import (
|
|||
|
||||
func pathUserPolicies(b *backend) *framework.Path {
|
||||
return &framework.Path{
|
||||
Pattern: "users/" + framework.GenericNameRegex("name") + "/policies$",
|
||||
Pattern: "users/" + framework.GenericNameRegex("username") + "/policies$",
|
||||
Fields: map[string]*framework.FieldSchema{
|
||||
"name": &framework.FieldSchema{
|
||||
"username": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: "Username for this user.",
|
||||
},
|
||||
|
|
|
@ -12,9 +12,9 @@ import (
|
|||
|
||||
func pathUsers(b *backend) *framework.Path {
|
||||
return &framework.Path{
|
||||
Pattern: "users/" + framework.GenericNameRegex("name"),
|
||||
Pattern: "users/" + framework.GenericNameRegex("username"),
|
||||
Fields: map[string]*framework.FieldSchema{
|
||||
"name": &framework.FieldSchema{
|
||||
"username": &framework.FieldSchema{
|
||||
Type: framework.TypeString,
|
||||
Description: "Username for this user.",
|
||||
},
|
||||
|
@ -55,9 +55,9 @@ func pathUsers(b *backend) *framework.Path {
|
|||
}
|
||||
|
||||
func (b *backend) userExistenceCheck(req *logical.Request, data *framework.FieldData) (bool, error) {
|
||||
username := data.Get("name").(string)
|
||||
username := data.Get("username").(string)
|
||||
if username == "" {
|
||||
return false, fmt.Errorf("name cannot be empty")
|
||||
return false, fmt.Errorf("missing username")
|
||||
}
|
||||
|
||||
entry, err := req.Storage.Get("user/" + strings.ToLower(username))
|
||||
|
@ -96,7 +96,7 @@ func (b *backend) SetUser(s logical.Storage, username string, userEntry *UserEnt
|
|||
|
||||
func (b *backend) pathUserDelete(
|
||||
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
||||
err := req.Storage.Delete("user/" + strings.ToLower(d.Get("name").(string)))
|
||||
err := req.Storage.Delete("user/" + strings.ToLower(d.Get("username").(string)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ func (b *backend) pathUserDelete(
|
|||
|
||||
func (b *backend) pathUserRead(
|
||||
req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
||||
user, err := b.user(req.Storage, strings.ToLower(d.Get("name").(string)))
|
||||
user, err := b.user(req.Storage, strings.ToLower(d.Get("username").(string)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -122,7 +122,7 @@ func (b *backend) pathUserRead(
|
|||
}
|
||||
|
||||
func (b *backend) userCreateUpdate(req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
||||
username := strings.ToLower(d.Get("name").(string))
|
||||
username := strings.ToLower(d.Get("username").(string))
|
||||
userEntry, err := b.user(req.Storage, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
Loading…
Reference in a new issue