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

add token info endpoint on organisation level #428

Merged
merged 2 commits into from
Apr 1, 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
2 changes: 1 addition & 1 deletion server/action/organisation/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/go-chi/chi"
)

//create - Create token for an organisation using organisation_id
// create - Create token for an organisation using organisation_id
// @Summary Create token for an organisation using organisation_id
// @Description Create token for an organisation using organisation_id
// @Tags OrganisationTokens
Expand Down
2 changes: 1 addition & 1 deletion server/action/organisation/token/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func delete(w http.ResponseWriter, r *http.Request) {

result := &model.OrganisationToken{}
result.ID = uint(tokID)

//check if organisation token exists
err = model.DB.Model(&model.OrganisationToken{}).Where(&model.OrganisationToken{
Base: model.Base{
Expand Down
41 changes: 41 additions & 0 deletions server/action/organisation/token/details.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package token

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 tokenInfo(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})
}
5 changes: 5 additions & 0 deletions server/action/organisation/token/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ type organisationToken struct {
Organisation *model.Organisation `gorm:"foreignKey:organisation_id" json:"organisation"`
}

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

func Router() chi.Router {
r := chi.NewRouter()
r.Get("/", list)
r.Post("/", create)
r.Delete("/{token_id}", delete)
r.Post("/validate", validate)
r.Post("/info", tokenInfo)

return r
}
3 changes: 0 additions & 3 deletions server/action/organisation/token/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import (
)

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

// Validate - validate organisation token
// @Summary Show a organisation token
Expand Down
Loading