From 358bd11f5476fec5e0e4dfdc864f12c2d1738207 Mon Sep 17 00:00:00 2001 From: Steve Munene Date: Fri, 20 Sep 2024 13:10:47 +0300 Subject: [PATCH] NOISSUE - Fix cert response (#19) * Remove cert and key field in cert response Signed-off-by: nyagamunene * Pointer to cert and key response Signed-off-by: nyagamunene * Fix linter Signed-off-by: nyagamunene * Fix list with entity id Signed-off-by: nyagamunene --------- Signed-off-by: nyagamunene --- api/http/endpoint.go | 19 ++++++++++++++----- api/http/responses.go | 13 ++++++------- certs.go | 16 ++++++++-------- sdk/sdk.go | 7 +++++-- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/api/http/endpoint.go b/api/http/endpoint.go index d7a92da..aafdcb6 100644 --- a/api/http/endpoint.go +++ b/api/http/endpoint.go @@ -112,11 +112,21 @@ func listCertsEndpoint(svc certs.Service) endpoint.Endpoint { return listCertsRes{}, err } + var crts []viewCertRes + for _, c := range certPage.Certificates { + crts = append(crts, viewCertRes{ + SerialNumber: c.SerialNumber, + Revoked: c.Revoked, + EntityID: c.EntityID, + ExpiryTime: c.ExpiryTime, + }) + } + return listCertsRes{ Total: certPage.Total, Offset: certPage.Offset, Limit: certPage.Limit, - Certificates: certPage.Certificates, + Certificates: crts, }, nil } } @@ -131,12 +141,11 @@ func viewCertEndpoint(svc certs.Service) endpoint.Endpoint { if err != nil { return viewCertRes{}, err } - crt := string(cert.Certificate) - key := string(cert.Key) + return viewCertRes{ SerialNumber: cert.SerialNumber, - Certificate: &crt, - Key: &key, + Certificate: string(cert.Certificate), + Key: string(cert.Key), Revoked: cert.Revoked, ExpiryTime: cert.ExpiryTime, EntityID: cert.EntityID, diff --git a/api/http/responses.go b/api/http/responses.go index 60c5f20..79bd352 100644 --- a/api/http/responses.go +++ b/api/http/responses.go @@ -9,7 +9,6 @@ import ( "net/http" "time" - "github.com/absmach/certs" "golang.org/x/crypto/ocsp" ) @@ -122,10 +121,10 @@ func (res issueCertRes) Empty() bool { } type listCertsRes struct { - Total uint64 `json:"total"` - Offset uint64 `json:"offset"` - Limit uint64 `json:"limit"` - Certificates []certs.Certificate `json:"certificates"` + Total uint64 `json:"total"` + Offset uint64 `json:"offset"` + Limit uint64 `json:"limit"` + Certificates []viewCertRes `json:"certificates"` } func (res listCertsRes) Code() int { @@ -138,8 +137,8 @@ func (res listCertsRes) Headers() map[string]string { type viewCertRes struct { SerialNumber string `json:"serial_number"` - Certificate *string `json:"certificate"` - Key *string `json:"key"` + Certificate string `json:"certificate"` + Key string `json:"key"` Revoked bool `json:"revoked"` ExpiryTime time.Time `json:"expiry_time"` EntityID string `json:"entity_id"` diff --git a/certs.go b/certs.go index 91ac031..bfbe54e 100644 --- a/certs.go +++ b/certs.go @@ -10,18 +10,18 @@ import ( ) type Certificate struct { - SerialNumber string `json:"serial_number" db:"serial_number"` - Certificate []byte `json:"certificate" db:"certificate"` - Key []byte `json:"key" db:"key"` - Revoked bool `json:"revoked" db:"revoked"` - ExpiryTime time.Time `json:"expiry_time" db:"expiry_time"` - EntityID string `json:"entity_id" db:"entity_id"` - DownloadUrl string `json:"-" db:"-"` + SerialNumber string `db:"serial_number"` + Certificate []byte `db:"certificate"` + Key []byte `db:"key"` + Revoked bool `db:"revoked"` + ExpiryTime time.Time `db:"expiry_time"` + EntityID string `db:"entity_id"` + DownloadUrl string `db:"-"` } type CertificatePage struct { PageMetadata - Certificates []Certificate `json:"certificates"` + Certificates []Certificate } type PageMetadata struct { diff --git a/sdk/sdk.go b/sdk/sdk.go index 4906422..a371ca3 100644 --- a/sdk/sdk.go +++ b/sdk/sdk.go @@ -57,8 +57,8 @@ type Token struct { type Certificate struct { SerialNumber string `json:"serial_number"` - Certificate *string `json:"certificate,omitempty"` - Key *string `json:"key,omitempty"` + Certificate string `json:"certificate,omitempty"` + Key string `json:"key,omitempty"` Revoked bool `json:"revoked"` ExpiryTime time.Time `json:"expiry_time"` EntityID string `json:"entity_id"` @@ -336,6 +336,9 @@ func (pm PageMetadata) query() (string, error) { if pm.Total != 0 { q.Add("total", strconv.FormatUint(pm.Total, 10)) } + if pm.EntityID != "" { + q.Add("entity_id", pm.EntityID) + } if pm.Token != "" { q.Add("token", pm.Token) }