Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add token validation at organisation level #420

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/action/organisation/token/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func Router() chi.Router {
r.Get("/", list)
r.Post("/", create)
r.Delete("/{token_id}", delete)
r.Post("/validate", validate)

return r
}
79 changes: 79 additions & 0 deletions server/action/organisation/token/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package token

import (
"encoding/json"
"errors"
"net/http"
"strconv"

"github.com/factly/kavach-server/model"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/renderx"
"github.com/factly/x/validationx"
"github.com/go-chi/chi"
"gorm.io/gorm"
)

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

// Validate - validate organisation token
// @Summary Show a organisation token
// @Description validate organisation token
// @Tags OrganisationorganisationsTokens
// @ID validate-organisation-organisation-token
// @Produce json
// @Param X-Organisation header string true "Organisation ID"
// @Param organisation_slug path string true "Application Slug"
// @Param ValidationBody body ValidationBody true "Validation Body"
// @Success 200 {object} model.organisation
// @Router /organisations/{application_id}/tokens/validate [post]
func validate(w http.ResponseWriter, r *http.Request) {
organisation_id := chi.URLParam(r, "organisation_id")
if organisation_id == "" {
errorx.Render(w, errorx.Parser(errorx.GetMessage("invalid id", http.StatusBadRequest)))
return
}
id, err := strconv.ParseUint(organisation_id, 10, 64)
if err != nil {
errorx.Render(w, errorx.Parser(errorx.GetMessage("invalid id", http.StatusBadRequest)))
return
}
//parse applicaion_id

tokenBody := validationBody{}
err = json.NewDecoder(r.Body).Decode(&tokenBody)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}

validationError := validationx.Check(tokenBody)
if validationError != nil {
loggerx.Error(errors.New("validation error"))
errorx.Render(w, validationError)
return
}

orgToken := model.OrganisationToken{}
// Fetch all tokens for a organisation
err = model.DB.Model(&model.OrganisationToken{}).Preload("Organisation").Where(&model.OrganisationToken{
Token: tokenBody.Token, OrganisationID: uint(id),
}).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})
}
Loading