Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
Unify basic and advanced configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
zdevaty committed Feb 1, 2024
1 parent bdb168f commit 130a2ee
Show file tree
Hide file tree
Showing 17 changed files with 1,811 additions and 3,416 deletions.
2 changes: 1 addition & 1 deletion apiserver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ To see how to make this your own, look here:
[README](https://openapi-generator.tech)

- API version: 1.0.0
- Build date: 2024-01-24T17:20:07.525819443Z[Etc/UTC]
- Build date: 2024-01-25T12:04:37.338334425Z[Etc/UTC]


### Running the server
Expand Down
12 changes: 4 additions & 8 deletions apiserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ import (
// The ConfigurationAPIRouter implementation should parse necessary information from the http request,
// pass the data to a ConfigurationAPIServicer to perform the required actions, then write the service results to the http response.
type ConfigurationAPIRouter interface {
GetAdvancedConfiguration(http.ResponseWriter, *http.Request)
GetAttributeMapping(http.ResponseWriter, *http.Request)
GetBasicConfiguration(http.ResponseWriter, *http.Request)
GetConfiguration(http.ResponseWriter, *http.Request)
GetPermissionMapping(http.ResponseWriter, *http.Request)
PutAdvancedConfiguration(http.ResponseWriter, *http.Request)
PutAttributeMapping(http.ResponseWriter, *http.Request)
PutBasicConfiguration(http.ResponseWriter, *http.Request)
PutConfiguration(http.ResponseWriter, *http.Request)
PutPermissionMapping(http.ResponseWriter, *http.Request)
}

Expand Down Expand Up @@ -57,13 +55,11 @@ type VersionAPIRouter interface {
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type ConfigurationAPIServicer interface {
GetAdvancedConfiguration(context.Context) (ImplResponse, error)
GetAttributeMapping(context.Context) (ImplResponse, error)
GetBasicConfiguration(context.Context) (ImplResponse, error)
GetConfiguration(context.Context) (ImplResponse, error)
GetPermissionMapping(context.Context) (ImplResponse, error)
PutAdvancedConfiguration(context.Context, AdvancedConfiguration) (ImplResponse, error)
PutAttributeMapping(context.Context, AttributeMap) (ImplResponse, error)
PutBasicConfiguration(context.Context, BasicConfiguration) (ImplResponse, error)
PutConfiguration(context.Context, Configuration) (ImplResponse, error)
PutPermissionMapping(context.Context, Permissions) (ImplResponse, error)
}

Expand Down
81 changes: 16 additions & 65 deletions apiserver/api_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,40 +50,30 @@ func NewConfigurationAPIController(s ConfigurationAPIServicer, opts ...Configura
// Routes returns all the api routes for the ConfigurationAPIController
func (c *ConfigurationAPIController) Routes() Routes {
return Routes{
"GetAdvancedConfiguration": Route{
strings.ToUpper("Get"),
"/v1/configuration/advanced",
c.GetAdvancedConfiguration,
},
"GetAttributeMapping": Route{
strings.ToUpper("Get"),
"/v1/configuration/attribute-mapping",
c.GetAttributeMapping,
},
"GetBasicConfiguration": Route{
"GetConfiguration": Route{
strings.ToUpper("Get"),
"/v1/configuration/basic",
c.GetBasicConfiguration,
"/v1/configuration",
c.GetConfiguration,
},
"GetPermissionMapping": Route{
strings.ToUpper("Get"),
"/v1/configuration/permission-mapping",
c.GetPermissionMapping,
},
"PutAdvancedConfiguration": Route{
strings.ToUpper("Put"),
"/v1/configuration/advanced",
c.PutAdvancedConfiguration,
},
"PutAttributeMapping": Route{
strings.ToUpper("Put"),
"/v1/configuration/attribute-mapping",
c.PutAttributeMapping,
},
"PutBasicConfiguration": Route{
"PutConfiguration": Route{
strings.ToUpper("Put"),
"/v1/configuration/basic",
c.PutBasicConfiguration,
"/v1/configuration",
c.PutConfiguration,
},
"PutPermissionMapping": Route{
strings.ToUpper("Put"),
Expand All @@ -93,18 +83,6 @@ func (c *ConfigurationAPIController) Routes() Routes {
}
}

// GetAdvancedConfiguration - Get Advanced Configuration
func (c *ConfigurationAPIController) GetAdvancedConfiguration(w http.ResponseWriter, r *http.Request) {
result, err := c.service.GetAdvancedConfiguration(r.Context())
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}

// GetAttributeMapping - Get Attribute Mapping
func (c *ConfigurationAPIController) GetAttributeMapping(w http.ResponseWriter, r *http.Request) {
result, err := c.service.GetAttributeMapping(r.Context())
Expand All @@ -117,9 +95,9 @@ func (c *ConfigurationAPIController) GetAttributeMapping(w http.ResponseWriter,
EncodeJSONResponse(result.Body, &result.Code, w)
}

// GetBasicConfiguration - Get Basic Configurations
func (c *ConfigurationAPIController) GetBasicConfiguration(w http.ResponseWriter, r *http.Request) {
result, err := c.service.GetBasicConfiguration(r.Context())
// GetConfiguration - Get Configurations
func (c *ConfigurationAPIController) GetConfiguration(w http.ResponseWriter, r *http.Request) {
result, err := c.service.GetConfiguration(r.Context())
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
Expand All @@ -141,33 +119,6 @@ func (c *ConfigurationAPIController) GetPermissionMapping(w http.ResponseWriter,
EncodeJSONResponse(result.Body, &result.Code, w)
}

// PutAdvancedConfiguration - Creates or Update Advanced Configuration
func (c *ConfigurationAPIController) PutAdvancedConfiguration(w http.ResponseWriter, r *http.Request) {
advancedConfigurationParam := AdvancedConfiguration{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&advancedConfigurationParam); err != nil && !errors.Is(err, io.EOF) {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if err := AssertAdvancedConfigurationRequired(advancedConfigurationParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
if err := AssertAdvancedConfigurationConstraints(advancedConfigurationParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
result, err := c.service.PutAdvancedConfiguration(r.Context(), advancedConfigurationParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
return
}
// If no error, encode the body and the result code
EncodeJSONResponse(result.Body, &result.Code, w)
}

// PutAttributeMapping - Creates or Update Attribute Mapping
func (c *ConfigurationAPIController) PutAttributeMapping(w http.ResponseWriter, r *http.Request) {
attributeMapParam := AttributeMap{}
Expand Down Expand Up @@ -195,24 +146,24 @@ func (c *ConfigurationAPIController) PutAttributeMapping(w http.ResponseWriter,
EncodeJSONResponse(result.Body, &result.Code, w)
}

// PutBasicConfiguration - Creates or Update Basic Configuration
func (c *ConfigurationAPIController) PutBasicConfiguration(w http.ResponseWriter, r *http.Request) {
basicConfigurationParam := BasicConfiguration{}
// PutConfiguration - Creates or Update Configuration
func (c *ConfigurationAPIController) PutConfiguration(w http.ResponseWriter, r *http.Request) {
configurationParam := Configuration{}
d := json.NewDecoder(r.Body)
d.DisallowUnknownFields()
if err := d.Decode(&basicConfigurationParam); err != nil && !errors.Is(err, io.EOF) {
if err := d.Decode(&configurationParam); err != nil && !errors.Is(err, io.EOF) {
c.errorHandler(w, r, &ParsingError{Err: err}, nil)
return
}
if err := AssertBasicConfigurationRequired(basicConfigurationParam); err != nil {
if err := AssertConfigurationRequired(configurationParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
if err := AssertBasicConfigurationConstraints(basicConfigurationParam); err != nil {
if err := AssertConfigurationConstraints(configurationParam); err != nil {
c.errorHandler(w, r, err, nil)
return
}
result, err := c.service.PutBasicConfiguration(r.Context(), basicConfigurationParam)
result, err := c.service.PutConfiguration(r.Context(), configurationParam)
// If an error occurred, encode the error with the status code
if err != nil {
c.errorHandler(w, r, err, &result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

package apiserver

// BasicConfiguration - The Basic Configurations for running a SAML 2.0 Service Provider
type BasicConfiguration struct {
// Configuration - The Configurations for running a SAML 2.0 Service Provider
type Configuration struct {

// Configuration Id. Can only be 1
Id int32 `json:"id,omitempty"`
Expand All @@ -35,14 +35,32 @@ type BasicConfiguration struct {

// If enabled, the new created user is archived and cannot login until a admin has activated it.
UserToArchive bool `json:"userToArchive,omitempty"`

// If the configuration is enabled or not
AllowInitializationByIdp bool `json:"allowInitializationByIdp,omitempty"`

// If the SP should make a signed SAML Authn-Request or not
SignedRequest bool `json:"signedRequest,omitempty"`

// Normaly this value is set to false for a SP. If set to true the user has to re-authenticate (login at IdP) even it has a valid session to the IdP.
ForceAuthn bool `json:"forceAuthn,omitempty"`

// If you have to use a customized Entity Id, you can overwrite it here. Normaly the default value can be leave as it is.
EntityId string `json:"entityId,omitempty"`

// only send cookies over encrypted connection (HTTPS)
CookieSecure bool `json:"cookieSecure,omitempty"`

// The url to redirect if the login failed. If this value is null the default page /noLogin will showed up
LoginFailedUrl string `json:"loginFailedUrl,omitempty"`
}

// AssertBasicConfigurationRequired checks if the required fields are not zero-ed
func AssertBasicConfigurationRequired(obj BasicConfiguration) error {
// AssertConfigurationRequired checks if the required fields are not zero-ed
func AssertConfigurationRequired(obj Configuration) error {
return nil
}

// AssertBasicConfigurationConstraints checks if the values respects the defined constraints
func AssertBasicConfigurationConstraints(obj BasicConfiguration) error {
// AssertConfigurationConstraints checks if the values respects the defined constraints
func AssertConfigurationConstraints(obj Configuration) error {
return nil
}
46 changes: 12 additions & 34 deletions apiservices/api_configuration_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,6 @@ func NewConfigurationApiService() apiserver.ConfigurationAPIServicer {
return &ConfigurationApiService{}
}

// GetAdvancedConfiguration - Get Advanced Configuration
func (s *ConfigurationApiService) GetAdvancedConfiguration(ctx context.Context) (apiserver.ImplResponse, error) {
// TODO - update GetAdvancedConfiguration with the required logic for this service method.
// Add api_configuration_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, AdvancedConfiguration{}) or use other options such as http.Ok ...
//return Response(200, AdvancedConfiguration{}), nil

return apiserver.Response(http.StatusNotImplemented, nil), errors.New("GetAdvancedConfiguration method not implemented")
}

// GetAttributeMapping - Get Attribute Mapping
func (s *ConfigurationApiService) GetAttributeMapping(ctx context.Context) (apiserver.ImplResponse, error) {
// TODO - update GetAttributeMapping with the required logic for this service method.
Expand All @@ -55,15 +44,15 @@ func (s *ConfigurationApiService) GetAttributeMapping(ctx context.Context) (apis
return apiserver.Response(http.StatusNotImplemented, nil), errors.New("GetAttributeMapping method not implemented")
}

// GetBasicConfiguration - Get Basic Configurations
func (s *ConfigurationApiService) GetBasicConfiguration(ctx context.Context) (apiserver.ImplResponse, error) {
// TODO - update GetBasicConfiguration with the required logic for this service method.
// GetConfiguration - Get Basic Configurations
func (s *ConfigurationApiService) GetConfiguration(ctx context.Context) (apiserver.ImplResponse, error) {
// TODO - update GetConfiguration with the required logic for this service method.
// Add api_configuration_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, BasicConfiguration{}) or use other options such as http.Ok ...
//return Response(200, BasicConfiguration{}), nil
//TODO: Uncomment the next line to return response Response(200, Configuration{}) or use other options such as http.Ok ...
//return Response(200, Configuration{}), nil

return apiserver.Response(http.StatusNotImplemented, nil), errors.New("GetBasicConfiguration method not implemented")
return apiserver.Response(http.StatusNotImplemented, nil), errors.New("GetConfiguration method not implemented")
}

// GetPermissionMapping - Get Permission Mapping
Expand All @@ -77,17 +66,6 @@ func (s *ConfigurationApiService) GetPermissionMapping(ctx context.Context) (api
return apiserver.Response(http.StatusNotImplemented, nil), errors.New("GetPermissionMapping method not implemented")
}

// PutAdvancedConfiguration - Creates or Update Advanced Configuration
func (s *ConfigurationApiService) PutAdvancedConfiguration(ctx context.Context, advancedConfiguration apiserver.AdvancedConfiguration) (apiserver.ImplResponse, error) {
// TODO - update PutAdvancedConfiguration with the required logic for this service method.
// Add api_configuration_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, AdvancedConfiguration{}) or use other options such as http.Ok ...
//return Response(200, AdvancedConfiguration{}), nil

return apiserver.Response(http.StatusNotImplemented, nil), errors.New("PutAdvancedConfiguration method not implemented")
}

// PutAttributeMapping - Creates or Update Attribute Mapping
func (s *ConfigurationApiService) PutAttributeMapping(ctx context.Context, attributeMap apiserver.AttributeMap) (apiserver.ImplResponse, error) {
// TODO - update PutAttributeMapping with the required logic for this service method.
Expand All @@ -99,15 +77,15 @@ func (s *ConfigurationApiService) PutAttributeMapping(ctx context.Context, attri
return apiserver.Response(http.StatusNotImplemented, nil), errors.New("PutAttributeMapping method not implemented")
}

// PutBasicConfiguration - Creates or Update Basic Configuration
func (s *ConfigurationApiService) PutBasicConfiguration(ctx context.Context, basicConfiguration apiserver.BasicConfiguration) (apiserver.ImplResponse, error) {
// TODO - update PutBasicConfiguration with the required logic for this service method.
// PutConfiguration - Creates or Update Basic Configuration
func (s *ConfigurationApiService) PutConfiguration(ctx context.Context, basicConfiguration apiserver.Configuration) (apiserver.ImplResponse, error) {
// TODO - update PutConfiguration with the required logic for this service method.
// Add api_configuration_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation.

//TODO: Uncomment the next line to return response Response(200, BasicConfiguration{}) or use other options such as http.Ok ...
//return Response(200, BasicConfiguration{}), nil
//TODO: Uncomment the next line to return response Response(200, Configuration{}) or use other options such as http.Ok ...
//return Response(200, Configuration{}), nil

return apiserver.Response(http.StatusNotImplemented, nil), errors.New("PutBasicConfiguration method not implemented")
return apiserver.Response(http.StatusNotImplemented, nil), errors.New("PutConfiguration method not implemented")
}

// PutPermissionMapping - Creates or Update Permission Mapping Configurations
Expand Down
37 changes: 16 additions & 21 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,14 @@ func run() {
log.Debug(LOG_REGIO, "insert default config: %v", err)
}

basicConfig, err := conf.GetBasicConfig(context.Background())
config, err := conf.GetConfig(context.Background())
if err != nil {
log.Fatal(LOG_REGIO, "cannot load basic config")
}

advancedConfig, err := conf.GetAdvancedConfig(context.Background())
if err != nil {
log.Fatal(LOG_REGIO, "cannot load advanced config")
}

if basicConfig.IdpMetadataUrl != nil && *basicConfig.IdpMetadataUrl != "" {
if config.IdpMetadataUrl != nil && *config.IdpMetadataUrl != "" {
// fetch metadata
metadataResp, err := http.Get(*basicConfig.IdpMetadataUrl)
metadataResp, err := http.Get(*config.IdpMetadataUrl)
if err != nil {
log.Error(LOG_REGIO, "cannot fetch IdP metadata from url: %v", err)
} else {
Expand All @@ -74,26 +69,26 @@ func run() {
}
metadata = metaB
}
} else if basicConfig.IdpMetadataXml != nil {
metadata = []byte(*basicConfig.IdpMetadataXml)
} else if config.IdpMetadataXml != nil {
metadata = []byte(*config.IdpMetadataXml)
} else {
log.Error(LOG_REGIO, "not able to set IdP Metadata")
}

apiPort := common.Getenv("API_SERVER_PORT", strconv.Itoa(API_SERVER_PORT))
samlSpPort := common.Getenv("SSO_SERVER_PORT", strconv.Itoa(SSO_SERVER_PORT))

fmt.Println(basicConfig.OwnUrl + ":" + apiPort)
fmt.Println(config.OwnUrl + ":" + apiPort)
sp, err := saml.NewServiceProviderAdvanced(
basicConfig.ServiceProviderCertificate,
basicConfig.ServiceProviderPrivateKey,
basicConfig.OwnUrl,
config.ServiceProviderCertificate,
config.ServiceProviderPrivateKey,
config.OwnUrl,
metadata,
&advancedConfig.EntityId,
&advancedConfig.AllowInitializationByIdp,
&advancedConfig.SignedRequest,
&advancedConfig.ForceAuthn,
&advancedConfig.CookieSecure,
&config.EntityId,
&config.AllowInitializationByIdp,
&config.SignedRequest,
&config.ForceAuthn,
&config.CookieSecure,
)
if err != nil {
log.Fatal(LOG_REGIO, "cannot initialize saml service provider: %v", err)
Expand All @@ -119,8 +114,8 @@ func run() {
}()

// saml specific handle (no RESTful) to router
elionaAuth := eliona.NewSingleSignOn(basicConfig.OwnUrl,
basicConfig.UserToArchive, advancedConfig.LoginFailedUrl)
elionaAuth := eliona.NewSingleSignOn(config.OwnUrl,
config.UserToArchive, config.LoginFailedUrl)

activeHandleFunc := http.HandlerFunc(elionaAuth.ActiveHandle)
http.Handle(eliona.ENDPOINT_SSO_GENERIC_ACTIVE, activeHandleFunc)
Expand Down
Loading

0 comments on commit 130a2ee

Please sign in to comment.