2019-11-25 03:27:59 +07:00
|
|
|
package suites
|
|
|
|
|
|
|
|
import (
|
2021-12-01 10:32:58 +07:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2019-11-25 03:27:59 +07:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
2019-11-30 23:49:52 +07:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-12-01 10:32:58 +07:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/duo"
|
2019-11-25 03:27:59 +07:00
|
|
|
)
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// DuoPolicy a type of policy.
|
2019-11-25 03:27:59 +07:00
|
|
|
type DuoPolicy int32
|
|
|
|
|
|
|
|
const (
|
2020-05-02 12:06:39 +07:00
|
|
|
// Deny deny policy.
|
2019-11-25 03:27:59 +07:00
|
|
|
Deny DuoPolicy = iota
|
2020-05-02 12:06:39 +07:00
|
|
|
// Allow allow policy.
|
2019-11-25 03:27:59 +07:00
|
|
|
Allow DuoPolicy = iota
|
|
|
|
)
|
|
|
|
|
2020-05-02 12:06:39 +07:00
|
|
|
// ConfigureDuo configure duo api to allow or block auth requests.
|
2019-11-25 03:27:59 +07:00
|
|
|
func ConfigureDuo(t *testing.T, allowDeny DuoPolicy) {
|
|
|
|
url := fmt.Sprintf("%s/allow", DuoBaseURL)
|
|
|
|
if allowDeny == Deny {
|
|
|
|
url = fmt.Sprintf("%s/deny", DuoBaseURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url, nil)
|
2019-11-30 23:49:52 +07:00
|
|
|
require.NoError(t, err)
|
2019-11-25 03:27:59 +07:00
|
|
|
|
|
|
|
client := NewHTTPClient()
|
|
|
|
res, err := client.Do(req)
|
2019-11-30 23:49:52 +07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, res.StatusCode)
|
2019-11-25 03:27:59 +07:00
|
|
|
}
|
2021-12-01 10:32:58 +07:00
|
|
|
|
|
|
|
// ConfigureDuoPreAuth configure duo api to respond with available devices or enrollment Url.
|
|
|
|
func ConfigureDuoPreAuth(t *testing.T, response duo.PreAuthResponse) {
|
|
|
|
url := fmt.Sprintf("%s/preauth", DuoBaseURL)
|
|
|
|
|
|
|
|
body, err := json.Marshal(response)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
client := NewHTTPClient()
|
|
|
|
res, err := client.Do(req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 200, res.StatusCode)
|
|
|
|
}
|