sig-auth/client/client.go

41 lines
859 B
Go
Raw Normal View History

2025-02-10 23:07:41 -05:00
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{
2025-02-16 13:51:12 -05:00
Tag: "auth",
2025-02-10 23:07:41 -05:00
KeyID: keyId,
Alg: alg,
})
return client, nil
}