2016-04-28 06:08:06 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2017-11-21 01:57:41 +02:00
|
|
|
"strings"
|
2016-04-28 06:08:06 +02:00
|
|
|
"testing"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-04-28 06:08:06 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
|
|
|
e := echo.New()
|
2018-10-14 17:16:58 +02:00
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
res := httptest.NewRecorder()
|
2016-04-28 06:08:06 +02:00
|
|
|
c := e.NewContext(req, res)
|
2017-05-27 12:10:51 +02:00
|
|
|
f := func(u, p string, c echo.Context) (bool, error) {
|
2016-04-28 06:08:06 +02:00
|
|
|
if u == "joe" && p == "secret" {
|
2017-05-27 12:10:51 +02:00
|
|
|
return true, nil
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
2017-05-27 12:10:51 +02:00
|
|
|
return false, nil
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|
|
|
|
h := BasicAuth(f)(func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "test")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Valid credentials
|
|
|
|
auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(echo.HeaderAuthorization, auth)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.NoError(t, h(c))
|
2016-04-28 06:08:06 +02:00
|
|
|
|
2018-02-21 20:44:17 +02:00
|
|
|
h = BasicAuthWithConfig(BasicAuthConfig{
|
|
|
|
Skipper: nil,
|
|
|
|
Validator: f,
|
|
|
|
Realm: "someRealm",
|
|
|
|
})(func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "test")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Valid credentials
|
|
|
|
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
|
|
|
req.Header.Set(echo.HeaderAuthorization, auth)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.NoError(t, h(c))
|
2018-02-21 20:44:17 +02:00
|
|
|
|
2017-11-21 01:57:41 +02:00
|
|
|
// Case-insensitive header scheme
|
|
|
|
auth = strings.ToUpper(basic) + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
|
|
|
req.Header.Set(echo.HeaderAuthorization, auth)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.NoError(t, h(c))
|
2017-11-21 01:57:41 +02:00
|
|
|
|
2017-01-03 06:12:06 +02:00
|
|
|
// Invalid credentials
|
|
|
|
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:invalid-password"))
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(echo.HeaderAuthorization, auth)
|
2016-04-28 06:08:06 +02:00
|
|
|
he := h(c).(*echo.HTTPError)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
|
|
|
assert.Equal(t, basic+` realm="someRealm"`, res.Header().Get(echo.HeaderWWWAuthenticate))
|
2016-04-28 06:08:06 +02:00
|
|
|
|
2022-05-27 18:44:51 +02:00
|
|
|
// Invalid base64 string
|
|
|
|
auth = basic + " invalidString"
|
|
|
|
req.Header.Set(echo.HeaderAuthorization, auth)
|
|
|
|
he = h(c).(*echo.HTTPError)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, http.StatusBadRequest, he.Code)
|
2022-05-27 18:44:51 +02:00
|
|
|
|
2017-01-03 06:12:06 +02:00
|
|
|
// Missing Authorization header
|
|
|
|
req.Header.Del(echo.HeaderAuthorization)
|
2016-04-28 06:08:06 +02:00
|
|
|
he = h(c).(*echo.HTTPError)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
2016-04-28 06:08:06 +02:00
|
|
|
|
|
|
|
// Invalid Authorization header
|
|
|
|
auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
|
2016-09-23 07:53:44 +02:00
|
|
|
req.Header.Set(echo.HeaderAuthorization, auth)
|
2016-04-28 06:08:06 +02:00
|
|
|
he = h(c).(*echo.HTTPError)
|
2022-10-12 20:47:21 +02:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code)
|
2016-04-28 06:08:06 +02:00
|
|
|
}
|