2015-05-11 15:43:54 -07:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2015-05-17 22:54:29 -07:00
|
|
|
|
2015-06-09 20:06:51 -07:00
|
|
|
"github.com/dgrijalva/jwt-go"
|
2015-05-17 22:54:29 -07:00
|
|
|
"github.com/labstack/echo"
|
2015-05-30 10:54:55 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-30 15:20:36 -07:00
|
|
|
"net/http/httptest"
|
2015-06-09 20:06:51 -07:00
|
|
|
"time"
|
2015-05-11 15:43:54 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicAuth(t *testing.T) {
|
2015-05-30 10:54:55 -07:00
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
2015-05-11 15:43:54 -07:00
|
|
|
fn := func(u, p string) bool {
|
|
|
|
if u == "joe" && p == "secret" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2015-05-13 23:07:03 -07:00
|
|
|
ba := BasicAuth(fn)
|
2015-05-11 15:43:54 -07:00
|
|
|
|
|
|
|
// Valid credentials
|
|
|
|
auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-30 10:54:55 -07:00
|
|
|
assert.NoError(t, ba(c))
|
2015-05-11 15:43:54 -07:00
|
|
|
|
|
|
|
//---------------------
|
|
|
|
// Invalid credentials
|
|
|
|
//---------------------
|
|
|
|
|
2015-05-16 12:49:01 -07:00
|
|
|
// Incorrect password
|
2015-06-09 20:06:51 -07:00
|
|
|
auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password"))
|
2015-05-11 15:43:54 -07:00
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-21 14:02:29 -07:00
|
|
|
he := ba(c).(*echo.HTTPError)
|
2015-05-30 10:54:55 -07:00
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code())
|
2015-05-17 22:54:29 -07:00
|
|
|
|
|
|
|
// Empty Authorization header
|
|
|
|
req.Header.Set(echo.Authorization, "")
|
2015-05-21 14:02:29 -07:00
|
|
|
he = ba(c).(*echo.HTTPError)
|
2015-05-30 10:54:55 -07:00
|
|
|
assert.Equal(t, http.StatusBadRequest, he.Code())
|
2015-05-11 15:43:54 -07:00
|
|
|
|
2015-05-17 22:54:29 -07:00
|
|
|
// Invalid Authorization header
|
2015-05-12 06:28:25 -07:00
|
|
|
auth = base64.StdEncoding.EncodeToString([]byte(" :secret"))
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-21 14:02:29 -07:00
|
|
|
he = ba(c).(*echo.HTTPError)
|
2015-05-30 10:54:55 -07:00
|
|
|
assert.Equal(t, http.StatusBadRequest, he.Code())
|
2015-05-12 06:28:25 -07:00
|
|
|
|
2015-05-11 15:43:54 -07:00
|
|
|
// Invalid scheme
|
2015-05-30 10:54:55 -07:00
|
|
|
auth = "Base " + base64.StdEncoding.EncodeToString([]byte(" :secret"))
|
2015-05-11 15:43:54 -07:00
|
|
|
req.Header.Set(echo.Authorization, auth)
|
2015-05-21 14:02:29 -07:00
|
|
|
he = ba(c).(*echo.HTTPError)
|
2015-05-30 10:54:55 -07:00
|
|
|
assert.Equal(t, http.StatusBadRequest, he.Code())
|
|
|
|
|
|
|
|
// WebSocket
|
|
|
|
c.Request().Header.Set(echo.Upgrade, echo.WebSocket)
|
|
|
|
assert.NoError(t, ba(c))
|
2015-05-11 15:43:54 -07:00
|
|
|
}
|
2015-06-09 20:06:51 -07:00
|
|
|
|
|
|
|
func TestJWTAuth(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
|
|
|
|
key := []byte("key")
|
|
|
|
fn := func(kid string, method jwt.SigningMethod) ([]byte, error) {
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
ja := JWTAuth(fn)
|
|
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
|
|
|
token.Claims["foo"] = "bar"
|
|
|
|
token.Claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
|
|
|
ts, err := token.SignedString(key)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Valid credentials
|
|
|
|
auth := Bearer + " " + ts
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
|
|
|
assert.NoError(t, ja(c))
|
|
|
|
|
|
|
|
//---------------------
|
|
|
|
// Invalid credentials
|
|
|
|
//---------------------
|
|
|
|
|
|
|
|
// Expired token
|
|
|
|
token.Claims["exp"] = time.Now().Add(-time.Second).Unix()
|
|
|
|
ts, err = token.SignedString(key)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
auth = Bearer + " " + ts
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
|
|
|
he := ja(c).(*echo.HTTPError)
|
|
|
|
assert.Equal(t, http.StatusUnauthorized, he.Code())
|
|
|
|
|
|
|
|
// Empty Authorization header
|
|
|
|
req.Header.Set(echo.Authorization, "")
|
|
|
|
he = ja(c).(*echo.HTTPError)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, he.Code())
|
|
|
|
|
|
|
|
// Invalid Authorization header
|
|
|
|
auth = "token"
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
|
|
|
he = ja(c).(*echo.HTTPError)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, he.Code())
|
|
|
|
|
|
|
|
// Invalid scheme
|
|
|
|
auth = "Bear token"
|
|
|
|
req.Header.Set(echo.Authorization, auth)
|
|
|
|
he = ja(c).(*echo.HTTPError)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, he.Code())
|
|
|
|
|
|
|
|
// WebSocket
|
|
|
|
c.Request().Header.Set(echo.Upgrade, echo.WebSocket)
|
|
|
|
assert.NoError(t, ja(c))
|
|
|
|
}
|