Repository navigation
Expand file tree
/
Copy pathserver.go
More file actions
129 lines (107 loc) · 2.72 KB
/
Copy pathserver.go
File metadata and controls
129 lines (107 loc) · 2.72 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"net/http"
"github.com/dobyte/jwt"
"github.com/gogf/gcache-adapter/adapter"
"github.com/gogf/gf/database/gredis"
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
)
type Response struct {
Status int `json:"status"`
Message string `json:"message"`
Data any `json:"data"`
}
var (
auth *jwt.JWT
payload = jwt.Payload{
"uid": 1,
"account": "fuxiao",
}
)
func init() {
gredis.SetConfig(&gredis.Config{
Host: "127.0.0.1",
Port: 6379,
Db: 1,
}, "jwt")
auth, _ = jwt.NewJWT(
jwt.WithIssuer("backend"),
jwt.WithSignAlgorithm(jwt.HS256),
jwt.WithSecretKey("secret"),
jwt.WithValidDuration(3600),
jwt.WithLookupLocations("header:Authorization"),
jwt.WithStore(adapter.NewRedis(g.Redis("jwt"))),
)
}
func failed(r *ghttp.Request, status int, message string) {
_ = r.Response.WriteJsonExit(Response{
Status: status,
Message: message,
})
}
func success(r *ghttp.Request, message string, data any) {
_ = r.Response.WriteJsonExit(Response{
Status: http.StatusOK,
Message: message,
Data: data,
})
}
func middleware(r *ghttp.Request) {
request, err := auth.Http().Middleware(r.Request)
if err != nil {
switch {
case jwt.IsInvalidToken(err):
failed(r, http.StatusUnauthorized, "token is invalid")
case jwt.IsExpiredToken(err):
failed(r, http.StatusUnauthorized, "token is expired")
case jwt.IsMissingToken(err):
failed(r, http.StatusUnauthorized, "token is missing")
case jwt.IsAuthElsewhere(err):
failed(r, http.StatusUnauthorized, "auth elsewhere")
default:
failed(r, http.StatusUnauthorized, "unauthorized")
}
}
r.Request = request
r.Middleware.Next()
}
func main() {
s := g.Server()
s.Group("", func(group *ghttp.RouterGroup) {
// login
group.POST("/login", func(r *ghttp.Request) {
token, err := auth.GenerateToken("1", payload)
if err != nil {
failed(r, http.StatusBadRequest, err.Error())
}
success(r, "login success", token)
})
// logout
group.DELETE("/logout", func(r *ghttp.Request) {
if err := auth.Http().DestroyToken(r.Request); err != nil {
failed(r, http.StatusBadRequest, err.Error())
}
success(r, "logout success", nil)
})
// refresh token
group.PUT("/refresh", func(r *ghttp.Request) {
token, err := auth.Http().RefreshToken(r.Request)
if err != nil {
failed(r, http.StatusBadRequest, err.Error())
}
success(r, "refresh success", token)
})
group.Middleware(middleware)
// get user profile information
group.GET("/profile", func(r *ghttp.Request) {
info, err := auth.Http().ParseToken(r.Request)
if err != nil {
failed(r, http.StatusBadRequest, err.Error())
}
success(r, "get profile success", info)
})
})
s.SetPort(8888)
s.Run()
}