diff --git a/server/action/organisation/token/create.go b/server/action/organisation/token/create.go index 64d883f4..dadb4048 100644 --- a/server/action/organisation/token/create.go +++ b/server/action/organisation/token/create.go @@ -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 diff --git a/server/action/organisation/token/delete.go b/server/action/organisation/token/delete.go index 29f10ab5..99de381c 100644 --- a/server/action/organisation/token/delete.go +++ b/server/action/organisation/token/delete.go @@ -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{ diff --git a/server/action/organisation/token/details.go b/server/action/organisation/token/details.go new file mode 100644 index 00000000..0aa22705 --- /dev/null +++ b/server/action/organisation/token/details.go @@ -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}) +} diff --git a/server/action/organisation/token/route.go b/server/action/organisation/token/route.go index add72371..e7d654e1 100644 --- a/server/action/organisation/token/route.go +++ b/server/action/organisation/token/route.go @@ -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 } diff --git a/server/action/organisation/token/validate.go b/server/action/organisation/token/validate.go index 2d9b9ba2..07e1f873 100644 --- a/server/action/organisation/token/validate.go +++ b/server/action/organisation/token/validate.go @@ -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