Skip to content

Commit

Permalink
Merge pull request #422 from factly/remove-headers
Browse files Browse the repository at this point in the history
remove unused headers from validate token routes
  • Loading branch information
shreeharsha-factly authored Mar 9, 2024
2 parents fdcb5fa + c3dfe75 commit 560b55b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 46 deletions.
18 changes: 1 addition & 17 deletions server/action/organisation/application/space/token/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,18 @@ 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"
)



func Validate(w http.ResponseWriter, r *http.Request) {
sID := chi.URLParam(r, "space_id")
spaceID, err := strconv.Atoi(sID)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}

tokenBody := model.ValidationBody{}
err = json.NewDecoder(r.Body).Decode(&tokenBody)
err := json.NewDecoder(r.Body).Decode(&tokenBody)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
Expand All @@ -50,10 +39,5 @@ func Validate(w http.ResponseWriter, r *http.Request) {
return
}

if spaceToken.SpaceID != uint(spaceID) {
renderx.JSON(w, http.StatusUnauthorized, map[string]interface{}{"valid": false})
return
}

renderx.JSON(w, http.StatusOK, map[string]interface{}{"valid": true})
}
17 changes: 3 additions & 14 deletions server/action/organisation/application/token/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ 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"
)

Expand All @@ -32,20 +30,10 @@ type validationBody struct {
// @Success 200 {object} model.Application
// @Router /applications/{application_id}/tokens/validate [post]
func validate(w http.ResponseWriter, r *http.Request) {
applicaion_id := chi.URLParam(r, "application_id")
// if applicaion_id == "" {
// errorx.Render(w, errorx.Parser(errorx.GetMessage("invalid id", http.StatusBadRequest)))
// return
// }
id, err := strconv.ParseUint(applicaion_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)
err := json.NewDecoder(r.Body).Decode(&tokenBody)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
Expand All @@ -61,8 +49,9 @@ func validate(w http.ResponseWriter, r *http.Request) {

appToken := model.ApplicationToken{}
// Fetch all tokens for a application
// to need to specify the organisation id as token itself is unique
err = model.DB.Model(&model.ApplicationToken{}).Preload("Application").Where(&model.ApplicationToken{
Token: tokenBody.Token, ApplicationID: uint(id),
Token: tokenBody.Token,
}).First(&appToken).Error

if err != nil {
Expand Down
18 changes: 3 additions & 15 deletions server/action/organisation/token/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ 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"
)

Expand All @@ -32,20 +30,10 @@ type validationBody struct {
// @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)
err := json.NewDecoder(r.Body).Decode(&tokenBody)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
Expand All @@ -60,9 +48,9 @@ func validate(w http.ResponseWriter, r *http.Request) {
}

orgToken := model.OrganisationToken{}
// Fetch all tokens for a organisation
// 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, OrganisationID: uint(id),
Token: tokenBody.Token,
}).First(&orgToken).Error

if err != nil {
Expand Down

0 comments on commit 560b55b

Please sign in to comment.