-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdetails.go
41 lines (34 loc) · 1.09 KB
/
details.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
package token
import (
"encoding/json"
"net/http"
"github.com/factly/kavach-server/model"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"gorm.io/gorm"
)
func tokenInfo(w http.ResponseWriter, r *http.Request) {
tokenBody := validationBody{}
err := json.NewDecoder(r.Body).Decode(&tokenBody)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
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, "organisation_id": orgToken.OrganisationID, "token_id": orgToken.ID})
}