-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
89 lines (73 loc) · 1.94 KB
/
main.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
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
package main
import (
"os"
"time"
"github.com/appleboy/gin-jwt"
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
"github.com/maliceio/malice-api/api"
)
var (
defaultUser string
defaultPass string
port string
)
// Token is an API key used for auth
type Token struct {
Key string `form:"key" json:"key" binding:"required"`
}
func getOpt(name, dfault string) string {
value := os.Getenv(name)
if value == "" {
value = dfault
}
return value
}
func init() {
defaultUser = getOpt("USER", "admin")
defaultPass = getOpt("PASS", "admin")
port = getOpt("PORT", "8080")
}
func main() {
r := gin.New()
r.Use(gin.Logger())
r.Use(gin.Recovery())
// the jwt middleware
authMiddleware := &jwt.GinJWTMiddleware{
Realm: "malice-server",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour,
Authenticator: func(userId string, password string, c *gin.Context) (string, bool) {
if userId == defaultUser && password == defaultPass {
return userId, true
}
return userId, false
},
Authorizator: func(userId string, c *gin.Context) bool {
if userId == defaultUser {
return true
}
return false
},
Unauthorized: func(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"code": code,
"message": message,
})
},
// TokenLookup is a string in the form of "<source>:<name>" that is used
// to extract token from the request.
TokenLookup: "query:token",
// TimeFunc provides the current time. You can override it to use another time value. This is useful for testing or if your server uses a different time zone than your tokens.
TimeFunc: time.Now,
}
r.POST("/login", authMiddleware.LoginHandler)
auth := r.Group("/auth")
auth.Use(authMiddleware.MiddlewareFunc())
{
auth.GET("/hello", api.HelloHandler)
auth.GET("/refresh_token", authMiddleware.RefreshHandler)
}
endless.ListenAndServe(":"+port, r) // listen and serve on 0.0.0.0:8080
}