-
Notifications
You must be signed in to change notification settings - Fork 0
/
refreshjwttoken.go
57 lines (46 loc) · 1.21 KB
/
refreshjwttoken.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
57
package main
import (
"net/http"
acmeserverless "github.com/retgits/acme-serverless"
"github.com/valyala/fasthttp"
)
// RefreshJWTToken ...
func RefreshJWTToken(ctx *fasthttp.RequestCtx) {
login, err := acmeserverless.UnmarshalLoginResponse(string(ctx.Request.Body()))
if err != nil {
ErrorHandler(ctx, "RefreshJWTToken", "UnmarshalLoginResponse", err)
return
}
valid, id, _, err := ValidateToken(login.RefreshToken)
if !valid || id == "" {
res := acmeserverless.VerifyTokenResponse{
Message: "Invalid Key. User Not Authorized",
Status: http.StatusForbidden,
}
payload, err := res.Marshal()
if err != nil {
ErrorHandler(ctx, "RefreshJWTToken", "Marshal", err)
return
}
ctx.SetStatusCode(http.StatusOK)
ctx.Write(payload)
return
}
newToken, err := GenerateAccessToken("eric", id)
if err != nil {
ErrorHandler(ctx, "RefreshJWTToken", "GenerateAccessToken", err)
return
}
res := acmeserverless.LoginResponse{
AccessToken: newToken,
RefreshToken: login.RefreshToken,
Status: http.StatusOK,
}
payload, err := res.Marshal()
if err != nil {
ErrorHandler(ctx, "RefreshJWTToken", "Marshal", err)
return
}
ctx.SetStatusCode(http.StatusOK)
ctx.Write(payload)
}