add token auth tests

This commit is contained in:
Iris Lightshard 2022-08-02 21:17:53 -06:00
parent 92f0f035a9
commit 10ecb3058f
Signed by: Iris Lightshard
GPG key ID: 3B7FBC22144E6398
2 changed files with 9 additions and 6 deletions

View file

@ -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)

View file

@ -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"))