honor expiry time on login info page

This commit is contained in:
Sebastian Mancke
2017-05-13 14:28:15 +02:00
parent 2c76f02671
commit 733ce7cf1e
3 changed files with 15 additions and 3 deletions
+6 -2
View File
@@ -229,8 +229,12 @@ func (h *Handler) getToken(r *http.Request) (userInfo model.UserInfo, valid bool
return model.UserInfo{}, false
}
u, v := token.Claims.(*model.UserInfo)
return *u, v
u, ok := token.Claims.(*model.UserInfo)
if !ok {
return model.UserInfo{}, false
}
return *u, u.Valid() == nil
}
func (h *Handler) respondError(w http.ResponseWriter, r *http.Request) {
+1 -1
View File
@@ -307,7 +307,7 @@ func TestHandler_LoginError(t *testing.T) {
func TestHandler_getToken_Valid(t *testing.T) {
h := testHandler()
input := model.UserInfo{Sub: "marvin"}
input := model.UserInfo{Sub: "marvin", Expiry: time.Now().Add(time.Second).Unix()}
token, err := h.createToken(input)
assert.NoError(t, err)
r := &http.Request{
+8
View File
@@ -1,5 +1,10 @@
package model
import (
"errors"
"time"
)
type UserInfo struct {
Sub string `json:"sub"`
Picture string `json:"picture,omitempty"`
@@ -12,5 +17,8 @@ type UserInfo struct {
// this interface implementation
// lets us use the user info as Claim for jwt-go
func (u UserInfo) Valid() error {
if u.Expiry < time.Now().Unix() {
return errors.New("token expired")
}
return nil
}