mirror of
https://tangled.org/evan.jarrett.net/at-container-registry
synced 2026-09-26 12:14:17 +00:00
cleanup more auth
This commit is contained in:
@@ -12,7 +12,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
atprotoclient "atcr.io/pkg/atproto"
|
||||
"github.com/bluesky-social/indigo/atproto/identity"
|
||||
"github.com/bluesky-social/indigo/atproto/syntax"
|
||||
)
|
||||
|
||||
// CachedSession represents a cached session
|
||||
@@ -25,7 +26,7 @@ type CachedSession struct {
|
||||
|
||||
// SessionValidator validates ATProto credentials
|
||||
type SessionValidator struct {
|
||||
resolver *atprotoclient.Resolver
|
||||
directory identity.Directory
|
||||
httpClient *http.Client
|
||||
cache map[string]*CachedSession
|
||||
cacheMu sync.RWMutex
|
||||
@@ -34,7 +35,7 @@ type SessionValidator struct {
|
||||
// NewSessionValidator creates a new ATProto session validator
|
||||
func NewSessionValidator() *SessionValidator {
|
||||
return &SessionValidator{
|
||||
resolver: atprotoclient.NewResolver(),
|
||||
directory: identity.DefaultDirectory(),
|
||||
httpClient: &http.Client{},
|
||||
cache: make(map[string]*CachedSession),
|
||||
}
|
||||
@@ -86,11 +87,22 @@ type SessionResponse struct {
|
||||
// Returns the user's DID and PDS endpoint if valid
|
||||
func (v *SessionValidator) ValidateCredentials(ctx context.Context, identifier, password string) (did, pdsEndpoint string, err error) {
|
||||
// Resolve identifier (handle or DID) to PDS endpoint
|
||||
resolvedDID, pds, err := v.resolver.ResolveIdentity(ctx, identifier)
|
||||
atID, err := syntax.ParseAtIdentifier(identifier)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("invalid identifier %q: %w", identifier, err)
|
||||
}
|
||||
|
||||
ident, err := v.directory.Lookup(ctx, *atID)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("failed to resolve identity %q: %w", identifier, err)
|
||||
}
|
||||
|
||||
resolvedDID := ident.DID.String()
|
||||
pds := ident.PDSEndpoint()
|
||||
if pds == "" {
|
||||
return "", "", fmt.Errorf("no PDS endpoint found for %q", identifier)
|
||||
}
|
||||
|
||||
fmt.Printf("DEBUG: Resolved %s to DID=%s, PDS=%s\n", identifier, resolvedDID, pds)
|
||||
|
||||
// Create session with the PDS
|
||||
@@ -119,11 +131,22 @@ func (v *SessionValidator) CreateSessionAndGetToken(ctx context.Context, identif
|
||||
fmt.Printf("DEBUG [atproto/session]: No cached session for %s, creating new session\n", identifier)
|
||||
|
||||
// Resolve identifier to PDS endpoint
|
||||
did, pds, err := v.resolver.ResolveIdentity(ctx, identifier)
|
||||
atID, err := syntax.ParseAtIdentifier(identifier)
|
||||
if err != nil {
|
||||
return "", "", "", fmt.Errorf("invalid identifier %q: %w", identifier, err)
|
||||
}
|
||||
|
||||
ident, err := v.directory.Lookup(ctx, *atID)
|
||||
if err != nil {
|
||||
return "", "", "", fmt.Errorf("failed to resolve identity %q: %w", identifier, err)
|
||||
}
|
||||
|
||||
did = ident.DID.String()
|
||||
pds := ident.PDSEndpoint()
|
||||
if pds == "" {
|
||||
return "", "", "", fmt.Errorf("no PDS endpoint found for %q", identifier)
|
||||
}
|
||||
|
||||
// Create session
|
||||
sessionResp, err := v.createSession(ctx, pds, identifier, password)
|
||||
if err != nil {
|
||||
|
||||
@@ -7,7 +7,8 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
mainAtproto "atcr.io/pkg/atproto"
|
||||
"github.com/bluesky-social/indigo/atproto/identity"
|
||||
"github.com/bluesky-social/indigo/atproto/syntax"
|
||||
)
|
||||
|
||||
// TokenValidator validates ATProto OAuth access tokens
|
||||
@@ -90,12 +91,22 @@ func (v *TokenValidator) ValidateToken(ctx context.Context, pdsEndpoint, accessT
|
||||
// dpopProof is optional - if provided, uses DPoP auth; otherwise uses Bearer
|
||||
func (v *TokenValidator) ValidateTokenWithResolver(ctx context.Context, handle, accessToken, dpopProof string) (*SessionInfo, error) {
|
||||
// Resolve handle to PDS endpoint
|
||||
resolver := mainAtproto.NewResolver()
|
||||
_, pdsEndpoint, err := resolver.ResolveIdentity(ctx, handle)
|
||||
directory := identity.DefaultDirectory()
|
||||
atID, err := syntax.ParseAtIdentifier(handle)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid identifier %q: %w", handle, err)
|
||||
}
|
||||
|
||||
ident, err := directory.Lookup(ctx, *atID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to resolve PDS endpoint: %w", err)
|
||||
}
|
||||
|
||||
pdsEndpoint := ident.PDSEndpoint()
|
||||
if pdsEndpoint == "" {
|
||||
return nil, fmt.Errorf("no PDS endpoint found for %q", handle)
|
||||
}
|
||||
|
||||
// Validate token against the PDS
|
||||
return v.ValidateToken(ctx, pdsEndpoint, accessToken, dpopProof)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user