-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (57 loc) · 1.63 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
package api
import (
checker "checker/internal/core"
"checker/internal/tokens"
"net/http"
"github.com/gin-gonic/gin"
)
type AuthRequest struct {
Token string `json:"token,omitempty" binding:"required"`
checker.AccessData
}
type Service struct {
authInfo *checker.AuthInfo
JWTPublicKey string
}
func NewService(authInfo *checker.AuthInfo, JWTPublicKey string) *Service {
return &Service{authInfo: authInfo, JWTPublicKey: JWTPublicKey}
}
func (s *Service) RegisterHandlers(router *gin.Engine) {
router.POST("/auth", s.CheckAccess)
router.POST("/auth/", s.CheckAccess)
}
func (s *Service) CheckAccess(c *gin.Context) {
// Parse request
var request AuthRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
token, err := tokens.DecodeToken(request.Token, s.JWTPublicKey)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
clientID, err := tokens.GetClientID(token)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
hasAccess, err := s.authInfo.CheckAccess(&request.AccessData, clientID)
if err != nil {
switch err.(type) {
case *checker.NotFoundError:
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
return
default:
c.JSON(http.StatusServiceUnavailable, gin.H{"error": err.Error()})
return
}
}
// Build response
if hasAccess {
c.IndentedJSON(http.StatusOK, gin.H{"message": "Access permit", "access": true, "client": clientID})
} else {
c.IndentedJSON(http.StatusForbidden, gin.H{"message": "Access denied", "access": false, "client": clientID})
}
}