64 lines
1.4 KiB
Go
64 lines
1.4 KiB
Go
package signer
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"git.front.kjuulh.io/kjuulh/curre"
|
|
"github.com/ProtonMail/gopenpgp/v2/crypto"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type OpenPGP struct {
|
|
logger *zap.Logger
|
|
PrivateKeyRing *crypto.KeyRing
|
|
config *OpenPgpConfig
|
|
}
|
|
|
|
type OpenPgpConfig struct {
|
|
PrivateKeyFilePath string
|
|
PrivateKeyPassword string
|
|
}
|
|
|
|
func NewOpenPGP(logger *zap.Logger, config *OpenPgpConfig) *OpenPGP {
|
|
return &OpenPGP{
|
|
logger: logger,
|
|
config: config,
|
|
}
|
|
}
|
|
|
|
func NewOpenPGPApp(openPGP *OpenPGP) curre.Component {
|
|
return curre.NewFunctionalComponent(&curre.FunctionalComponent{
|
|
InitFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
|
|
|
|
content, err := os.ReadFile(openPGP.config.PrivateKeyFilePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
privateKeyObj, err := crypto.NewKeyFromArmored(string(content))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
unlockedPrivateKeyRing, err := privateKeyObj.Unlock([]byte(openPGP.config.PrivateKeyPassword))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
privateKeyRing, err := crypto.NewKeyRing(unlockedPrivateKeyRing)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
openPGP.PrivateKeyRing = privateKeyRing
|
|
|
|
return nil
|
|
},
|
|
StartFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
|
|
return nil
|
|
},
|
|
StopFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
|
|
return nil
|
|
},
|
|
})
|
|
|
|
}
|