Skip to content

Commit

Permalink
Merge pull request #428 from factly/add-token-info-endpoint
Browse files Browse the repository at this point in the history
add token info endpoint on organisation level
  • Loading branch information
shreeharsha-factly authored Apr 1, 2024
2 parents e0de424 + a9224dd commit 3c002d0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/action/organisation/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/go-chi/chi"
)

//create - Create token for an organisation using organisation_id
// create - Create token for an organisation using organisation_id
// @Summary Create token for an organisation using organisation_id
// @Description Create token for an organisation using organisation_id
// @Tags OrganisationTokens
Expand Down
2 changes: 1 addition & 1 deletion server/action/organisation/token/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func delete(w http.ResponseWriter, r *http.Request) {

result := &model.OrganisationToken{}
result.ID = uint(tokID)

//check if organisation token exists
err = model.DB.Model(&model.OrganisationToken{}).Where(&model.OrganisationToken{
Base: model.Base{
Expand Down
41 changes: 41 additions & 0 deletions server/action/organisation/token/details.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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})
}
5 changes: 5 additions & 0 deletions server/action/organisation/token/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ type organisationToken struct {
Organisation *model.Organisation `gorm:"foreignKey:organisation_id" json:"organisation"`
}

type validationBody struct {
Token string `json:"token" validate:"required"`
}

func Router() chi.Router {
r := chi.NewRouter()
r.Get("/", list)
r.Post("/", create)
r.Delete("/{token_id}", delete)
r.Post("/validate", validate)
r.Post("/info", tokenInfo)

return r
}
3 changes: 0 additions & 3 deletions server/action/organisation/token/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (
)

// validationBody request body
type validationBody struct {
Token string `json:"token" validate:"required"`
}

// Validate - validate organisation token
// @Summary Show a organisation token
Expand Down

0 comments on commit 3c002d0

Please sign in to comment.