-
Notifications
You must be signed in to change notification settings - Fork 1
/
refresh.go
56 lines (45 loc) · 1.79 KB
/
refresh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package views
import (
"net/http"
"os"
"strings"
"time"
"github.com/andela-sjames/go-bucketlist-api/auth"
"github.com/andela-sjames/go-bucketlist-api/models"
"github.com/andela-sjames/go-bucketlist-api/utils"
jwt "github.com/dgrijalva/jwt-go"
)
// RefreshHandler functioned defined to handle renewal of close to expire token
func RefreshHandler(w http.ResponseWriter, r *http.Request) {
userObj := r.Context().Value(auth.CtxKey).(map[string]interface{})
userID := userObj["userID"].(uint)
userEmail := userObj["userEmail"].(string)
tokenHeader := r.Header.Get("Authorization")
splitted := strings.Split(tokenHeader, " ")
tokenPart := splitted[1]
claims := &models.Token{}
_, err := jwt.ParseWithClaims(tokenPart, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(os.Getenv("PASSPHRASE")), nil
})
// We ensure that a new token is not issued until enough time has elapsed
// In this case, a new token will only be issued if the old token is within
// 60 seconds of expiry. Otherwise, return a bad request status
if time.Unix(claims.ExpiresAt, 0).Sub(time.Now()) > 60*time.Second {
response := utils.Message(false, "Token refresh only applies to a valid token with less than 60s to expire")
w.WriteHeader(http.StatusBadRequest)
w.Header().Add("Content-Type", "application/json")
utils.Respond(w, response)
return
}
// Create new JWT for the current use, with a renewed expiration time
newClaims := models.GenerateUserClaims(userID, userEmail)
newToken := jwt.NewWithClaims(jwt.GetSigningMethod("HS256"), newClaims)
newTokenString, err := newToken.SignedString([]byte(os.Getenv("PASSPHRASE")))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
resp := utils.Message(true, "success")
resp["token"] = newTokenString
utils.Respond(w, resp)
}