Skip to content

Commit eb05822

Browse files
committed
add more token options
1 parent d34cf4f commit eb05822

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

flake.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
src = ./vault_plugin;
3333

34-
vendorHash = "sha256-/6aE5w6Rki1ZIXMX9Ryo4XrGzS/01xZQiWDUROriixs=";
34+
vendorHash = "sha256-1uGPhzCk9b0tJNz08S/3QQ8ceuppajn796zQ4glYXeQ=";
3535
};
3636
};
3737

@@ -51,6 +51,7 @@
5151
vault-bin
5252
protoc-gen-go
5353
pkgs.protoc-gen-connect-go
54+
terraform
5455
];
5556
shellHook = ''
5657
export CFLAGS="-I${pkgs.glibc.dev}/include"

vault_plugin/backend.go

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/bufbuild/connect-go"
1919
"github.com/google/uuid"
2020
"github.com/hashicorp/vault/sdk/framework"
21-
"github.com/hashicorp/vault/sdk/helper/policyutil"
21+
"github.com/hashicorp/vault/sdk/helper/tokenutil"
2222
"github.com/hashicorp/vault/sdk/logical"
2323
api_v1 "github.com/kilimnik/vepiot/vault_plugin/gen/api/v1"
2424
"github.com/kilimnik/vepiot/vault_plugin/gen/api/v1/v1connect"
@@ -35,8 +35,9 @@ type User struct {
3535
}
3636

3737
type Auth struct {
38-
Policies []string
39-
Users map[string]*User
38+
tokenutil.TokenParams
39+
40+
Users map[string]*User
4041
}
4142

4243
// backend wraps the backend framework and adds a map for storing key value pairs.
@@ -151,19 +152,14 @@ func (b *backend) handleLogin(ctx context.Context, req *logical.Request, data *f
151152
resp := &logical.Response{
152153
Auth: &logical.Auth{
153154
// Policies can be passed in as a parameter to the request
154-
Policies: auth.Policies,
155155
Metadata: map[string]string{
156156
"name": name,
157157
},
158-
// Lease options can be passed in as parameters to the request
159-
LeaseOptions: logical.LeaseOptions{
160-
TTL: 30 * time.Second,
161-
MaxTTL: 15 * time.Minute,
162-
Renewable: false,
163-
},
164158
},
165159
}
166160

161+
auth.PopulateTokenAuth(resp.Auth)
162+
167163
return resp, nil
168164
}
169165

@@ -380,27 +376,26 @@ func RetrieveTOTP(
380376
}
381377

382378
func (b *backend) pathAuths() []*framework.Path {
383-
return []*framework.Path{
379+
fields := map[string]*framework.FieldSchema{
380+
"name": {
381+
Required: true,
382+
Type: framework.TypeString,
383+
Description: "Specifies the auth name",
384+
},
385+
"firebase_device_ids": {
386+
Required: true,
387+
Type: framework.TypeCommaStringSlice,
388+
Description: "Specifies the device ids to send the notification to",
389+
},
390+
}
391+
392+
tokenutil.AddTokenFields(fields)
393+
394+
p := []*framework.Path{
384395
{
385396
Pattern: "auth/" + framework.GenericNameRegex("name"),
386397

387-
Fields: map[string]*framework.FieldSchema{
388-
"name": {
389-
Required: true,
390-
Type: framework.TypeString,
391-
Description: "Specifies the auth name",
392-
},
393-
"policies": {
394-
Required: true,
395-
Type: framework.TypeCommaStringSlice,
396-
Description: "Specifies the policies",
397-
},
398-
"firebase_device_ids": {
399-
Required: true,
400-
Type: framework.TypeCommaStringSlice,
401-
Description: "Specifies the device ids to send the notification to",
402-
},
403-
},
398+
Fields: fields,
404399

405400
Operations: map[logical.Operation]framework.OperationHandler{
406401
logical.CreateOperation: &framework.PathOperation{
@@ -416,6 +411,8 @@ func (b *backend) pathAuths() []*framework.Path {
416411
ExistenceCheck: b.handleExistenceCheck,
417412
},
418413
}
414+
415+
return p
419416
}
420417

421418
func (b *backend) handleExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
@@ -457,11 +454,6 @@ func (b *backend) handleAuthWrite(ctx context.Context, req *logical.Request, dat
457454
return logical.ErrorResponse("'name': name must be provided"), nil
458455
}
459456

460-
policies := policyutil.ParsePolicies(data.Get("policies"))
461-
if len(policies) == 0 {
462-
return logical.ErrorResponse("'policies': at least one policy must be provided"), nil
463-
}
464-
465457
firebaseDeviceIds := ParseList(data.Get("firebase_device_ids"))
466458
if len(firebaseDeviceIds) == 0 {
467459
return logical.ErrorResponse("'firebase_device_ids': at least one firebase device id must be provided"), nil
@@ -489,11 +481,17 @@ func (b *backend) handleAuthWrite(ctx context.Context, req *logical.Request, dat
489481
totpQrCodes[device] = ".\n" + buf.String()
490482
}
491483

492-
b.auths[name] = &Auth{
493-
Policies: policies,
494-
Users: users,
484+
auth := Auth{
485+
Users: users,
495486
}
496487

488+
// Get tokenutil fields
489+
if err := auth.ParseTokenFields(req, data); err != nil {
490+
return logical.ErrorResponse(err.Error()), logical.ErrInvalidRequest
491+
}
492+
493+
b.auths[name] = &auth
494+
497495
resp := &logical.Response{
498496
Data: totpQrCodes,
499497
}

0 commit comments

Comments
 (0)