Files
probo/pkg/esign/certgen.go
Sacha Al Himdani 4c57d201a4 Make license declarations consistently MIT
The source headers, LICENSE files, and license metadata had drifted
apart. Align the entire project to MIT:

- Convert every source-file header to the MIT text across all comment
  styles (Go, TS, TSX, JS, MJS, SQL, CSS, GraphQL, shell), including
  SPDX-License-Identifier tags
- Set the root and cookie-banner LICENSE files to the MIT text with a
  "MIT License" title line
- Switch the package.json license fields, Docker image label, and
  cookie-banner README to MIT
- Update docs and the genmodels header generator accordingly
- Normalize copyright lines to a single format
  (Copyright (c) <year(s)> Probo Inc <hello@probo.com>.): unify the
  hello@getprobo.com and hello@probo.inc emails to hello@probo.com and
  the comma-separated years to a hyphenated range

Genuine third-party references are intentionally left untouched: the
Lucide icon attributions (Lucide is ISC) and the trivy dependency
license allowlist.

Signed-off-by: Sacha Al Himdani <sacha@probo.com>
2026-07-13 16:21:14 +02:00

177 lines
5.1 KiB
Go

// Copyright (c) 2026 Probo Inc <hello@probo.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package esign
import (
"bytes"
"context"
_ "embed"
"fmt"
"html/template"
"io"
"strings"
"time"
"github.com/digitorus/timestamp"
"go.gearno.de/x/ref"
"go.probo.inc/probo/pkg/coredata"
"go.probo.inc/probo/pkg/html2pdf"
)
type (
CertificateGenerator struct {
HTML2PDFConverter *html2pdf.Converter
}
certificateData struct {
SignatureID string
OrganizationID string
SignerFullName string
SignerEmail string
SignerIPAddress string
SignerUserAgent string
DocumentType string
DocumentTypeName string
FileID string
FileHash string
Seal string
SealVersion int
ConsentText string
SignedAt string
TSAAuthority string
TSATime string
TSASerial string
Events []certificateEvent
}
certificateEvent struct {
EventType string
Source string
Actor string
IPAddress string
OccurredAt string
}
)
var (
//go:embed certificate.html.tmpl
certificateTemplateHTML string
certificateTemplate = template.Must(template.New("certificate").Parse(certificateTemplateHTML))
)
func (g *CertificateGenerator) Generate(
ctx context.Context,
signature *coredata.ElectronicSignature,
events coredata.ElectronicSignatureEvents,
) (io.Reader, error) {
data := certificateData{
SignatureID: signature.ID.String(),
OrganizationID: signature.OrganizationID.String(),
SignerFullName: ref.UnrefOrZero(signature.SignerFullName),
SignerEmail: signature.SignerEmail,
SignerIPAddress: ref.UnrefOrZero(signature.SignerIPAddress),
SignerUserAgent: ref.UnrefOrZero(signature.SignerUserAgent),
DocumentType: signature.DocumentType.String(),
DocumentTypeName: signature.DocumentType.DisplayName(),
FileID: signature.FileID.String(),
FileHash: ref.UnrefOrZero(signature.FileHash),
Seal: ref.UnrefOrZero(signature.Seal),
SealVersion: signature.SealVersion,
ConsentText: signature.ConsentText,
}
if signature.SignedAt == nil {
return nil, fmt.Errorf("cannot generate certificate: signature %s has no signed_at timestamp", signature.ID)
}
data.SignedAt = signature.SignedAt.UTC().Format(time.RFC3339)
if len(signature.TSAToken) == 0 {
return nil, fmt.Errorf("cannot generate certificate: signature %s has no TSA token", signature.ID)
}
tsResp, err := timestamp.ParseResponse(signature.TSAToken)
if err != nil {
return nil, fmt.Errorf("cannot parse TSA token for signature %s: %w", signature.ID, err)
}
data.TSATime = tsResp.Time.UTC().Format(time.RFC3339)
data.TSASerial = tsResp.SerialNumber.String()
data.TSAAuthority = tsaAuthorityName(tsResp)
for _, evt := range events {
data.Events = append(
data.Events,
certificateEvent{
EventType: evt.EventType.String(),
Source: evt.EventSource.String(),
Actor: evt.ActorEmail,
IPAddress: evt.ActorIPAddress,
OccurredAt: evt.OccurredAt.UTC().Format(time.RFC3339),
},
)
}
var htmlBuf bytes.Buffer
if err := certificateTemplate.Execute(&htmlBuf, data); err != nil {
return nil, fmt.Errorf("cannot render certificate template: %w", err)
}
pdfReader, err := g.HTML2PDFConverter.GeneratePDF(
ctx,
htmlBuf.Bytes(),
html2pdf.RenderConfig{
PageFormat: html2pdf.PageFormatA4,
Orientation: html2pdf.OrientationPortrait,
MarginTop: html2pdf.NewMarginMillimeters(20),
MarginBottom: html2pdf.NewMarginMillimeters(20),
MarginLeft: html2pdf.NewMarginMillimeters(20),
MarginRight: html2pdf.NewMarginMillimeters(20),
PrintBackground: true,
},
)
if err != nil {
return nil, fmt.Errorf("cannot generate certificate PDF: %w", err)
}
return pdfReader, nil
}
func tsaAuthorityName(ts *timestamp.Timestamp) string {
if len(ts.Certificates) == 0 {
return ""
}
cert := ts.Certificates[0]
if len(cert.Subject.Organization) > 0 {
org := strings.Join(cert.Subject.Organization, ", ")
if cert.Subject.CommonName != "" {
return org + " (" + cert.Subject.CommonName + ")"
}
return org
}
return cert.Subject.CommonName
}