From 10ecb3058f785f2ee5ddd3b3c8951d12cccf7309 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Tue, 2 Aug 2022 21:17:53 -0600 Subject: [PATCH] add token auth tests --- middleware/middleware.go | 12 ++++++------ quartzgun_test.go | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/middleware/middleware.go b/middleware/middleware.go index 046f5c9..30dd4cc 100644 --- a/middleware/middleware.go +++ b/middleware/middleware.go @@ -12,9 +12,9 @@ import ( ) type TokenPayload struct { - access_token string - token_type string - expires_in int + AccessToken string `json:"access_token"` + TokenType string `json:"token_type"` + ExpiresIn int `json:"expires_in"` } func Protected(next http.Handler, method string, userStore auth.UserStore, login string) http.Handler { @@ -91,9 +91,9 @@ func Provision(userStore auth.UserStore, ttl int) http.Handler { token, err := userStore.GrantToken(user, password, ttl) if err == nil { token := TokenPayload{ - access_token: token, - token_type: "bearer", - expires_in: ttl, + AccessToken: token, + TokenType: "bearer", + ExpiresIn: ttl, } util.AddContextValue(req, "token", token) renderer.JSON("token").ServeHTTP(w, req) diff --git a/quartzgun_test.go b/quartzgun_test.go index ca7b2be..b555d90 100644 --- a/quartzgun_test.go +++ b/quartzgun_test.go @@ -49,6 +49,9 @@ func TestMain(m *testing.M) { rtr.Post("/login", middleware.Authorize("/", udb, "/login?tryagain=1", 120)) + rtr.Post("/provision", middleware.Provision(udb, 60)) + rtr.Get("/protected", middleware.Validate(renderer.Template("testData/templates/test.html"), udb, map[string]string{})) + rtr.Get("/", middleware.Protected( renderer.Template( "testData/templates/test.html"), http.MethodGet, udb, "/login"))