diff --git a/server/action/organisation/info_token.go b/server/action/organisation/info_token.go new file mode 100644 index 00000000..f7fe5dca --- /dev/null +++ b/server/action/organisation/info_token.go @@ -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}) +} diff --git a/server/action/organisation/route.go b/server/action/organisation/route.go index 85e94ad9..91d4f969 100644 --- a/server/action/organisation/route.go +++ b/server/action/organisation/route.go @@ -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)