New internal/atproto package implements the atproto OAuth profile as a public client: handle/DID resolution with bidirectional verification, PDS and authorization-server metadata discovery, PAR + PKCE, ES256 DPoP proofs with server-nonce retry, and token exchange/refresh with sub verification. Sessions (tokens + DPoP key) are saved 0600. 'latha-auth atproto' collects the code via the existing deploy relay; 'latha-auth atproto-token' prints a refreshed access token.
155 lines
5.6 KiB
Go
155 lines
5.6 KiB
Go
package atproto
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
type ProtectedResourceMetadata struct {
|
|
Resource string `json:"resource"`
|
|
AuthorizationServers []string `json:"authorization_servers"`
|
|
}
|
|
|
|
type AuthServerMetadata struct {
|
|
Issuer string `json:"issuer"`
|
|
AuthorizationEndpoint string `json:"authorization_endpoint"`
|
|
TokenEndpoint string `json:"token_endpoint"`
|
|
PushedAuthorizationRequestEndpoint string `json:"pushed_authorization_request_endpoint"`
|
|
RevocationEndpoint string `json:"revocation_endpoint,omitempty"`
|
|
ScopesSupported []string `json:"scopes_supported"`
|
|
GrantTypesSupported []string `json:"grant_types_supported"`
|
|
CodeChallengeMethodsSupported []string `json:"code_challenge_methods_supported"`
|
|
TokenEndpointAuthMethodsSupported []string `json:"token_endpoint_auth_methods_supported"`
|
|
DPoPSigningAlgValuesSupported []string `json:"dpop_signing_alg_values_supported"`
|
|
RequirePushedAuthorizationRequests bool `json:"require_pushed_authorization_requests"`
|
|
AuthorizationResponseIssParameterSupported bool `json:"authorization_response_iss_parameter_supported"`
|
|
}
|
|
|
|
// ResolvedIdentity is an account plus the authorization server that its
|
|
// own DID document (via its PDS) points at. The issuer is therefore
|
|
// bound to the session before any browser interaction happens.
|
|
type ResolvedIdentity struct {
|
|
DID string
|
|
Handle string
|
|
PDS string
|
|
Issuer string
|
|
AuthServer *AuthServerMetadata
|
|
}
|
|
|
|
func checkHTTPSOrigin(raw, what string) error {
|
|
u, err := url.Parse(raw)
|
|
if err != nil {
|
|
return fmt.Errorf("parse %s %q: %w", what, raw, err)
|
|
}
|
|
host := u.Hostname()
|
|
if u.Scheme == "http" && (host == "localhost" || host == "127.0.0.1" || host == "::1") {
|
|
return nil
|
|
}
|
|
if u.Scheme != "https" {
|
|
return fmt.Errorf("%s %q must use https", what, raw)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FetchProtectedResource(ctx context.Context, client *http.Client, pdsURL string) (*ProtectedResourceMetadata, error) {
|
|
client = httpClientOrDefault(client)
|
|
if err := checkHTTPSOrigin(pdsURL, "PDS URL"); err != nil {
|
|
return nil, err
|
|
}
|
|
var out ProtectedResourceMetadata
|
|
endpoint := strings.TrimRight(pdsURL, "/") + "/.well-known/oauth-protected-resource"
|
|
if err := getJSON(ctx, client, endpoint, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(out.AuthorizationServers) == 0 {
|
|
return nil, fmt.Errorf("protected resource metadata at %s lists no authorization_servers", endpoint)
|
|
}
|
|
issuer := strings.TrimRight(out.AuthorizationServers[0], "/")
|
|
if err := checkHTTPSOrigin(issuer, "authorization server"); err != nil {
|
|
return nil, err
|
|
}
|
|
out.AuthorizationServers[0] = issuer
|
|
return &out, nil
|
|
}
|
|
|
|
func (m *AuthServerMetadata) validate(wantIssuer string) error {
|
|
if m.Issuer != wantIssuer {
|
|
return fmt.Errorf("authorization server issuer %q does not match %q", m.Issuer, wantIssuer)
|
|
}
|
|
if m.AuthorizationEndpoint == "" || m.TokenEndpoint == "" {
|
|
return fmt.Errorf("authorization server metadata missing authorization/token endpoint")
|
|
}
|
|
if !m.RequirePushedAuthorizationRequests || m.PushedAuthorizationRequestEndpoint == "" {
|
|
return fmt.Errorf("authorization server does not require PAR (atproto OAuth profile requires it)")
|
|
}
|
|
if !slices.Contains(m.CodeChallengeMethodsSupported, "S256") {
|
|
return fmt.Errorf("authorization server does not support PKCE S256")
|
|
}
|
|
if !slices.Contains(m.DPoPSigningAlgValuesSupported, "ES256") {
|
|
return fmt.Errorf("authorization server does not support DPoP ES256")
|
|
}
|
|
if !slices.Contains(m.ScopesSupported, "atproto") {
|
|
return fmt.Errorf("authorization server does not support the atproto scope")
|
|
}
|
|
if !slices.Contains(m.GrantTypesSupported, "authorization_code") ||
|
|
!slices.Contains(m.GrantTypesSupported, "refresh_token") {
|
|
return fmt.Errorf("authorization server must support authorization_code and refresh_token grants")
|
|
}
|
|
if !slices.Contains(m.TokenEndpointAuthMethodsSupported, "none") {
|
|
return fmt.Errorf("authorization server does not support public clients (token_endpoint_auth_method none)")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FetchAuthServerMetadata(ctx context.Context, client *http.Client, issuer string) (*AuthServerMetadata, error) {
|
|
client = httpClientOrDefault(client)
|
|
issuer = strings.TrimRight(issuer, "/")
|
|
if err := checkHTTPSOrigin(issuer, "authorization server"); err != nil {
|
|
return nil, err
|
|
}
|
|
var out AuthServerMetadata
|
|
endpoint := issuer + "/.well-known/oauth-authorization-server"
|
|
if err := getJSON(ctx, client, endpoint, &out); err != nil {
|
|
return nil, err
|
|
}
|
|
out.Issuer = strings.TrimRight(out.Issuer, "/")
|
|
if err := out.validate(issuer); err != nil {
|
|
return nil, err
|
|
}
|
|
return &out, nil
|
|
}
|
|
|
|
// ResolveAuthServer walks identifier -> DID document -> PDS ->
|
|
// protected-resource metadata -> authorization server metadata.
|
|
func ResolveAuthServer(ctx context.Context, client *http.Client, plcURL, identifier string) (*ResolvedIdentity, error) {
|
|
client = httpClientOrDefault(client)
|
|
did, handle, doc, err := ResolveIdentifier(ctx, client, plcURL, identifier)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pds, ok := doc.PDSEndpoint()
|
|
if !ok {
|
|
return nil, fmt.Errorf("DID document for %s has no #atproto_pds service", did)
|
|
}
|
|
pr, err := FetchProtectedResource(ctx, client, pds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
issuer := pr.AuthorizationServers[0]
|
|
meta, err := FetchAuthServerMetadata(ctx, client, issuer)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &ResolvedIdentity{
|
|
DID: did,
|
|
Handle: handle,
|
|
PDS: pds,
|
|
Issuer: issuer,
|
|
AuthServer: meta,
|
|
}, nil
|
|
}
|