Skip to content

Commit 8a61869

Browse files
authored
move tools models into main project model (#14)
1 parent 4623f35 commit 8a61869

8 files changed

Lines changed: 108 additions & 62 deletions

File tree

app/models/models.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ import (
1010
type Service string
1111

1212
const (
13-
AIS Service = "AISP"
14-
PIS Service = "PISP"
13+
AISP Service = "AISP"
14+
PISP Service = "PISP"
1515
// CBPII Service = "CBPII"
1616
)
1717

1818
func serviceFromString(str string) (Service, error) {
1919
switch str {
2020
case "PS_080":
21-
return AIS, nil
21+
return AISP, nil
2222
case "PS_070":
23-
return PIS, nil
23+
return PISP, nil
2424
}
2525
return "", fmt.Errorf("unknown service: %s", str)
2626
}
@@ -154,3 +154,40 @@ func (t *TPP) UnmarshalJSON(data []byte) error {
154154
t.Registry = "EBA"
155155
return nil
156156
}
157+
158+
159+
type Register string
160+
161+
const (
162+
EBA Register = "EBA"
163+
)
164+
165+
type CertType string
166+
167+
const (
168+
QWAC CertType = "QWAC"
169+
QSealC CertType = "QSealC"
170+
)
171+
172+
type Scope string
173+
174+
const (
175+
AIS Scope = "AIS"
176+
PIS Scope = "PIS"
177+
)
178+
179+
180+
type ParsedCert struct {
181+
Pem string
182+
SerialNumber string
183+
Sha256 string
184+
// Links []string
185+
Registers []Register
186+
NotBefore time.Time
187+
NotAfter time.Time
188+
Type CertType // types?
189+
Scopes []Scope
190+
Order int
191+
RootSha256 string
192+
// CRLs []string ??
193+
}

app/verify/verify.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,7 @@ func formatCertContent(content []byte) ([]byte, error) {
356356
buffer.WriteString(certPrefix)
357357
buffer.WriteString("\n")
358358
for i := 0; i < len(contentString); i += pemLineLength {
359-
end := i + pemLineLength
360-
if end > len(contentString) {
361-
end = len(contentString)
362-
}
359+
end := min(i+pemLineLength, len(contentString))
363360
buffer.WriteString(contentString[i:end])
364361
buffer.WriteString("\n")
365362
}
@@ -543,9 +540,9 @@ func getCertServices(cert ParsedCert) []models.Service {
543540
for _, scope := range cert.Scopes {
544541
switch scope {
545542
case PSP_PI:
546-
services = append(services, models.PIS)
543+
services = append(services, models.PISP)
547544
case PSP_AI:
548-
services = append(services, models.AIS)
545+
services = append(services, models.AISP)
549546
default:
550547
log.Printf("Unknown scope in certificate: %s", scope)
551548
continue

app/verify/verify_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (m *MockDb) GetTpp(ctx context.Context, id string) (*models.TPP, error) {
9393
NameNative: "Teszt TPP",
9494
Authority: "Test Authority",
9595
Services: map[string][]models.Service{
96-
"FI": {models.AIS, models.PIS},
96+
"FI": {models.AISP, models.PISP},
9797
},
9898
AuthorizedAt: time.Now(),
9999
WithdrawnAt: time.Time{},

server/db/mongo.go

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,20 @@ type MongoClient struct {
1717
Database *mongo.Database
1818
}
1919

20-
func GetMongoDb() (*MongoClient, error) {
20+
func GetMongoDb(ctx context.Context) (*MongoClient, error) {
2121
mongoURI := os.Getenv("MONGO_URL")
2222
if mongoURI == "" {
2323
return nil, errors.New("MONGO_URL is not set")
2424
}
2525
clientOptions := options.Client().ApplyURI(mongoURI)
2626

27-
mongoClient, err := mongo.Connect(nil, clientOptions)
27+
mongoClient, err := mongo.Connect(ctx, clientOptions)
2828
if err != nil {
2929
log.Fatal(err)
3030
return nil, err
3131
}
3232

33-
err = mongoClient.Ping(nil, nil)
33+
err = mongoClient.Ping(ctx, nil)
3434
if err != nil {
3535
log.Fatal(err)
3636
return nil, err
@@ -45,7 +45,7 @@ func GetMongoDb() (*MongoClient, error) {
4545
}
4646

4747
func (db *MongoClient) Disconnect(ctx context.Context) error {
48-
return db.Disconnect(ctx)
48+
return db.Database.Client().Disconnect(ctx)
4949
}
5050

5151

@@ -62,6 +62,29 @@ func (r *TppMongoRepository) GetTpp(ctx context.Context, id string) (*models.TPP
6262
return tpp, nil
6363
}
6464

65+
func (r *TppMongoRepository) GetRootCertificates(ctx context.Context) ([]string, error) {
66+
// Get all certificates from the "certs" collection for now
67+
cursor, err := r.db.Collection("certs").Find(ctx, bson.M{})
68+
if err != nil {
69+
return nil, err
70+
}
71+
defer cursor.Close(ctx)
72+
73+
var roots []string
74+
for cursor.Next(ctx) {
75+
// TODO: move models from the tools package to the app/models package
76+
var tpp models.ParsedCert
77+
if err := cursor.Decode(&tpp); err != nil {
78+
return nil, err
79+
}
80+
roots = append(roots, tpp.Pem)
81+
}
82+
if err := cursor.Err(); err != nil {
83+
return nil, err
84+
}
85+
return roots, nil
86+
}
87+
6588
func NewTppMongoRepository(db *mongo.Database) *TppMongoRepository {
6689
return &TppMongoRepository{db: db}
6790
}

server/run.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"crypto/x509"
56
"log"
67
"net/http"
78

@@ -11,7 +12,8 @@ import (
1112
)
1213

1314
func main() {
14-
client, err := db.GetMongoDb()
15+
ctx := context.Background()
16+
client, err := db.GetMongoDb(ctx)
1517
if err != nil {
1618
panic(err)
1719
}
@@ -24,6 +26,18 @@ func main() {
2426
httpClient := &http.Client{} // Assuming you want to use a default HTTP client
2527
tppRepo := db.NewTppMongoRepository(client.Database)
2628
vs := verify.NewVerifySvc(tppRepo, httpClient)
29+
roots, err := tppRepo.GetRootCertificates(ctx)
30+
if err != nil {
31+
log.Fatalf("Failed to get root certificates: %v", err)
32+
}
33+
rootPool := x509.NewCertPool()
34+
for _, root := range roots {
35+
// TODO: check if roots are formatted correctly
36+
if !rootPool.AppendCertsFromPEM([]byte(root)) {
37+
log.Printf("Failed to append root certificate")
38+
}
39+
}
40+
vs.SetRoots(rootPool)
2741
r := app.SetupRouter(vs)
2842
r.Run()
2943
}

tools/eba_certs/main.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,18 @@ import (
1212

1313
"go.mongodb.org/mongo-driver/mongo"
1414
"go.mongodb.org/mongo-driver/mongo/options"
15+
16+
"github.com/botsman/tppVerifier/app/models"
1517
)
1618

1719
// Load XML files from EBA
1820
// Parse them
1921
// Insert results into the database
2022

21-
type CertType string
22-
23-
const (
24-
QWAC CertType = "QWAC"
25-
QSealC CertType = "QSealC"
26-
)
27-
28-
type Scope string
29-
30-
const (
31-
AIS Scope = "AIS"
32-
PIS Scope = "PIS"
33-
)
34-
35-
type Register string
36-
37-
const (
38-
EBA Register = "EBA"
39-
)
4023

4124
type RawCert struct {
4225
Pem string
43-
Type CertType
26+
Type models.CertType
4427
}
4528

4629
func getEEACountries() []string {
@@ -125,7 +108,7 @@ func parseXML(xmlData []byte) <-chan RawCert {
125108
continue
126109
}
127110
serviceType := tspService.ServiceInformation.getType()
128-
if serviceType != QSealC {
111+
if serviceType != models.QSealC {
129112
continue
130113
}
131114
cert := tspService.ServiceInformation.getPemCert()
@@ -163,8 +146,8 @@ func parseXMLs(xmlChan <-chan []byte) <-chan RawCert {
163146
return certsChan
164147
}
165148

166-
func parseCerts(certChan <-chan RawCert) <-chan ParsedCert {
167-
parsedCertChan := make(chan ParsedCert)
149+
func parseCerts(certChan <-chan RawCert) <-chan models.ParsedCert {
150+
parsedCertChan := make(chan models.ParsedCert)
168151
go func() {
169152
defer close(parsedCertChan)
170153
for cert := range certChan {

tools/eba_certs/parseCert.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,34 @@ import (
66
"encoding/hex"
77
"encoding/pem"
88
"fmt"
9-
"time"
9+
10+
"github.com/botsman/tppVerifier/app/models"
1011
)
1112

12-
type ParsedCert struct {
13-
Pem string
14-
SerialNumber string
15-
Sha256 string
16-
// Links []string
17-
Registers []Register
18-
NotBefore time.Time
19-
NotAfter time.Time
20-
Type CertType // types?
21-
Scopes []Scope
22-
Order int
23-
RootSha256 string
24-
// CRLs []string ??
25-
}
13+
2614

2715
const certPrefix = "-----BEGIN CERTIFICATE-----"
2816
const certSuffix = "-----END CERTIFICATE-----"
2917

3018

31-
func parseCert(cert RawCert) (ParsedCert, error) {
19+
func parseCert(cert RawCert) (models.ParsedCert, error) {
3220
var certPem = cert.Pem
3321
formattedCert := fmt.Sprintf("%s\n%s\n%s", certPrefix, certPem, certSuffix)
3422
var block, _ = pem.Decode([]byte(formattedCert))
3523
if block == nil {
36-
return ParsedCert{}, fmt.Errorf("failed to parse certificate")
24+
return models.ParsedCert{}, fmt.Errorf("failed to parse certificate")
3725
}
3826
x509Cert, err := x509.ParseCertificate([]byte(block.Bytes))
3927
if err != nil {
40-
return ParsedCert{}, err
28+
return models.ParsedCert{}, err
4129
}
4230

43-
return ParsedCert{
31+
return models.ParsedCert{
4432
Pem: cert.Pem,
4533
SerialNumber: x509Cert.SerialNumber.String(),
4634
Sha256: getSha256(x509Cert),
4735
// Links: x509Cert.IssuingCertificateURL,
48-
Registers: []Register{EBA},
36+
Registers: []models.Register{models.EBA},
4937
NotBefore: x509Cert.NotBefore,
5038
NotAfter: x509Cert.NotAfter,
5139
Type: cert.Type,

tools/eba_certs/xmlStructs.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package main
22

3+
import (
4+
"github.com/botsman/tppVerifier/app/models"
5+
)
6+
37
type TrustServiceStatusList struct {
48
TrustServiceProviders []TrustServiceProvider `xml:"TrustServiceProviderList>TrustServiceProvider"`
59
}
@@ -23,13 +27,13 @@ func (si ServiceInformation) isValidStatus() bool {
2327
return si.ServiceStatus == "http://uri.etsi.org/TrstSvc/TrustedList/Svcstatus/granted"
2428
}
2529

26-
func (si ServiceInformation) getType() CertType {
30+
func (si ServiceInformation) getType() models.CertType {
2731
for _, ext := range si.ServiceInformationExtensions.Extensions {
2832
if ext.AdditionalServiceInformation.URI == "http://uri.etsi.org/TrstSvc/TrustedList/SvcInfoExt/ForWebSiteAuthentication" {
29-
return QWAC
33+
return models.QWAC
3034
}
3135
if ext.AdditionalServiceInformation.URI == "http://uri.etsi.org/TrstSvc/TrustedList/SvcInfoExt/ForeSeals" {
32-
return QSealC
36+
return models.QSealC
3337
}
3438
}
3539
return ""

0 commit comments

Comments
 (0)