Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Userpass auth #25

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
Loading