2015-07-28 01:05:06 +00:00
|
|
|
package duo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/duosecurity/duo_api_golang/authapi"
|
2016-07-06 16:25:40 +00:00
|
|
|
"github.com/hashicorp/vault/helper/jsonutil"
|
2015-07-28 01:05:06 +00:00
|
|
|
"github.com/hashicorp/vault/logical"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MockClientData struct {
|
2016-07-06 16:25:40 +00:00
|
|
|
PreauthData *authapi.PreauthResult
|
2015-07-28 01:05:06 +00:00
|
|
|
PreauthError error
|
2016-07-06 16:25:40 +00:00
|
|
|
AuthData *authapi.AuthResult
|
|
|
|
AuthError error
|
2015-07-28 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MockAuthClient struct {
|
|
|
|
MockData *MockClientData
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockAuthClient) Preauth(options ...func(*url.Values)) (*authapi.PreauthResult, error) {
|
|
|
|
return c.MockData.PreauthData, c.MockData.PreauthError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *MockAuthClient) Auth(factor string, options ...func(*url.Values)) (*authapi.AuthResult, error) {
|
|
|
|
return c.MockData.AuthData, c.MockData.AuthError
|
|
|
|
}
|
|
|
|
|
2016-07-06 16:25:40 +00:00
|
|
|
func MockGetDuoAuthClient(data *MockClientData) func(*logical.Request, *DuoConfig) (AuthClient, error) {
|
|
|
|
return func(*logical.Request, *DuoConfig) (AuthClient, error) {
|
2015-07-28 01:05:06 +00:00
|
|
|
return getDuoAuthClient(data), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDuoAuthClient(data *MockClientData) AuthClient {
|
|
|
|
var c MockAuthClient
|
2016-07-06 16:25:40 +00:00
|
|
|
// set default response to be successful
|
2015-07-28 18:55:46 +00:00
|
|
|
preauthSuccessJSON := `
|
|
|
|
{
|
|
|
|
"Stat": "OK",
|
|
|
|
"Response": {
|
|
|
|
"Result": "auth",
|
|
|
|
"Status_Msg": "Needs authentication",
|
|
|
|
"Devices": []
|
|
|
|
}
|
|
|
|
}`
|
2015-07-28 01:05:06 +00:00
|
|
|
if data.PreauthData == nil {
|
|
|
|
data.PreauthData = &authapi.PreauthResult{}
|
2016-07-06 16:25:40 +00:00
|
|
|
jsonutil.DecodeJSON([]byte(preauthSuccessJSON), data.PreauthData)
|
2015-07-28 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-28 18:55:46 +00:00
|
|
|
authSuccessJSON := `
|
|
|
|
{
|
|
|
|
"Stat": "OK",
|
|
|
|
"Response": {
|
|
|
|
"Result": "allow"
|
|
|
|
}
|
|
|
|
}`
|
2015-07-28 01:05:06 +00:00
|
|
|
if data.AuthData == nil {
|
|
|
|
data.AuthData = &authapi.AuthResult{}
|
2016-07-06 16:25:40 +00:00
|
|
|
jsonutil.DecodeJSON([]byte(authSuccessJSON), data.AuthData)
|
2015-07-28 01:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.MockData = data
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuoHandlerSuccess(t *testing.T) {
|
|
|
|
successResp := &logical.Response{
|
|
|
|
Auth: &logical.Auth{},
|
|
|
|
}
|
|
|
|
duoConfig := &DuoConfig{
|
|
|
|
UsernameFormat: "%s",
|
|
|
|
}
|
|
|
|
duoAuthClient := getDuoAuthClient(&MockClientData{})
|
2016-07-06 16:25:40 +00:00
|
|
|
resp, err := duoHandler(duoConfig, duoAuthClient, &duoAuthRequest{
|
2015-07-28 18:55:46 +00:00
|
|
|
successResp: successResp,
|
2016-07-06 16:25:40 +00:00
|
|
|
username: "",
|
2015-07-28 18:55:46 +00:00
|
|
|
})
|
2015-07-28 01:05:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
if resp != successResp {
|
|
|
|
t.Fatalf("Testing Duo authentication gave incorrect response (expected success, got: %v)", resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuoHandlerReject(t *testing.T) {
|
|
|
|
AuthData := &authapi.AuthResult{}
|
2015-07-28 18:55:46 +00:00
|
|
|
authRejectJSON := `
|
|
|
|
{
|
|
|
|
"Stat": "OK",
|
|
|
|
"Response": {
|
|
|
|
"Result": "deny",
|
|
|
|
"Status_Msg": "Invalid auth"
|
|
|
|
}
|
|
|
|
}`
|
2016-07-06 16:25:40 +00:00
|
|
|
jsonutil.DecodeJSON([]byte(authRejectJSON), AuthData)
|
2015-07-28 01:05:06 +00:00
|
|
|
successResp := &logical.Response{
|
|
|
|
Auth: &logical.Auth{},
|
|
|
|
}
|
|
|
|
expectedError := AuthData.Response.Status_Msg
|
|
|
|
duoConfig := &DuoConfig{
|
|
|
|
UsernameFormat: "%s",
|
|
|
|
}
|
|
|
|
duoAuthClient := getDuoAuthClient(&MockClientData{
|
|
|
|
AuthData: AuthData,
|
|
|
|
})
|
2016-07-06 16:25:40 +00:00
|
|
|
resp, err := duoHandler(duoConfig, duoAuthClient, &duoAuthRequest{
|
2015-07-28 18:55:46 +00:00
|
|
|
successResp: successResp,
|
2016-07-06 16:25:40 +00:00
|
|
|
username: "user",
|
2015-07-28 18:55:46 +00:00
|
|
|
})
|
2015-07-28 01:05:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
error, ok := resp.Data["error"].(string)
|
|
|
|
if !ok || !strings.Contains(error, expectedError) {
|
|
|
|
t.Fatalf("Testing Duo authentication gave incorrect response (expected deny, got: %v)", error)
|
|
|
|
}
|
|
|
|
}
|