From e144d641b5113decf57e825696f43ae4cdb9e111 Mon Sep 17 00:00:00 2001 From: George J Padayatti Date: Mon, 23 Oct 2023 21:20:11 +0530 Subject: [PATCH] Fix #339: Add endpoint to list IDPs Signed-off-by: George J Padayatti --- resources/config | 2 +- src/v2/handler/idp/config_list_idps.go | 74 ++++++++++++++++++++++++++ src/v2/http_path/config_paths.go | 1 + src/v2/http_path/routes.go | 1 + src/v2/idp/idp.go | 2 +- 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 src/v2/handler/idp/config_list_idps.go diff --git a/resources/config b/resources/config index 09f79d0..ccedc32 160000 --- a/resources/config +++ b/resources/config @@ -1 +1 @@ -Subproject commit 09f79d0172c3672c4cb85bb8823e614077afc642 +Subproject commit ccedc329ebc0c3d904ea96b697880173b51aaa56 diff --git a/src/v2/handler/idp/config_list_idps.go b/src/v2/handler/idp/config_list_idps.go new file mode 100644 index 0000000..f1b28e9 --- /dev/null +++ b/src/v2/handler/idp/config_list_idps.go @@ -0,0 +1,74 @@ +package idp + +import ( + "context" + "encoding/json" + "errors" + "net/http" + + "github.com/bb-consent/api/src/common" + "github.com/bb-consent/api/src/config" + "github.com/bb-consent/api/src/v2/idp" + "github.com/bb-consent/api/src/v2/paginate" +) + +func returnHTTPResponse(resp interface{}, w http.ResponseWriter) { + response, _ := json.Marshal(resp) + w.Header().Set(config.ContentTypeHeader, config.ContentTypeJSON) + w.WriteHeader(http.StatusOK) + w.Write(response) +} + +type listIdpsResp struct { + Idps interface{} `json:"idps"` + Pagination paginate.Pagination `json:"pagination"` +} + +// ConfigListIdps +func ConfigListIdps(w http.ResponseWriter, r *http.Request) { + + // Headers + organisationId := r.Header.Get(config.OrganizationId) + organisationId = common.Sanitize(organisationId) + + var resp listIdpsResp + + // Query params + offset, limit := paginate.ParsePaginationQueryParams(r) + + // Repository + idpRepo := idp.IdentityProviderRepository{} + idpRepo.Init(organisationId) + + // Paginate idps + var idps []idp.IdentityProvider + query := paginate.PaginateDBObjectsQuery{ + Filter: idpRepo.DefaultFilter, + Collection: idp.Collection(), + Context: context.Background(), + Limit: limit, + Offset: offset, + } + result, err := paginate.PaginateDBObjects(query, &idps) + if err != nil { + if errors.Is(err, paginate.EmptyDBError) { + emptyIdps := make([]interface{}, 0) + resp = listIdpsResp{ + Idps: emptyIdps, + Pagination: result.Pagination, + } + returnHTTPResponse(resp, w) + return + } + m := "Failed to paginate idps" + common.HandleErrorV2(w, http.StatusInternalServerError, m, err) + return + + } + resp = listIdpsResp{ + Idps: result.Items, + Pagination: result.Pagination, + } + + returnHTTPResponse(resp, w) +} diff --git a/src/v2/http_path/config_paths.go b/src/v2/http_path/config_paths.go index 191e9d2..7306f3b 100644 --- a/src/v2/http_path/config_paths.go +++ b/src/v2/http_path/config_paths.go @@ -45,6 +45,7 @@ const AddIdentityProvider = "/v2/config/idp/open-id" const UpdateIdentityProvider = "/v2/config/idp/open-id/{idpId}" const DeleteIdentityProvider = "/v2/config/idp/open-id/{idpId}" const GetIdentityProvider = "/v2/config/idp/open-id/{idpId}" +const ConfigListIdentityProviders = "/v2/config/idps" // Individuals const ConfigCreateIndividual = "/v2/config/individual" diff --git a/src/v2/http_path/routes.go b/src/v2/http_path/routes.go index 84d0fd6..fdf8953 100644 --- a/src/v2/http_path/routes.go +++ b/src/v2/http_path/routes.go @@ -61,6 +61,7 @@ func SetRoutes(r *mux.Router, e *casbin.Enforcer) { r.Handle(UpdateIdentityProvider, m.Chain(idpHandler.UpdateIdentityProvider, m.Logger(), m.Authorize(e), m.Authenticate(), m.AddContentType())).Methods("PUT") r.Handle(DeleteIdentityProvider, m.Chain(idpHandler.DeleteIdentityProvider, m.Logger(), m.Authorize(e), m.Authenticate(), m.AddContentType())).Methods("DELETE") r.Handle(GetIdentityProvider, m.Chain(idpHandler.GetIdentityProvider, m.Logger(), m.Authorize(e), m.Authenticate(), m.AddContentType())).Methods("GET") + r.Handle(ConfigListIdentityProviders, m.Chain(idpHandler.ConfigListIdps, m.Logger(), m.Authorize(e), m.Authenticate(), m.AddContentType())).Methods("GET") // Individual related api(s) r.Handle(ConfigReadIndividual, m.Chain(individualHandler.ConfigReadIndividual, m.Logger(), m.Authorize(e), m.SetApplicationMode(), m.Authenticate(), m.AddContentType())).Methods("GET") diff --git a/src/v2/idp/idp.go b/src/v2/idp/idp.go index 371ac12..a23fb5e 100644 --- a/src/v2/idp/idp.go +++ b/src/v2/idp/idp.go @@ -17,7 +17,7 @@ func Collection() *mongo.Collection { type IdentityProvider struct { Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` IssuerUrl string `json:"issuerUrl"` - AuthorizationURL string `json:"authorizationUrl" valid:"required"` + AuthorizationURL string `json:"authorisationUrl" valid:"required"` TokenURL string `json:"tokenUrl" valid:"required"` LogoutURL string `json:"logoutUrl" valid:"required"` ClientID string `json:"clientId" valid:"required"`