Placeholder for generating token
This commit is contained in:
parent
7871c1db59
commit
e0d56f0a29
2 changed files with 24 additions and 7 deletions
17
jwt.go
17
jwt.go
|
@ -8,8 +8,11 @@ import (
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Secret string
|
Secret string
|
||||||
|
Auth AuthFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AuthFunc func(string, string) (bool, error)
|
||||||
|
|
||||||
type JWTMiddleware struct {
|
type JWTMiddleware struct {
|
||||||
config Config
|
config Config
|
||||||
}
|
}
|
||||||
|
@ -28,11 +31,19 @@ func (m *JWTMiddleware) Secure(h http.Handler) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *JWTMiddleware) Auth(w http.ResponseWriter, r *http.Request) {
|
func (m *JWTMiddleware) GenerateToken(w http.ResponseWriter, r *http.Request) {
|
||||||
var b interface{}
|
var b map[string]string
|
||||||
err := json.NewDecoder(r.Body).Decode(&b)
|
err := json.NewDecoder(r.Body).Decode(&b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
w.Write([]byte("test"))
|
result, err := m.config.Auth(b["email"], b["password"])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
resp := "failure"
|
||||||
|
if result {
|
||||||
|
resp = "success"
|
||||||
|
}
|
||||||
|
w.Write([]byte(resp))
|
||||||
}
|
}
|
||||||
|
|
14
jwt_test.go
14
jwt_test.go
|
@ -13,9 +13,14 @@ var testHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request)
|
||||||
w.Write([]byte("test"))
|
w.Write([]byte("test"))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
var authFunc = func(email, password string) (bool, error) {
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
func newJWTMiddlewareOrFatal(t *testing.T) *JWTMiddleware {
|
func newJWTMiddlewareOrFatal(t *testing.T) *JWTMiddleware {
|
||||||
config := &Config{
|
config := &Config{
|
||||||
Secret: "password",
|
Secret: "password",
|
||||||
|
Auth: authFunc,
|
||||||
}
|
}
|
||||||
middleware, err := NewMiddleware(config)
|
middleware, err := NewMiddleware(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -29,6 +34,7 @@ func TestNewJWTMiddleware(t *testing.T) {
|
||||||
if middleware.config.Secret != "password" {
|
if middleware.config.Secret != "password" {
|
||||||
t.Errorf("expected 'password', got %v", middleware.config.Secret)
|
t.Errorf("expected 'password', got %v", middleware.config.Secret)
|
||||||
}
|
}
|
||||||
|
// TODO: test auth func init
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewJWTMiddlewareNoConfig(t *testing.T) {
|
func TestNewJWTMiddlewareNoConfig(t *testing.T) {
|
||||||
|
@ -48,7 +54,7 @@ func TestSecureHandler(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAuthHandler(t *testing.T) {
|
func TestGenerateTokenHandler(t *testing.T) {
|
||||||
middleware := newJWTMiddlewareOrFatal(t)
|
middleware := newJWTMiddlewareOrFatal(t)
|
||||||
authBody := map[string]interface{}{
|
authBody := map[string]interface{}{
|
||||||
"email": "user@example.com",
|
"email": "user@example.com",
|
||||||
|
@ -58,7 +64,7 @@ func TestAuthHandler(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
ts := httptest.NewServer(http.HandlerFunc(middleware.Auth))
|
ts := httptest.NewServer(http.HandlerFunc(middleware.GenerateToken))
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
resp, err := http.Post(ts.URL, "application/json", bytes.NewReader(body))
|
resp, err := http.Post(ts.URL, "application/json", bytes.NewReader(body))
|
||||||
respBody, err := ioutil.ReadAll(resp.Body)
|
respBody, err := ioutil.ReadAll(resp.Body)
|
||||||
|
@ -66,7 +72,7 @@ func TestAuthHandler(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
if string(respBody) != "test" {
|
if string(respBody) != "success" {
|
||||||
t.Errorf("expected 'test', got %v", respBody)
|
t.Errorf("expected 'success', got %v", string(respBody))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue