Update sharedmodels to implement new okapi spec

master
Bas Kloosterman 2 years ago
parent e49e768454
commit 478fc9fa9e
  1. 47
      sharedmodel/auth.go
  2. 12
      sharedmodel/connection.go
  3. 31
      sharedmodel/model.go
  4. 50
      sharedmodel/registration.go
  5. 41
      sharedmodel/service.go
  6. 30
      sharedmodel/subscription.go

@ -1,14 +1,18 @@
package sharedmodel package sharedmodel
import ( import (
"google.golang.org/protobuf/types/known/structpb"
"gorm.io/gorm" "gorm.io/gorm"
"whiteboxsystems.nl/openkvpoc/openkv" "src.whiteboxsystems.nl/DECOZO/okapi"
) )
const AuthMethodDecozoMTLS = "http://decozo.org/proto/auth/mtls"
const AuthMethodDecozoBearerToken = "http://decozo.org/proto/auth/bearer-token"
type AuthConfig struct { type AuthConfig struct {
gorm.Model gorm.Model
Raw string Raw string
Method openkv.AuthMethod Method string
} }
func (cfg AuthConfig) Clone() *AuthConfig { func (cfg AuthConfig) Clone() *AuthConfig {
@ -18,19 +22,44 @@ func (cfg AuthConfig) Clone() *AuthConfig {
} }
} }
func NewAuthConfig(cfg *openkv.AuthConfig) *AuthConfig { func (cfg AuthConfig) ToOkapi() *okapi.ProtocolAuthConfiguration {
conf := &structpb.Struct{}
switch cfg.Method {
case "BearerToken":
conf, _ = structpb.NewStruct(map[string]interface{}{
"token": cfg.Raw,
})
}
return &okapi.ProtocolAuthConfiguration{
Method: cfg.Method,
Configuration: conf,
}
}
func NewAuthConfig(cfg *okapi.ProtocolAuthConfiguration) *AuthConfig {
authConfig := &AuthConfig{ authConfig := &AuthConfig{
Method: cfg.Method, Method: cfg.Method,
} }
switch cfg.Method { switch cfg.Method {
case openkv.AuthMethod_JWT: case "BearerToken":
authConfig.Raw = cfg.GetJwtConfig().GetPublicKey() authConfig.Raw, _ = cfg.GetConfiguration().AsMap()["token"].(string)
case openkv.AuthMethod_APIToken:
authConfig.Raw = cfg.GetApiTokenConfig().GetToken()
case openkv.AuthMethod_mTLS:
authConfig.Raw = cfg.GetMtlsConfig().GetPublicKey()
} }
return authConfig return authConfig
} }
type XISAuthConfig struct {
gorm.Model
Raw string
Method int32
}
func (cfg XISAuthConfig) Clone() *XISAuthConfig {
return &XISAuthConfig{
Raw: cfg.Raw,
Method: cfg.Method,
}
}

@ -6,10 +6,10 @@ import (
type Connection struct { type Connection struct {
gorm.Model gorm.Model
OrganisationId string OrganisationIdentifier string
OrganisationIdSystem string OrganisationIdentifierType string
OrganisationDisplayName string OrganisationDisplayName string
AuthConfigID uint AuthConfigID uint
AuthConfig *AuthConfig AuthConfig *XISAuthConfig `gorm:"constraint:OnDelete:CASCADE;"`
Services []ServiceConfig Services []ServiceConfig `gorm:"constraint:OnDelete:CASCADE;"`
} }

@ -6,12 +6,35 @@ import (
"errors" "errors"
"fmt" "fmt"
"whiteboxsystems.nl/openkvpoc/openkv" "src.whiteboxsystems.nl/DECOZO/okapi"
) )
type Protocol struct { type Protocol struct {
Protocol string Protocol string
AuthMethods AuthMethodArray AuthMethods []string
}
type ListOfStrings []string
// Scan scan value into Jsonb, implements sql.Scanner interface
func (j *ListOfStrings) Scan(value interface{}) error {
bytes, ok := value.([]byte)
if !ok {
return errors.New(fmt.Sprint("Failed to unmarshal ListOfStrings value:", value))
}
result := []string{}
err := json.Unmarshal(bytes, &result)
*j = ListOfStrings(result)
return err
}
// Value return json value, implement driver.Valuer interface
func (j ListOfStrings) Value() (driver.Value, error) {
if len(j) == 0 {
return nil, nil
}
return json.Marshal(j)
} }
type ProtocolArray []Protocol type ProtocolArray []Protocol
@ -37,7 +60,7 @@ func (j ProtocolArray) Value() (driver.Value, error) {
return json.Marshal(j) return json.Marshal(j)
} }
type AuthMethodArray []openkv.AuthMethod type AuthMethodArray []okapi.ProtocolAuthConfiguration
// Scan scan value into Jsonb, implements sql.Scanner interface // Scan scan value into Jsonb, implements sql.Scanner interface
func (j *AuthMethodArray) Scan(value interface{}) error { func (j *AuthMethodArray) Scan(value interface{}) error {
@ -46,7 +69,7 @@ func (j *AuthMethodArray) Scan(value interface{}) error {
return errors.New(fmt.Sprint("Failed to unmarshal AuthMethodArray value:", value)) return errors.New(fmt.Sprint("Failed to unmarshal AuthMethodArray value:", value))
} }
result := []openkv.AuthMethod{} result := []okapi.ProtocolAuthConfiguration{}
err := json.Unmarshal(bytes, &result) err := json.Unmarshal(bytes, &result)
*j = AuthMethodArray(result) *j = AuthMethodArray(result)
return err return err

@ -1,8 +1,12 @@
package sharedmodel package sharedmodel
import ( import (
"crypto"
"fmt"
"gorm.io/gorm" "gorm.io/gorm"
"whiteboxsystems.nl/openkvpoc/openkv" "src.whiteboxsystems.nl/DECOZO/okapi"
"whiteboxsystems.nl/okapidemo/certgen"
) )
type RegistrationStatus string type RegistrationStatus string
@ -14,29 +18,39 @@ const (
type Registration struct { type Registration struct {
gorm.Model gorm.Model
OrganisationId string OrganisationIdentifier string
OrganisationIdSystem string OrganisationIdentifierType string
OrganisationDisplayName string OrganisationDisplayName string
AuthConfigID uint AuthConfigID uint
AuthConfig *AuthConfig AuthConfig *XISAuthConfig
Reference string Reference string
PSK string PSK string
Status RegistrationStatus Status RegistrationStatus
} }
func (r *Registration) SetAuthConfig(cfg *openkv.AuthConfig) { func (r *Registration) SetAuthConfig(cfg *okapi.XISAuthConfiguration) error {
authConfig := &AuthConfig{ authConfig := &XISAuthConfig{
Method: cfg.Method, Method: int32(cfg.Method),
} }
switch cfg.Method { switch cfg.Method {
case openkv.AuthMethod_JWT: case okapi.XISAuthMethod_mTLS:
authConfig.Raw = cfg.GetJwtConfig().GetPublicKey() k, err := certgen.StringToJWK(cfg.GetMtlsConfiguration().GetPublicKey())
case openkv.AuthMethod_APIToken:
authConfig.Raw = cfg.GetApiTokenConfig().GetToken() if err != nil {
case openkv.AuthMethod_mTLS: return err
authConfig.Raw = cfg.GetMtlsConfig().GetPublicKey() }
tp, err := k.Thumbprint(crypto.SHA256)
if err != nil {
return err
}
authConfig.Raw = fmt.Sprintf("%X", tp)
} }
r.AuthConfig = authConfig r.AuthConfig = authConfig
return nil
} }

@ -3,25 +3,26 @@ package sharedmodel
import ( import (
"encoding/json" "encoding/json"
"google.golang.org/protobuf/types/known/structpb"
"gorm.io/gorm" "gorm.io/gorm"
"whiteboxsystems.nl/openkvpoc/openkv" "src.whiteboxsystems.nl/DECOZO/okapi"
) )
type Service struct { type ServiceDefinition struct {
gorm.Model gorm.Model
ServiceID string ServiceID string
Name string Name string
Description string Description string
SubscriptionPolicy openkv.SubscriptionPolicy SubscriptionPolicy okapi.SubscriptionPolicy
ConsentPolicy openkv.ConsentPolicy ConsentPolicy okapi.ConsentPolicy
FetchProtocols ProtocolArray `gorm:"type:text"` FetchProtocols ProtocolArray `gorm:"type:text"`
PushProtocols ProtocolArray `gorm:"type:text"` PushProtocols ProtocolArray `gorm:"type:text"`
} }
func (s Service) GetFetchProtocols() []*openkv.ProtocolDefinition { func (s ServiceDefinition) GetFetchProtocols() []*okapi.ProtocolDefinition {
protoDefs := []*openkv.ProtocolDefinition{} protoDefs := []*okapi.ProtocolDefinition{}
for _, sd := range s.FetchProtocols { for _, sd := range s.FetchProtocols {
protoDefs = append(protoDefs, &openkv.ProtocolDefinition{ protoDefs = append(protoDefs, &okapi.ProtocolDefinition{
Protocol: sd.Protocol, Protocol: sd.Protocol,
AuthMethods: sd.AuthMethods, AuthMethods: sd.AuthMethods,
}) })
@ -30,10 +31,10 @@ func (s Service) GetFetchProtocols() []*openkv.ProtocolDefinition {
return protoDefs return protoDefs
} }
func (s Service) GetPushProtocols() []*openkv.ProtocolDefinition { func (s ServiceDefinition) GetPushProtocols() []*okapi.ProtocolDefinition {
protoDefs := []*openkv.ProtocolDefinition{} protoDefs := []*okapi.ProtocolDefinition{}
for _, sd := range s.PushProtocols { for _, sd := range s.PushProtocols {
protoDefs = append(protoDefs, &openkv.ProtocolDefinition{ protoDefs = append(protoDefs, &okapi.ProtocolDefinition{
Protocol: sd.Protocol, Protocol: sd.Protocol,
AuthMethods: sd.AuthMethods, AuthMethods: sd.AuthMethods,
}) })
@ -46,7 +47,7 @@ type ProtocolConfig struct {
gorm.Model gorm.Model
Protocol string Protocol string
AuthConfigID uint AuthConfigID uint
AuthConfig *AuthConfig AuthConfig *AuthConfig `gorm:"constraint:OnDelete:CASCADE;"`
Config string Config string
} }
@ -54,6 +55,15 @@ func (pc ProtocolConfig) UnmarshalConfig(in interface{}) error {
return json.Unmarshal([]byte(pc.Config), in) return json.Unmarshal([]byte(pc.Config), in)
} }
func (pc ProtocolConfig) ConfigToOkapi() *structpb.Struct {
config := map[string]interface{}{}
pc.UnmarshalConfig(&config)
cnf, _ := structpb.NewStruct(config)
return cnf
}
func (pc *ProtocolConfig) SetConfig(in interface{}) error { func (pc *ProtocolConfig) SetConfig(in interface{}) error {
b, err := json.Marshal(in) b, err := json.Marshal(in)
if err != nil { if err != nil {
@ -67,13 +77,12 @@ func (pc *ProtocolConfig) SetConfig(in interface{}) error {
type ServiceConfig struct { type ServiceConfig struct {
gorm.Model gorm.Model
ServiceID uint ServiceID uint
Service Service Service ServiceDefinition
Enabled bool
ConnectionID uint ConnectionID uint
Connection Connection `json:"-"` Connection Connection `json:"-"`
PushProtocolID uint PushProtocolID uint
PushProtocol *ProtocolConfig `gorm:"foreignKey:PushProtocolID"` PushProtocol *ProtocolConfig `gorm:"foreignKey:PushProtocolID;constraint:OnDelete:CASCADE;"`
FetchProtocolID uint FetchProtocolID uint
FetchProtocol *ProtocolConfig `gorm:"foreignKey:FetchProtocolID"` FetchProtocol *ProtocolConfig `gorm:"foreignKey:FetchProtocolID;constraint:OnDelete:CASCADE;"`
Subscriptions []*Subscription Subscriptions []*Subscription `gorm:"constraint:OnDelete:CASCADE;"`
} }

@ -2,19 +2,33 @@ package sharedmodel
import ( import (
"encoding/json" "encoding/json"
"time"
"gorm.io/gorm" "gorm.io/gorm"
) )
type Subscription struct { type Subscription struct {
gorm.Model ID string `gorm:"primarykey"`
SubjectExternalId string CreatedAt time.Time
SubjectExternalIdSystem string UpdatedAt time.Time
SubjectName string DeletedAt gorm.DeletedAt `gorm:"index"`
SubjectBirthdate string SubjectExternalId string
ProtocolMeta string SubjectExternalIdSystem string
ServiceConfigID uint SubjectDisplayName string
ServiceConfig *ServiceConfig SubjectGiven ListOfStrings `gorm:"type:text"`
SubjectOwnName string
SubjectOwnNamePrefix string
SubjectPartnerName string
SubjectPartnerNamePrefix string
SubjectBirthdate string
SubjectAddressStreet string
SubjectAddressStreetNumber string
SubjectAddressPostalCode string
SubjectAddressCity string
SubjectAddressCountry string
ProtocolMeta string
ServiceConfigID uint
ServiceConfig *ServiceConfig
} }
func (s Subscription) GetProtocolMeta(meta interface{}) error { func (s Subscription) GetProtocolMeta(meta interface{}) error {

Loading…
Cancel
Save