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

Commit

Permalink
add converters and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-stauffer committed Sep 20, 2023
1 parent 52c100b commit 7283df9
Show file tree
Hide file tree
Showing 21 changed files with 793 additions and 60 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: 2023-09-20T08:05:33.350893Z[Etc/UTC]
- Build date: 2023-09-20T14:31:37.352932Z[Etc/UTC]


### Running the server
Expand Down
2 changes: 2 additions & 0 deletions apiserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type GenericSingleSignOnApiRouter interface {
// pass the data to a SAML20ApiServicer to perform the required actions, then write the service results to the http response.
type SAML20ApiRouter interface {
SamlAcsPost(http.ResponseWriter, *http.Request)
SamlSloPost(http.ResponseWriter, *http.Request)
}

// VersionApiRouter defines the required methods for binding the api requests to a responses for the VersionApi
Expand Down Expand Up @@ -81,6 +82,7 @@ type GenericSingleSignOnApiServicer interface {
// and updated with the logic required for the API.
type SAML20ApiServicer interface {
SamlAcsPost(context.Context) (ImplResponse, error)
SamlSloPost(context.Context) (ImplResponse, error)
}

// VersionApiServicer defines the api actions for the VersionApi service
Expand Down
19 changes: 19 additions & 0 deletions apiserver/api_saml20.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ func (c *SAML20ApiController) Routes() Routes {
"/v1/saml/acs",
c.SamlAcsPost,
},
{
"SamlSloPost",
strings.ToUpper("Post"),
"/v1/saml/slo",
c.SamlSloPost,
},
}
}

Expand All @@ -68,3 +74,16 @@ func (c *SAML20ApiController) SamlAcsPost(w http.ResponseWriter, r *http.Request
EncodeJSONResponse(result.Body, &result.Code, w)

}

