2025-02-06 14:19:30 +01:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"iter"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2026-01-08 15:45:56 -05:00
|
|
|
"code.sonr.org/go/did-it"
|
2025-08-05 12:11:20 +02:00
|
|
|
|
2026-01-08 15:45:56 -05:00
|
|
|
"code.sonr.org/go/ucan/pkg/command"
|
|
|
|
|
"code.sonr.org/go/ucan/token/delegation"
|
2025-02-06 14:19:30 +01:00
|
|
|
|
2026-01-08 15:45:56 -05:00
|
|
|
"code.sonr.org/go/ucan/toolkit/client"
|
|
|
|
|
"code.sonr.org/go/ucan/toolkit/issuer"
|
2025-02-06 14:19:30 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var _ client.DelegationRequester = &Requester{}
|
|
|
|
|
|
|
|
|
|
type Requester struct {
|
|
|
|
|
issuerURL string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewRequester(issuerURL string) *Requester {
|
|
|
|
|
return &Requester{issuerURL: issuerURL}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r Requester) RequestDelegation(ctx context.Context, audience did.DID, cmd command.Command, subject did.DID) (iter.Seq2[*delegation.Bundle, error], error) {
|
|
|
|
|
log.Printf("requesting delegation for %s on %s", cmd, subject)
|
|
|
|
|
|
|
|
|
|
// we match the simple json protocol of the issuer
|
|
|
|
|
data := struct {
|
|
|
|
|
Audience string `json:"aud"`
|
|
|
|
|
Cmd string `json:"cmd"`
|
|
|
|
|
Subject string `json:"sub"`
|
|
|
|
|
}{
|
|
|
|
|
Audience: audience.String(),
|
|
|
|
|
Cmd: cmd.String(),
|
|
|
|
|
Subject: subject.String(),
|
|
|
|
|
}
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
err := json.NewEncoder(buf).Encode(data)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 13:16:53 +01:00
|
|
|
req, err := http.NewRequest(http.MethodPost, r.issuerURL, buf)
|
2025-02-06 14:19:30 +01:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res, err := http.DefaultClient.Do(req.WithContext(ctx))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 12:11:20 +02:00
|
|
|
return issuer.DecodeResponse(res, audience, cmd, subject)
|
2025-02-06 14:19:30 +01:00
|
|
|
}
|