1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package oauthclient
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/bluesky-social/indigo/atproto/identity"
"github.com/bluesky-social/indigo/atproto/syntax"
)
func BindEntrywayAccount(ctx context.Context, client *http.Client, directory identity.Directory, data SessionData) (SessionData, error) {
entryway := strings.TrimRight(data.AuthServerURL, "/")
if directory == nil {
return SessionData{}, fmt.Errorf("oauthclient: no identity directory resolves the account the entryway %s signed in", entryway)
}
did, err := syntax.ParseDID(data.AccountDID)
if err != nil {
return SessionData{}, fmt.Errorf("oauthclient: the entryway account DID: %w", err)
}
ident, err := directory.LookupDID(ctx, did)
if err != nil {
return SessionData{}, fmt.Errorf("oauthclient: resolving %s, the account the entryway %s signed in: %w", did, entryway, err)
}
pds := strings.TrimRight(strings.TrimSpace(ident.PDSEndpoint()), "/")
if pds == "" {
return SessionData{}, fmt.Errorf("oauthclient: %s names no PDS", did)
}
authServer, tokenEndpoint, revokeEndpoint, err := DiscoverAuthServer(ctx, client, pds)
if err != nil {
return SessionData{}, fmt.Errorf("oauthclient: the PDS %s of %s: %w", pds, did, err)
}
if authServer != entryway {
return SessionData{}, fmt.Errorf("oauthclient: the PDS %s of %s names the authorization server %s, not the entryway %s", pds, did, authServer, entryway)
}
if err := checkAudience(data.AccessToken, pds); err != nil {
return SessionData{}, err
}
data.HostURL = pds
data.SessionID = SessionID(did.String(), pds)
data.AuthServerURL = authServer
data.AuthServerTokenEndpoint = tokenEndpoint
data.AuthServerRevocationEndpoint = revokeEndpoint
data.DPoPHostNonce = ""
return data, nil
}
func checkAudience(token, pds string) error {
audiences := tokenAudiences(token)
host, err := url.Parse(pds)
if err != nil || host.Host == "" {
return fmt.Errorf("oauthclient: the PDS %q is not an absolute URL", pds)
}
checked := false
for _, aud := range audiences {
name, ok := strings.CutPrefix(aud, "did:web:")
if !ok {
continue
}
decoded, err := url.PathUnescape(name)
if err != nil {
continue
}
if strings.EqualFold(decoded, host.Host) {
return nil
}
checked = true
}
if checked {
return fmt.Errorf("oauthclient: the access token is for %s, not the PDS %s", strings.Join(audiences, ", "), pds)
}
return nil
}
func tokenAudiences(token string) []string {
parts := strings.Split(token, ".")
if len(parts) < 2 {
return nil
}
raw, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil
}
var claims struct {
Audience json.RawMessage `json:"aud"`
}
if json.Unmarshal(raw, &claims) != nil || len(claims.Audience) == 0 {
return nil
}
var one string
if json.Unmarshal(claims.Audience, &one) == nil {
return []string{one}
}
var many []string
if json.Unmarshal(claims.Audience, &many) == nil {
return many
}
return nil
}