sig-auth/client/client.go
2025-02-22 15:10:35 -05:00

40 lines
863 B
Go

package client
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/rsa"
"fmt"
"net/http"
"reflect"
"github.com/common-fate/httpsig"
"github.com/common-fate/httpsig/alg_ecdsa"
"github.com/common-fate/httpsig/alg_ed25519"
"github.com/common-fate/httpsig/alg_rsa"
"github.com/common-fate/httpsig/signer"
)
func GetSigningClient(key crypto.PrivateKey, keyId string) (*http.Client, error) {
var alg signer.Algorithm
switch p := key.(type) {
case *rsa.PrivateKey:
alg = alg_rsa.NewRSAPKCS256Signer(p)
case *ed25519.PrivateKey:
alg = alg_ed25519.Ed25519{PrivateKey: *p}
case *ecdsa.PrivateKey:
alg = alg_ecdsa.NewP256Signer(p)
default:
return nil, fmt.Errorf("type is unknown: %s", reflect.TypeOf(key))
}
client := httpsig.NewClient(httpsig.ClientOpts{
Tag: "test-tag",
KeyID: keyId,
Alg: alg,
})
return client, nil
}