mirror of
https://github.com/wahyd4/loginsrv.git
synced 2026-08-09 04:46:29 +10:00
honor expiry time on login info page
This commit is contained in:
+6
-2
@@ -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) {
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user