2015-07-27 18:23:34 +00:00
|
|
|
package mfa
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
|
|
|
|
2015-07-27 18:23:34 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pathMFAConfig(b *backend) *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: `mfa_config`,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"type": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Enables MFA with given backend (available: duo)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-08-19 20:48:32 +00:00
|
|
|
logical.UpdateOperation: b.pathMFAConfigWrite,
|
|
|
|
logical.ReadOperation: b.pathMFAConfigRead,
|
2015-07-27 18:23:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathMFAConfigHelpSyn,
|
|
|
|
HelpDescription: pathMFAConfigHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
func (b *backend) MFAConfig(ctx context.Context, req *logical.Request) (*MFAConfig, error) {
|
|
|
|
entry, err := req.Storage.Get(ctx, "mfa_config")
|
2015-07-27 18:23:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
var result MFAConfig
|
|
|
|
if err := entry.DecodeJSON(&result); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &result, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathMFAConfigWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-07-27 18:23:34 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("mfa_config", MFAConfig{
|
|
|
|
Type: d.Get("type").(string),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
if err := req.Storage.Put(ctx, entry); err != nil {
|
2015-07-27 18:23:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func (b *backend) pathMFAConfigRead(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
config, err := b.MFAConfig(ctx, req)
|
2015-07-27 18:23:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if config == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &logical.Response{
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"type": config.Type,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type MFAConfig struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathMFAConfigHelpSyn = `
|
|
|
|
Configure multi factor backend.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathMFAConfigHelpDesc = `
|
|
|
|
This endpoint allows you to turn on multi-factor authentication with a given backend.
|
|
|
|
Currently only Duo is supported.
|
|
|
|
`
|