oficina / agent-access Public

Clone
git clone https://amadan.net/oficina/agent-access

Plain git — no account needed to clone.

Download this file

1package agentaccess
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "io"
8 "net/http"
9 "net/url"
10 "time"
11)
12
13// RemoteVerifier checks an app-audience token online so revocation is observed
14// on the next request. Resolve the returned Home subject locally before use.
15type RemoteVerifier struct {
16 Endpoint, Audience string
17 IdentityIssuer string
18 Client *http.Client
19}
20
21func NewRemoteVerifier(issuer, audience, identityIssuer string) (*RemoteVerifier, error) {
22 u, err := url.Parse(issuer)
23 a, ae := url.Parse(audience)
24 i, ie := url.Parse(identityIssuer)
25 if ie != nil || i.Scheme != "https" || i.Host == "" || i.User != nil || i.Path != "" || i.RawQuery != "" || i.Fragment != "" || identityIssuer != "https://"+i.Host || identityIssuer == issuer || err != nil || ae != nil || u.Scheme != "https" || u.Host == "" || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" || issuer != "https://"+u.Host || a.Scheme != "https" || a.Host == "" || a.User != nil || a.RawQuery != "" || a.Fragment != "" {
26 return nil, Fail("invalid", "Use trusted HTTPS issuer and app resource URLs.")
27 }
28 return &RemoteVerifier{Endpoint: issuer + "/api/v1/credential", Audience: audience, IdentityIssuer: identityIssuer, Client: &http.Client{Timeout: 10 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}, nil
29}
30func (v *RemoteVerifier) Verify(ctx context.Context, token string) (Principal, error) {
31 raw, _ := json.Marshal(map[string]string{"resource": v.Audience})
32 req, err := http.NewRequestWithContext(ctx, "POST", v.Endpoint, bytes.NewReader(raw))
33 if err != nil {
34 return Principal{}, Fail("unavailable", "Authorization is unavailable.")
35 }
36 req.Header.Set("Authorization", "Bearer "+token)
37 req.Header.Set("Content-Type", "application/json")
38 response, err := v.Client.Do(req)
39 if err != nil {
40 return Principal{}, Fail("unavailable", "Authorization is unavailable.")
41 }
42 defer response.Body.Close()
43 if response.StatusCode == 401 {
44 return Principal{}, Fail("unauthenticated", "Connect to Oficina again.")
45 }
46 if response.StatusCode != 200 {
47 return Principal{}, Fail("unavailable", "Authorization is unavailable.")
48 }
49 body, err := io.ReadAll(io.LimitReader(response.Body, 65537))
50 if err != nil || len(body) > 65536 {
51 return Principal{}, Fail("unavailable", "Authorization is unavailable.")
52 }
53 var out struct {
54 Resource string `json:"resource"`
55 IdentityIssuer string `json:"identity_issuer"`
56 Principal Principal `json:"principal"`
57 }
58 if UniqueJSON(body) != nil || json.Unmarshal(body, &out) != nil || v.IdentityIssuer == "" || out.IdentityIssuer != v.IdentityIssuer || out.Resource != v.Audience || out.Principal.Subject == "" || out.Principal.Grant == "" {
59 return Principal{}, Fail("unavailable", "Authorization is unavailable.")
60 }
61 return out.Principal, nil
62}
63