Skip to content

Commit

Permalink
feat(auth): Adding token auth (#39)
Browse files Browse the repository at this point in the history
Adding token auth
  • Loading branch information
Jacobbrewer1 authored Jan 24, 2025
1 parent 868e889 commit adfa8fc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 0 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ func NewClient(opts ...ClientOption) (Client, error) {

c.authCreds = authCreds

go c.renewAuthInfo()

return c, nil
}

Expand Down
22 changes: 20 additions & 2 deletions client_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,36 @@ func WithConfig(config *hashiVault.Config) ClientOption {
}
}

func WithTokenAuth(token string) ClientOption {
return func(c *client) {
c.auth = func(v *hashiVault.Client) (*hashiVault.Secret, error) {
return tokenLogin(v, token)
}
}
}

func WithAppRoleAuth(roleID, secretID string) ClientOption {
return func(c *client) {
c.auth = func(v *hashiVault.Client) (*hashiVault.Secret, error) {
return appRoleLogin(v, roleID, secretID)
sec, err := appRoleLogin(v, roleID, secretID)
if err != nil {
return nil, err
}
go c.renewAuthInfo()
return sec, nil
}
}
}

func WithUserPassAuth(username, password string) ClientOption {
return func(c *client) {
c.auth = func(v *hashiVault.Client) (*hashiVault.Secret, error) {
return userPassLogin(v, username, password)
sec, err := userPassLogin(v, username, password)
if err != nil {
return nil, err
}
go c.renewAuthInfo()
return sec, nil
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package vaulty

import hashiVault "github.com/hashicorp/vault/api"

func tokenLogin(v *hashiVault.Client, token string) (*hashiVault.Secret, error) {
v.SetToken(token)
return v.Auth().Token().LookupSelf()
}

0 comments on commit adfa8fc

Please sign in to comment.