Skip to content

Commit

Permalink
Merge pull request #426 from factly/change-validate-token-routes
Browse files Browse the repository at this point in the history
feat: add new validation routes
  • Loading branch information
shreeharsha-factly authored Mar 13, 2024
2 parents f8a29b6 + 4204087 commit 7999d0d
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 6 deletions.
3 changes: 1 addition & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ services:
restart: on-failure
networks:
- kavach

###########################################################################
####### KAVACH #######
####### KAVACH #######
###########################################################################
kavach-server:
depends_on:
Expand Down
1 change: 1 addition & 0 deletions server/action/organisation/application/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func Router() chi.Router {
r.Post("/", create)
r.Get("/", list)
r.Get("/default", listDefault)
r.Post("/token/validate", validate_token)
r.Get("/{application_slug}/access", access)
r.Route("/{application_id}", func(r chi.Router) {
r.Get("/", details)
Expand Down
4 changes: 3 additions & 1 deletion server/action/organisation/application/space/route.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package space

import (
"github.com/factly/kavach-server/action/organisation/application/space/policy"
"github.com/factly/kavach-server/action/organisation/application/space/roles"
"github.com/factly/kavach-server/action/organisation/application/space/token"
"github.com/factly/kavach-server/action/organisation/application/space/user"
"github.com/factly/kavach-server/action/organisation/application/space/policy"
"github.com/go-chi/chi"
)

const namespace string = "spaces"
const appNamespace string = "applications"

// Router organisation
func Router() chi.Router {
r := chi.NewRouter()

r.Post("/", create)
r.Get("/", list)
r.Post("/token/validate", validate_token)
r.Route("/{space_id}", func(r chi.Router) {
r.Mount("/users", user.Router())
r.Delete("/", delete)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Validate(w http.ResponseWriter, r *http.Request) {
spaceToken := model.SpaceToken{}
err = model.DB.Model(&model.SpaceToken{}).Where(&model.SpaceToken{
Token: tokenBody.Token,
}).Find(&spaceToken).Error
}).First(&spaceToken).Error
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
Expand Down
46 changes: 46 additions & 0 deletions server/action/organisation/application/space/validate_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package space

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

"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"
)

func validate_token(w http.ResponseWriter, r *http.Request) {

tokenBody := model.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
}
fmt.Println(tokenBody.Token)

spaceToken := model.SpaceToken{}
err = model.DB.Model(&model.SpaceToken{}).Where(&model.SpaceToken{
Token: tokenBody.Token,
}).First(&spaceToken).Error

if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.GetMessage("invalid space token", 403)))
return
}

renderx.JSON(w, http.StatusOK, map[string]interface{}{"valid": true})
}
68 changes: 68 additions & 0 deletions server/action/organisation/application/validate_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package application

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

"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"
"gorm.io/gorm"
)

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

// Validate - validate_token application token
// @Summary Show a application token
// @Description validate_token application token
// @Tags OrganisationApplicationsTokens
// @ID validate_token-organisation-application-token
// @Produce json
// @Param X-Organisation header string true "Organisation ID"
// @Param application_slug path string true "Application Slug"
// @Param ValidationBody body ValidationBody true "Validation Body"
// @Success 200 {object} model.Application
// @Router /applications/{application_id}/tokens/validate_token [post]
func validate_token(w http.ResponseWriter, r *http.Request) {
//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
}

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{}).Where(&model.ApplicationToken{
Token: tokenBody.Token,
}).First(&appToken).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})
}
7 changes: 5 additions & 2 deletions server/action/organisation/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ import (
)

type orgWithRole struct {
Organisation model.Organisation `json:"organisation"`
Permission model.OrganisationUser `json:"permission"`
Organisation model.Organisation `json:"organisation"`
Permission model.OrganisationUser `json:"permission"`
AllApplications []model.Application `json:"applications,omitempty"`
}

var userContext model.ContextKey = "organisation_user"

const namespace string = "organisations"

// Router organisation
func Router() chi.Router {
r := chi.NewRouter()

r.Get("/my", list)
r.Post("/", create)
r.Post("/token/validate", validate_token)
// r.Get("/", all)
r.Route("/{organisation_id}", func(r chi.Router) {
r.Get("/", details)
Expand Down
67 changes: 67 additions & 0 deletions server/action/organisation/validate_token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package organisation

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

"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"
"gorm.io/gorm"
)

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

// Validate - validate_token organisation token
// @Summary Show a organisation token
// @Description validate_token organisation token
// @Tags OrganisationorganisationsTokens
// @ID validate_token-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_token [post]
func validate_token(w http.ResponseWriter, r *http.Request) {
//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{}
// 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})
}

0 comments on commit 7999d0d

Please sign in to comment.