-
Notifications
You must be signed in to change notification settings - Fork 7
/
token.go
113 lines (89 loc) · 2.69 KB
/
token.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package controllers
import (
"net/http"
"github.com/uploadexpress/app/services/config"
"github.com/gin-gonic/gin"
"github.com/uploadexpress/app/helpers"
"github.com/uploadexpress/app/helpers/params"
"github.com/uploadexpress/app/models"
"github.com/uploadexpress/app/store"
)
// TokenController holds all controller functions related to the Token entity
type TokenController struct{}
// NewTokenController instantiates of the controller
func NewTokenController() TokenController {
return TokenController{}
}
// CreateToken to create a new Token
func (tc TokenController) CreateToken(c *gin.Context) {
token := &models.Token{}
if err := c.BindJSON(token); err != nil {
c.AbortWithError(http.StatusBadRequest, helpers.ErrorWithCode("invalid_input", "Failed to bind the body data", err))
return
}
token.Ip = c.ClientIP()
if err := store.CreateToken(c, token); err != nil {
c.Error(err)
c.Abort()
return
}
secret := config.GetString(c, "jwt_secret")
accessToken, err := helpers.GenerateApiToken(secret, store.Current(c).Id, token.Id)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, helpers.ErrorWithCode("token_generation_failed", "Could not generate the access token", err))
return
}
// Only sent once
token.Token = accessToken
c.JSON(http.StatusCreated, token)
}
// GetToken from id (in context)
func (tc TokenController) GetToken(c *gin.Context) {
token, err := store.FindTokenById(c, c.Param("id"))
if err != nil {
c.AbortWithError(http.StatusNotFound, helpers.ErrorWithCode("Token_not_found", "The token does not exist", err))
return
}
c.JSON(http.StatusOK, token)
}
// GetAllTokens to get all Tokens
func (tc TokenController) GetAllTokens(c *gin.Context) {
tokens, err := store.GetAllTokens(c)
if err != nil {
c.Error(err)
c.Abort()
return
}
c.JSON(http.StatusOK, tokens)
}
//UpdateToken updates the Token entity
func (tc TokenController) UpdateToken(c *gin.Context) {
newToken := models.Token{}
err := c.BindJSON(&newToken)
if err != nil {
c.AbortWithError(http.StatusBadRequest, helpers.ErrorWithCode("invalid_input", "Failed to bind the body data", err))
return
}
_, err = store.FindTokenById(c, c.Param("id"))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, helpers.ErrorWithCode("Token_not_found", "Failed to find Token id", err))
return
}
err = store.UpdateToken(c, c.Param("id"), params.M{"$set": newToken})
if err != nil {
c.Error(err)
c.Abort()
return
}
c.JSON(http.StatusOK, nil)
}
// DeleteToken to delete an existing Token
func (Tc TokenController) DeleteToken(c *gin.Context) {
err := store.DeleteToken(c, c.Param("id"))
if err != nil {
c.Error(err)
c.Abort()
return
}
c.JSON(http.StatusOK, nil)
}