-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
65 lines (54 loc) · 1.71 KB
/
handler.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
package handler
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"github.com/rudeigerc/broker-gateway/model"
"github.com/rudeigerc/broker-gateway/service"
"github.com/spf13/viper"
)
type Error struct {
Status int `json:"status"`
Title string `json:"title"`
Message string `json:"message"`
}
func StatusHandler(c *gin.Context) {
c.JSON(200, gin.H{"message": "Normal"})
}
func ErrorHandler(c *gin.Context, code int, message string) {
response := &Error{
Status: code,
Title: http.StatusText(code),
Message: message,
}
c.JSON(code, gin.H{"error": response})
}
func validate(c *gin.Context) (model.Firm, error) {
tokenString := c.Request.Header.Get("Authorization")
if tokenString != "" {
token, _ := jwt.ParseWithClaims(strings.Split(tokenString, " ")[1], &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(viper.GetString("auth.secret")), nil
})
if claims, ok := token.Claims.(*jwt.StandardClaims); ok && token.Valid {
firmID, _ := strconv.Atoi(claims.Subject)
firm := service.Auth{}.Validate(firmID)
return firm, nil
}
}
return model.Firm{}, fmt.Errorf("[hanlder.auth.validate] [ERROR] Invalid token")
}
func adminValidate(c *gin.Context) error {
tokenString := c.Request.Header.Get("Authorization")
if tokenString != "" {
token, _ := jwt.ParseWithClaims(strings.Split(tokenString, " ")[1], &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(viper.GetString("auth.admin.secret")), nil
})
if _, ok := token.Claims.(*jwt.StandardClaims); ok && token.Valid {
return nil
}
}
return fmt.Errorf("[hanlder.auth.adminValidate] [ERROR] Invalid token")
}