-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvalidate.go
67 lines (58 loc) · 1.89 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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"
"gorm.io/gorm"
)
// validationBody request body
type validationBody struct {
Token string `json:"token" validate:"required"`
}
// Validate - validate organisation token
// @Summary Show a organisation token
// @Description validate organisation token
// @Tags OrganisationorganisationsTokens
// @ID validate-organisation-organisation-token
// @Produce json
// @Param X-Organisation header string true "Organisation ID"
// @Param organisation_slug path string true "Application Slug"
// @Param ValidationBody body ValidationBody true "Validation Body"
// @Success 200 {object} model.organisation
// @Router /organisations/{application_id}/tokens/validate [post]
func validate(w http.ResponseWriter, r *http.Request) {
//parse applicaion_id
tokenBody := 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
}
orgToken := model.OrganisationToken{}
// to need to specify the organisation id as token itself is unique
err = model.DB.Model(&model.OrganisationToken{}).Preload("Organisation").Where(&model.OrganisationToken{
Token: tokenBody.Token,
}).First(&orgToken).Error
if err != nil {
loggerx.Error(err)
if err == gorm.ErrRecordNotFound {
renderx.JSON(w, http.StatusUnauthorized, map[string]interface{}{"valid": false})
return
}
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
renderx.JSON(w, http.StatusOK, map[string]interface{}{"valid": true})
}