feat: add atproto OAuth login and token commands
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.
This commit is contained in:
1 parent
653da47753
commit
fb79db4e21
9 files changed
+1531
-6
No files matched your search
@@ -0,0 +1,118 @@
|
||||
package atproto
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"crypto/elliptic"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"math/big"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func verifyProof(t *testing.T, proof string) (header map[string]any, claims map[string]any) {
|
||||
t.Helper()
|
||||
parts := strings.Split(proof, ".")
|
||||
if len(parts) != 3 {
|
||||
t.Fatalf("proof has %d parts, want 3", len(parts))
|
||||
}
|
||||
headerJSON, err := base64.RawURLEncoding.DecodeString(parts[0])
|
||||
if err != nil {
|
||||
t.Fatalf("decode header: %v", err)
|
||||
}
|
||||
claimsJSON, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
t.Fatalf("decode claims: %v", err)
|
||||
}
|
||||
sig, err := base64.RawURLEncoding.DecodeString(parts[2])
|
||||
if err != nil {
|
||||
t.Fatalf("decode signature: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(headerJSON, &header); err != nil {
|
||||
t.Fatalf("unmarshal header: %v", err)
|
||||
}
|
||||
if err := json.Unmarshal(claimsJSON, &claims); err != nil {
|
||||
t.Fatalf("unmarshal claims: %v", err)
|
||||
}
|
||||
|
||||
jwkRaw, err := json.Marshal(header["jwk"])
|
||||
if err != nil {
|
||||
t.Fatalf("marshal jwk: %v", err)
|
||||
}
|
||||
var jwk JWK
|
||||
if err := json.Unmarshal(jwkRaw, &jwk); err != nil {
|
||||
t.Fatalf("unmarshal jwk: %v", err)
|
||||
}
|
||||
if jwk.D != "" {
|
||||
t.Fatal("proof header jwk leaks the private key")
|
||||
}
|
||||
x, _ := base64.RawURLEncoding.DecodeString(jwk.X)
|
||||
y, _ := base64.RawURLEncoding.DecodeString(jwk.Y)
|
||||
pub := &ecdsa.PublicKey{
|
||||
Curve: elliptic.P256(),
|
||||
X: new(big.Int).SetBytes(x),
|
||||
Y: new(big.Int).SetBytes(y),
|
||||
}
|
||||
if len(sig) != 64 {
|
||||
t.Fatalf("signature is %d bytes, want 64 (r||s)", len(sig))
|
||||
}
|
||||
digest := sha256.Sum256([]byte(parts[0] + "." + parts[1]))
|
||||
r := new(big.Int).SetBytes(sig[:32])
|
||||
s := new(big.Int).SetBytes(sig[32:])
|
||||
if !ecdsa.Verify(pub, digest[:], r, s) {
|
||||
t.Fatal("DPoP proof signature does not verify")
|
||||
}
|
||||
return header, claims
|
||||
}
|
||||
|
||||
func TestDPoPProofAndKeyRoundTrip(t *testing.T) {
|
||||
key, err := GenerateDPoPKey()
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateDPoPKey: %v", err)
|
||||
}
|
||||
|
||||
proof, err := key.Proof("post", "https://bsky.social/oauth/token?ignored=1#frag", "nonce-1", "access-1")
|
||||
if err != nil {
|
||||
t.Fatalf("Proof: %v", err)
|
||||
}
|
||||
header, claims := verifyProof(t, proof)
|
||||
if header["typ"] != "dpop+jwt" || header["alg"] != "ES256" {
|
||||
t.Fatalf("header = %v, want typ dpop+jwt alg ES256", header)
|
||||
}
|
||||
if claims["htm"] != "POST" {
|
||||
t.Fatalf("htm = %v, want POST", claims["htm"])
|
||||
}
|
||||
if claims["htu"] != "https://bsky.social/oauth/token" {
|
||||
t.Fatalf("htu = %v, want query/fragment stripped", claims["htu"])
|
||||
}
|
||||
if claims["nonce"] != "nonce-1" {
|
||||
t.Fatalf("nonce = %v, want nonce-1", claims["nonce"])
|
||||
}
|
||||
sum := sha256.Sum256([]byte("access-1"))
|
||||
if claims["ath"] != base64.RawURLEncoding.EncodeToString(sum[:]) {
|
||||
t.Fatalf("ath = %v, want sha256(access_token)", claims["ath"])
|
||||
}
|
||||
if claims["jti"] == nil || claims["iat"] == nil {
|
||||
t.Fatalf("claims missing jti/iat: %v", claims)
|
||||
}
|
||||
|
||||
parsed, err := ParseDPoPKey(key.PrivateJWK())
|
||||
if err != nil {
|
||||
t.Fatalf("ParseDPoPKey: %v", err)
|
||||
}
|
||||
if parsed.Thumbprint() != key.Thumbprint() {
|
||||
t.Fatal("thumbprint changed across key round-trip")
|
||||
}
|
||||
proof2, err := parsed.Proof("GET", "https://example.com/xrpc/test", "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Proof after round-trip: %v", err)
|
||||
}
|
||||
_, claims2 := verifyProof(t, proof2)
|
||||
if _, ok := claims2["ath"]; ok {
|
||||
t.Fatal("ath must be omitted without an access token")
|
||||
}
|
||||
if _, ok := claims2["nonce"]; ok {
|
||||
t.Fatal("nonce must be omitted when empty")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user