Skip to content

Commit

Permalink
Implement Userpass auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucaber committed Jun 6, 2024
1 parent 62b111d commit 0eb8447
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions client_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ func WithAuthToken(token string) ClientOpts {
return nil
}
}

func WithUserpassAuth(username string, password string, opts ...UserpassAuthOpt) ClientOpts {
return func(c *Client) error {
userpassAuthProvider, err := NewUserpassAuth(c, username, password, opts...)
if err != nil {
return err
}

c.auth = userpassAuthProvider

return nil
}
}
57 changes: 57 additions & 0 deletions userpass_auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package vault

func NewUserpassAuth(c *Client, username string, password string, opts ...UserpassAuthOpt) (AuthProvider, error) {
k := &UserpassAuth{
Client: c,
mountPoint: "userpass",
username: username,
password: password,
}

for _, opt := range opts {
err := opt(k)
if err != nil {
return nil, err
}
}

return k, nil
}

type UserpassAuth struct {
Client *Client
mountPoint string
username string
password string
}

type userpassAuthConfig struct {
Password string `json:"password"`
}

func (k UserpassAuth) Auth() (*AuthResponse, error) {
conf := &userpassAuthConfig{
Password: k.password,
}

res := &AuthResponse{}

err := k.Client.Write([]string{"v1", "auth", k.mountPoint, "login", k.username}, conf, res, &RequestOptions{
SkipRenewal: true,
})
if err != nil {
return nil, err
}

return res, nil
}

type UserpassAuthOpt func(k *UserpassAuth) error

func WithUserpassMountPoint(mountPoint string) UserpassAuthOpt {
return func(k *UserpassAuth) error {
k.mountPoint = mountPoint

return nil
}
}

0 comments on commit 0eb8447

Please sign in to comment.