Reorg client code
This commit is contained in:
parent
61aa1be730
commit
f313a8f1c9
4 changed files with 58 additions and 37 deletions
|
@ -1,12 +1,14 @@
|
|||
package client
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto"
|
||||
"crypto/ecdsa"
|
||||
"crypto/ed25519"
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
|
||||
"github.com/common-fate/httpsig"
|
||||
|
@ -14,9 +16,42 @@ import (
|
|||
"github.com/common-fate/httpsig/alg_ed25519"
|
||||
"github.com/common-fate/httpsig/alg_rsa"
|
||||
"github.com/common-fate/httpsig/signer"
|
||||
"github.com/opencontainers/go-digest"
|
||||
)
|
||||
|
||||
func GetSigningClient(key crypto.PrivateKey, keyId string) (*http.Client, error) {
|
||||
func Post(baseUrl *url.URL, key crypto.PrivateKey, keyId string, data []byte, simulateCaddy bool) (*http.Response, error) {
|
||||
client, err := getSigningClient(key, keyId)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id := digest.FromBytes(data)
|
||||
|
||||
authUrl := baseUrl.JoinPath("auth")
|
||||
|
||||
var req *http.Request
|
||||
|
||||
req, err = http.NewRequest("POST", authUrl.String(), bytes.NewBuffer(data))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.Header.Add("Content-Digest", string(id.Algorithm())+"="+id.Encoded())
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
|
||||
if simulateCaddy {
|
||||
req.Header.Add("X-Forwarded-Method", req.Method)
|
||||
req.Header.Add("X-Forwarded-Uri", req.RequestURI)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func getSigningClient(key crypto.PrivateKey, keyId string) (*http.Client, error) {
|
||||
var alg signer.Algorithm
|
||||
|
||||
switch p := key.(type) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue