-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
77 lines (68 loc) · 1.49 KB
/
router.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
// copywrite 2022 Matthew R Kasun [email protected]
package main
import (
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt/v4"
)
var SigningKey []byte
type CustomClaims struct {
jwt.RegisteredClaims
}
func SetupRouter() *gin.Engine {
router := gin.Default()
//basic routes
router.GET("/ip", GetIP)
router.POST("/login", Login)
//authenticated routes
restricted := router.Group("/api", Auth)
{
restricted.GET("/hello", Hello)
}
return router
}
func Auth(c *gin.Context) {
if len(c.Request.Header["Authorization"]) == 0 {
fail(c, "no auth header")
return
}
id, status := getFromJWT(c.Request.Header["Authorization"][0])
log.Println(id, status, time.Now())
if status == 1 {
fail(c, "token expired")
return
}
if status == 2 {
fail(c, "invalid token")
}
if id != "demo" {
fail(c, "no such user")
return
}
c.Next()
}
func fail(c *gin.Context, message string) {
c.JSON(http.StatusUnauthorized, gin.H{"message": message})
c.Abort()
}
func getFromJWT(auth string) (string, int) {
//parts := strings.Split(auth, " ")
log.Println(auth)
//if len(parts) < 2 {
//return "", nil
//}
token, err := jwt.ParseWithClaims(auth, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return SigningKey, nil
})
if err != nil {
log.Println("error from jwt parse ", err)
return "", 1
}
if claims, ok := token.Claims.(*CustomClaims); ok && token.Valid {
log.Println("claims ", claims, "token ", token.Valid)
return claims.ID, 0
}
return "", 2
}