-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvalidate.go
43 lines (36 loc) · 999 Bytes
/
validate.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
package token
import (
"encoding/json"
"errors"
"net/http"
"github.com/factly/kavach-server/model"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/validationx"
)
func Validate(w http.ResponseWriter, r *http.Request) {
tokenBody := model.ValidationBody{}
err := json.NewDecoder(r.Body).Decode(&tokenBody)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
validationError := validationx.Check(tokenBody)
if validationError != nil {
loggerx.Error(errors.New("validation error"))
errorx.Render(w, validationError)
return
}
spaceToken := model.SpaceToken{}
err = model.DB.Model(&model.SpaceToken{}).Where(&model.SpaceToken{
Token: tokenBody.Token,
}).Find(&spaceToken).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
renderx.JSON(w, http.StatusOK, map[string]interface{}{"valid": true})
}