Skip to content

Commit

Permalink
Fix #339: Add endpoint to list IDPs
Browse files Browse the repository at this point in the history
Signed-off-by: George J Padayatti <[email protected]>
  • Loading branch information
georgepadayatti committed Oct 23, 2023
1 parent 0d264cb commit e144d64
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion resources/config
74 changes: 74 additions & 0 deletions src/v2/handler/idp/config_list_idps.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions src/v2/http_path/config_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/v2/http_path/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/v2/idp/idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit e144d64

Please sign in to comment.