Skip to content

Commit

Permalink
Merge pull request #429 from factly/add-token-info-endpoint
Browse files Browse the repository at this point in the history
add route at organisation level
shreeharsha-factly authored Apr 1, 2024
2 parents 3c002d0 + c3af638 commit a6d434a
Showing 2 changed files with 42 additions and 0 deletions.
41 changes: 41 additions & 0 deletions server/action/organisation/info_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package organisation

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 info_token(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})
}
1 change: 1 addition & 0 deletions server/action/organisation/route.go
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ func Router() chi.Router {
r.Get("/my", list)
r.Post("/", create)
r.Post("/token/validate", validate_token)
r.Post("/token/info", info_token)
// r.Get("/", all)
r.Route("/{organisation_id}", func(r chi.Router) {
r.Get("/", details)

0 comments on commit a6d434a

Please sign in to comment.