-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #426 from factly/change-validate-token-routes
feat: add new validation routes
- Loading branch information
Showing
8 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
server/action/organisation/application/space/validate_token.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |