Skip to content

Commit 89e6635

Browse files
committed
add token info endpoint
1 parent e0de424 commit 89e6635

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

server/action/organisation/token/create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
"github.com/go-chi/chi"
1616
)
1717

18-
//create - Create token for an organisation using organisation_id
18+
// create - Create token for an organisation using organisation_id
1919
// @Summary Create token for an organisation using organisation_id
2020
// @Description Create token for an organisation using organisation_id
2121
// @Tags OrganisationTokens

server/action/organisation/token/delete.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func delete(w http.ResponseWriter, r *http.Request) {
5555

5656
result := &model.OrganisationToken{}
5757
result.ID = uint(tokID)
58-
58+
5959
//check if organisation token exists
6060
err = model.DB.Model(&model.OrganisationToken{}).Where(&model.OrganisationToken{
6161
Base: model.Base{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package token
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
7+
"github.com/factly/kavach-server/model"
8+
"github.com/factly/x/errorx"
9+
"github.com/factly/x/loggerx"
10+
"github.com/factly/x/renderx"
11+
"gorm.io/gorm"
12+
)
13+
14+
func tokenInfo(w http.ResponseWriter, r *http.Request) {
15+
tokenBody := validationBody{}
16+
17+
err := json.NewDecoder(r.Body).Decode(&tokenBody)
18+
if err != nil {
19+
loggerx.Error(err)
20+
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
21+
return
22+
}
23+
24+
orgToken := model.OrganisationToken{}
25+
// to need to specify the organisation id as token itself is unique
26+
err = model.DB.Model(&model.OrganisationToken{}).Preload("Organisation").Where(&model.OrganisationToken{
27+
Token: tokenBody.Token,
28+
}).First(&orgToken).Error
29+
30+
if err != nil {
31+
loggerx.Error(err)
32+
if err == gorm.ErrRecordNotFound {
33+
renderx.JSON(w, http.StatusUnauthorized, map[string]interface{}{"valid": false})
34+
return
35+
}
36+
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
37+
return
38+
}
39+
40+
renderx.JSON(w, http.StatusOK, map[string]interface{}{"valid": true, "organisation_id": orgToken.OrganisationID, "token_id": orgToken.ID})
41+
}

server/action/organisation/token/route.go

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ type organisationToken struct {
1313
Organisation *model.Organisation `gorm:"foreignKey:organisation_id" json:"organisation"`
1414
}
1515

16+
type validationBody struct {
17+
Token string `json:"token" validate:"required"`
18+
}
19+
1620
func Router() chi.Router {
1721
r := chi.NewRouter()
1822
r.Get("/", list)

0 commit comments

Comments
 (0)