Skip to content

Commit 0eb8447

Browse files
committed
Implement Userpass auth
1 parent 62b111d commit 0eb8447

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

client_opts.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,16 @@ func WithAuthToken(token string) ClientOpts {
2121
return nil
2222
}
2323
}
24+
25+
func WithUserpassAuth(username string, password string, opts ...UserpassAuthOpt) ClientOpts {
26+
return func(c *Client) error {
27+
userpassAuthProvider, err := NewUserpassAuth(c, username, password, opts...)
28+
if err != nil {
29+
return err
30+
}
31+
32+
c.auth = userpassAuthProvider
33+
34+
return nil
35+
}
36+
}

userpass_auth.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package vault
2+
3+
func NewUserpassAuth(c *Client, username string, password string, opts ...UserpassAuthOpt) (AuthProvider, error) {
4+
k := &UserpassAuth{
5+
Client: c,
6+
mountPoint: "userpass",
7+
username: username,
8+
password: password,
9+
}
10+
11+
for _, opt := range opts {
12+
err := opt(k)
13+
if err != nil {
14+
return nil, err
15+
}
16+
}
17+
18+
return k, nil
19+
}
20+
21+
type UserpassAuth struct {
22+
Client *Client
23+
mountPoint string
24+
username string
25+
password string
26+
}
27+
28+
type userpassAuthConfig struct {
29+
Password string `json:"password"`
30+
}
31+
32+
func (k UserpassAuth) Auth() (*AuthResponse, error) {
33+
conf := &userpassAuthConfig{
34+
Password: k.password,
35+
}
36+
37+
res := &AuthResponse{}
38+
39+
err := k.Client.Write([]string{"v1", "auth", k.mountPoint, "login", k.username}, conf, res, &RequestOptions{
40+
SkipRenewal: true,
41+
})
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
return res, nil
47+
}
48+
49+
type UserpassAuthOpt func(k *UserpassAuth) error
50+
51+
func WithUserpassMountPoint(mountPoint string) UserpassAuthOpt {
52+
return func(k *UserpassAuth) error {
53+
k.mountPoint = mountPoint
54+
55+
return nil
56+
}
57+
}

0 commit comments

Comments
 (0)