// SamlSloPost -
func (c *SAML20ApiController) SamlSloPost(w http.ResponseWriter, r *http.Request) {
result, err := c.service.SamlSloPost(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)

}
6 changes: 3 additions & 3 deletions apiserver/model_advanced_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ type AdvancedConfiguration struct {
// 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"`

// ff
CookieSecure *interface{} `json:"cookieSecure,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 *interface{} `json:"loginFailedUrl,omitempty"`
LoginFailedUrl string `json:"loginFailedUrl,omitempty"`
}

// AssertAdvancedConfigurationRequired checks if the required fields are not zero-ed
Expand Down
11 changes: 7 additions & 4 deletions apiserver/model_attribute_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ type AttributeMap struct {
Enable bool `json:"enable,omitempty"`

// SAML attribute to map to the email (login) of a user
Email *interface{} `json:"email,omitempty"`
Email string `json:"email,omitempty"`

FirstName *interface{} `json:"first_name,omitempty"`
// SAML attribute to map to the first name of a user
FirstName *string `json:"first_name,omitempty"`

LastName *interface{} `json:"last_name,omitempty"`
// SAML attribute to map to the last name of a user
LastName *string `json:"last_name,omitempty"`

Phone *interface{} `json:"phone,omitempty"`
// SAML attribute to map to the phone number of a user
Phone *string `json:"phone,omitempty"`
}

// AssertAttributeMapRequired checks if the required fields are not zero-ed
Expand Down
11 changes: 11 additions & 0 deletions apiservices/api_saml20_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ func (s *SAML20ApiService) SamlAcsPost(ctx context.Context) (apiserver.ImplRespo

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

// SamlSloPost -
func (s *SAML20ApiService) SamlSloPost(ctx context.Context) (apiserver.ImplResponse, error) {
// TODO - update SamlSloPost with the required logic for this service method.
// Add api_saml20_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(302, {}) or use other options such as http.Ok ...
//return Response(302, nil),nil

return apiserver.Response(http.StatusNotImplemented, nil), errors.New("SamlSloPost method not implemented")
}
10 changes: 5 additions & 5 deletions appdb/basic_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 149 additions & 7 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,98 @@ import (

"saml-sso/apiserver"
"saml-sso/appdb"
"saml-sso/utils"

"github.com/eliona-smart-building-assistant/go-utils/db"
"github.com/eliona-smart-building-assistant/go-utils/log"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
)

// Todo: Define anything for configuration like structures and methods to read and process configuration
func InsertDefaultSamlConfiguration(connection db.Connection) error {
const (
LOG_REGIO = "config"
)

const (
AUTO_CNF_DEFAULT_ENABLED = true
AUTO_CNF_DEFAULT_ALLOW_INIT_BY_IDP = false
AUTO_CNF_DEFAULT_SIGNING_REQ = true
AUTO_CNF_DEFAULT_FORCE_AUTHN = false
AUTO_CNF_DEFAULT_COOKIE_SECURE = false
AUTO_CNF_DEFAULT_ENTITY_ID = "{ownUrl}/saml/metadata"
AUTO_CNF_DEFAULT_LOGIN_FAIL_URL = "{ownUrl}/noLogin"
AUTO_CNF_DEFAULT_USERNAME_ATTR = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn"
AUTO_CNF_DEFAULT_SYSTEM_PERMISSION = "regular"
AUTO_CNF_DEFAULT_PROJ_PERMISSION = "operator"
AUTO_CNF_DEFAULT_CERT_VALID_DAYS = 3650
AUTO_CNF_DEFAULT_KEY_LENGTH = 4096
)

func InsertAutoSamlConfiguration(ctx context.Context) error {

var (
basicConfig appdb.BasicConfig = appdb.BasicConfig{
Enable: AUTO_CNF_DEFAULT_ENABLED,
IdpMetadataURL: null.StringFromPtr(nil),
MetadataXML: null.StringFromPtr(nil),
}
advancedConfig appdb.AdvancedConfig = appdb.AdvancedConfig{
Enable: AUTO_CNF_DEFAULT_ENABLED,
AllowInitializationByIdp: AUTO_CNF_DEFAULT_ALLOW_INIT_BY_IDP,
SignedRequest: AUTO_CNF_DEFAULT_SIGNING_REQ,
ForceAuthn: AUTO_CNF_DEFAULT_FORCE_AUTHN,
EntityID: AUTO_CNF_DEFAULT_ENTITY_ID,
CookieSecure: AUTO_CNF_DEFAULT_COOKIE_SECURE,
LoginFailedURL: AUTO_CNF_DEFAULT_LOGIN_FAIL_URL,
}
attributeMapping appdb.AttributeMap = appdb.AttributeMap{
Enable: AUTO_CNF_DEFAULT_ENABLED,
Email: AUTO_CNF_DEFAULT_USERNAME_ATTR,
FirstName: null.StringFromPtr(nil),
LastName: null.StringFromPtr(nil),
Phone: null.StringFromPtr(nil),
}
permissionCnf appdb.Permission = appdb.Permission{
Enable: AUTO_CNF_DEFAULT_ENABLED,
DefaultSystemRole: AUTO_CNF_DEFAULT_SYSTEM_PERMISSION,
DefaultProjRole: AUTO_CNF_DEFAULT_PROJ_PERMISSION,
SystemRoleSamlAttribute: null.StringFromPtr(nil),
SystemRoleMap: null.JSONFromPtr(nil),
ProjRoleSamlAttribute: null.StringFromPtr(nil),
ProjRoleMap: null.JSONFromPtr(nil),
}
)

basicConfig.OwnURL = "https://" + GetElionaHost()
certificate, privateKey, err := utils.CreateSelfsignedX509Certificate(AUTO_CNF_DEFAULT_CERT_VALID_DAYS, AUTO_CNF_DEFAULT_KEY_LENGTH, nil, nil)
if err != nil {
log.Error(LOG_REGIO, "auto configuration generate x509 certificates: %v", err)
} else {
basicConfig.SPCertificate = certificate
basicConfig.SPPrivateKey = privateKey
}

err = basicConfig.Insert(ctx, getDb(), boil.Infer())
if err != nil {
return err
}

return errors.New("not implemented")
err = advancedConfig.Insert(ctx, getDb(), boil.Infer())
if err != nil {
return err
}

err = attributeMapping.Insert(ctx, getDb(), boil.Infer())
if err != nil {
return err
}

err = permissionCnf.Insert(ctx, getDb(), boil.Infer())
if err != nil {
return err
}

return err
}

func GetBasicConfig(ctx context.Context) (*apiserver.BasicConfiguration, error) {
Expand Down Expand Up @@ -59,7 +142,7 @@ func GetBasicConfig(ctx context.Context) (*apiserver.BasicConfiguration, error)
return basicConfigApi, err
}

func SetBasicConfig(ctx context.Context, basicConfig *apiserver.BasicConfiguration) (*apiserver.BasicConfiguration, error) {
func SetBasicConfig(ctx context.Context, config *apiserver.BasicConfiguration) (*apiserver.BasicConfiguration, error) {

var (
err error = nil
Expand All @@ -69,18 +152,77 @@ func SetBasicConfig(ctx context.Context, basicConfig *apiserver.BasicConfigurati
dbForm any = nil
)

if basicConfig == nil {
if config == nil {
return nil, errors.New("basic config is nil")
}

dbForm, err = ConvertDbToApiForm(basicConfig)
dbForm, err = ConvertDbToApiForm(config)
if err != nil {
return nil, err
} else {
basicConfigDb = dbForm.(*appdb.BasicConfig)
}

basicConfigDb.Insert(ctx, getDb(), boil.Infer())
exists, err := basicConfigDb.Exists(ctx, getDb())
if err != nil {
return nil, err
}

if exists {
_, err = basicConfigDb.Update(ctx, getDb(), boil.Infer())
} else {
err = basicConfigDb.Insert(ctx, getDb(), boil.Infer())
}

apiForm, err = ConvertDbToApiForm(basicConfigDb)
if err != nil {
return nil, err
}

return apiForm.(*apiserver.BasicConfiguration), err
}

func GetAdvancedConfig(ctx context.Context) (*apiserver.AdvancedConfiguration, error) {
return nil, errors.New("not implemented")
}

func SetAdvancedConfig(ctx context.Context, config *apiserver.AdvancedConfiguration) (*apiserver.AdvancedConfiguration, error) {
if config != nil {
return nil, errors.New("advanced config to set is nil")
}
return nil, errors.New("not implemented")
}

func GetAttributeMapping(ctx context.Context) (*apiserver.AttributeMap, error) {
return nil, errors.New("not implemented")
}

func SetAttributeMapping(ctx context.Context, mapping *apiserver.AttributeMap) (*apiserver.AttributeMap, error) {
if mapping != nil {
return nil, errors.New("attribute map settings to set is nil")
}
return nil, errors.New("not implemented")
}

func GetPermissionSettings(ctx context.Context) (*apiserver.AttributeMap, error) {
return nil, errors.New("not implemented")
}

func SetPermissionSettings(ctx context.Context, permissions *apiserver.Permissions) (*apiserver.Permissions, error) {
if permissions != nil {
return nil, errors.New("permission settings to set is nil")
}
return nil, errors.New("not implemented")
}

func GetElionaHost() string {
var eliDomain string

db := getDb()
row := db.QueryRow("SELECT domain_name FROM eliona_config ;")
row.Scan()

return eliDomain
}

func getDb() *sql.DB {
Expand Down
Loading

0 comments on commit 7283df9

Please sign in to comment.