2015-07-27 18:23:34 +00:00
|
|
|
package duo
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2015-07-27 18:23:34 +00:00
|
|
|
"fmt"
|
2015-07-28 01:05:06 +00:00
|
|
|
"net/url"
|
2015-07-27 18:23:34 +00:00
|
|
|
|
|
|
|
"github.com/duosecurity/duo_api_golang"
|
|
|
|
"github.com/duosecurity/duo_api_golang/authapi"
|
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
"github.com/hashicorp/vault/logical/framework"
|
|
|
|
)
|
|
|
|
|
2015-07-28 01:05:06 +00:00
|
|
|
type AuthClient interface {
|
|
|
|
Preauth(options ...func(*url.Values)) (*authapi.PreauthResult, error)
|
|
|
|
Auth(factor string, options ...func(*url.Values)) (*authapi.AuthResult, error)
|
|
|
|
}
|
|
|
|
|
2015-07-27 18:23:34 +00:00
|
|
|
func pathDuoAccess() *framework.Path {
|
|
|
|
return &framework.Path{
|
|
|
|
Pattern: `duo/access`,
|
|
|
|
Fields: map[string]*framework.FieldSchema{
|
|
|
|
"skey": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Duo secret key",
|
|
|
|
},
|
|
|
|
"ikey": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Duo integration key",
|
|
|
|
},
|
|
|
|
"host": &framework.FieldSchema{
|
|
|
|
Type: framework.TypeString,
|
|
|
|
Description: "Duo api host",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Callbacks: map[logical.Operation]framework.OperationFunc{
|
2016-08-19 20:48:32 +00:00
|
|
|
logical.UpdateOperation: pathDuoAccessWrite,
|
2015-07-27 18:23:34 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
HelpSynopsis: pathDuoAccessHelpSyn,
|
|
|
|
HelpDescription: pathDuoAccessHelpDesc,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 01:05:06 +00:00
|
|
|
func GetDuoAuthClient(req *logical.Request, config *DuoConfig) (AuthClient, error) {
|
2015-07-27 18:23:34 +00:00
|
|
|
entry, err := req.Storage.Get("duo/access")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if entry == nil {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"Duo access credentials haven't been configured. Please configure\n" +
|
|
|
|
"them at the 'duo/access' endpoint")
|
|
|
|
}
|
|
|
|
var access DuoAccess
|
|
|
|
if err := entry.DecodeJSON(&access); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-28 01:05:06 +00:00
|
|
|
duoClient := duoapi.NewDuoApi(
|
2015-07-27 18:23:34 +00:00
|
|
|
access.IKey,
|
|
|
|
access.SKey,
|
|
|
|
access.Host,
|
|
|
|
config.UserAgent,
|
|
|
|
)
|
2015-07-28 01:05:06 +00:00
|
|
|
duoAuthClient := authapi.NewAuthApi(*duoClient)
|
|
|
|
check, err := duoAuthClient.Check()
|
2015-07-27 18:23:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if check.StatResult.Stat != "OK" {
|
|
|
|
return nil, fmt.Errorf("Could not connect to Duo: %s (%s)", *check.StatResult.Message, *check.StatResult.Message_Detail)
|
|
|
|
}
|
2015-07-28 01:05:06 +00:00
|
|
|
return duoAuthClient, nil
|
2015-07-27 18:23:34 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
func pathDuoAccessWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
|
2015-07-27 18:23:34 +00:00
|
|
|
entry, err := logical.StorageEntryJSON("duo/access", DuoAccess{
|
|
|
|
SKey: d.Get("skey").(string),
|
|
|
|
IKey: d.Get("ikey").(string),
|
|
|
|
Host: d.Get("host").(string),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := req.Storage.Put(entry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DuoAccess struct {
|
|
|
|
SKey string `json:"skey"`
|
|
|
|
IKey string `json:"ikey"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const pathDuoAccessHelpSyn = `
|
|
|
|
Configure the access keys and host for Duo API connections.
|
|
|
|
`
|
|
|
|
|
|
|
|
const pathDuoAccessHelpDesc = `
|
|
|
|
To authenticate users with Duo, the backend needs to know what host to connect to
|
|
|
|
and must authenticate with an integration key and secret key. This endpoint is used
|
|
|
|
to configure that information.
|
|
|
|
`
|