Reorg client code

This commit is contained in:
cheddar 2025-02-20 21:49:05 -05:00
parent 61aa1be730
commit f313a8f1c9
No known key found for this signature in database
4 changed files with 58 additions and 37 deletions

View file

@ -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) {

View file

@ -6,11 +6,12 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"crispbyte.dev/sig-auth/server"
)
func RegisterKey(key string, userId string) error {
func RegisterKey(baseUrl *url.URL, key string, userId string) error {
request := server.RegisterRequest{
UserId: userId,
Key: key,
@ -18,8 +19,10 @@ func RegisterKey(key string, userId string) error {
json_data, _ := json.Marshal(request)
registerUrl := baseUrl.JoinPath("register")
resp, err := http.DefaultClient.Post(
"http://localhost:8080/register",
registerUrl.String(),
"application/json",
bytes.NewBuffer(json_data))