···
+
"github.com/golang-jwt/jwt/v5"
+
// Claims represents the standard JWT claims we care about
+
Scope string `json:"scope,omitempty"`
+
// ParseJWT parses a JWT token without verification (Phase 1)
+
// Returns the claims if the token is valid JSON and has required fields
+
func ParseJWT(tokenString string) (*Claims, error) {
+
// Remove "Bearer " prefix if present
+
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
+
tokenString = strings.TrimSpace(tokenString)
+
// Parse without verification first to extract claims
+
parser := jwt.NewParser(jwt.WithoutClaimsValidation())
+
token, _, err := parser.ParseUnverified(tokenString, &Claims{})
+
return nil, fmt.Errorf("failed to parse JWT: %w", err)
+
claims, ok := token.Claims.(*Claims)
+
return nil, fmt.Errorf("invalid claims type")
+
// Validate required fields
+
if claims.Subject == "" {
+
return nil, fmt.Errorf("missing 'sub' claim (user DID)")
+
// atProto PDSes may use 'aud' instead of 'iss' for the authorization server
+
// If 'iss' is missing, use 'aud' as the authorization server identifier
+
if claims.Issuer == "" {
+
if len(claims.Audience) > 0 {
+
claims.Issuer = claims.Audience[0]
+
return nil, fmt.Errorf("missing both 'iss' and 'aud' claims (authorization server)")
+
// Validate claims (even in Phase 1, we need basic validation like expiry)
+
if err := validateClaims(claims); err != nil {
+
// VerifyJWT verifies a JWT token's signature and claims (Phase 2)
+
// Fetches the public key from the issuer's JWKS endpoint and validates the signature
+
func VerifyJWT(ctx context.Context, tokenString string, keyFetcher JWKSFetcher) (*Claims, error) {
+
// First parse to get the issuer
+
claims, err := ParseJWT(tokenString)
+
// Fetch the public key from the issuer
+
publicKey, err := keyFetcher.FetchPublicKey(ctx, claims.Issuer, tokenString)
+
return nil, fmt.Errorf("failed to fetch public key: %w", err)
+
// Now parse and verify with the public key
+
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
+
token, err := jwt.ParseWithClaims(tokenString, &Claims{}, func(token *jwt.Token) (interface{}, error) {
+
// Validate signing method - support both RSA and ECDSA (atProto uses ES256 primarily)
+
switch token.Method.(type) {
+
case *jwt.SigningMethodRSA, *jwt.SigningMethodECDSA:
+
// Valid signing methods for atProto
+
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
+
return nil, fmt.Errorf("failed to verify JWT: %w", err)
+
return nil, fmt.Errorf("token is invalid")
+
verifiedClaims, ok := token.Claims.(*Claims)
+
return nil, fmt.Errorf("invalid claims type after verification")
+
// Additional validation
+
if err := validateClaims(verifiedClaims); err != nil {
+
return verifiedClaims, nil
+
// validateClaims performs additional validation on JWT claims
+
func validateClaims(claims *Claims) error {
+
if claims.ExpiresAt != nil && claims.ExpiresAt.Before(now) {
+
return fmt.Errorf("token has expired")
+
if claims.NotBefore != nil && claims.NotBefore.After(now) {
+
return fmt.Errorf("token not yet valid")
+
// Validate DID format in sub claim
+
if !strings.HasPrefix(claims.Subject, "did:") {
+
return fmt.Errorf("invalid DID format in 'sub' claim: %s", claims.Subject)
+
// Validate issuer is either an HTTPS URL or a DID
+
// atProto uses DIDs (did:web:, did:plc:) or HTTPS URLs as issuer identifiers
+
if !strings.HasPrefix(claims.Issuer, "https://") && !strings.HasPrefix(claims.Issuer, "did:") {
+
return fmt.Errorf("issuer must be HTTPS URL or DID, got: %s", claims.Issuer)
+
// Parse to ensure it's a valid URL
+
if _, err := url.Parse(claims.Issuer); err != nil {
+
return fmt.Errorf("invalid issuer URL: %w", err)
+
// Validate scope if present (lenient: allow empty, but reject wrong scopes)
+
if claims.Scope != "" && !strings.Contains(claims.Scope, "atproto") {
+
return fmt.Errorf("token missing required 'atproto' scope, got: %s", claims.Scope)
+
// JWKSFetcher defines the interface for fetching public keys from JWKS endpoints
+
// Returns interface{} to support both RSA and ECDSA keys
+
type JWKSFetcher interface {
+
FetchPublicKey(ctx context.Context, issuer, token string) (interface{}, error)
+
// JWK represents a JSON Web Key from a JWKS endpoint
+
// Supports both RSA and EC (ECDSA) keys
+
Kid string `json:"kid"` // Key ID
+
Kty string `json:"kty"` // Key type ("RSA" or "EC")
+
Alg string `json:"alg"` // Algorithm (e.g., "RS256", "ES256")
+
Use string `json:"use"` // Public key use (should be "sig" for signatures)
+
N string `json:"n,omitempty"` // RSA modulus
+
E string `json:"e,omitempty"` // RSA exponent
+
Crv string `json:"crv,omitempty"` // EC curve (e.g., "P-256")
+
X string `json:"x,omitempty"` // EC x coordinate
+
Y string `json:"y,omitempty"` // EC y coordinate
+
// ToPublicKey converts a JWK to a public key (RSA or ECDSA)
+
func (j *JWK) ToPublicKey() (interface{}, error) {
+
return j.toRSAPublicKey()
+
return j.toECPublicKey()
+
return nil, fmt.Errorf("unsupported key type: %s", j.Kty)
+
// toRSAPublicKey converts a JWK to an RSA public key
+
func (j *JWK) toRSAPublicKey() (*rsa.PublicKey, error) {
+
nBytes, err := base64.RawURLEncoding.DecodeString(j.N)
+
return nil, fmt.Errorf("failed to decode RSA modulus: %w", err)
+
eBytes, err := base64.RawURLEncoding.DecodeString(j.E)
+
return nil, fmt.Errorf("failed to decode RSA exponent: %w", err)
+
// Convert exponent to int
+
for _, b := range eBytes {
+
eInt = eInt*256 + int(b)
+
N: new(big.Int).SetBytes(nBytes),
+
// toECPublicKey converts a JWK to an ECDSA public key
+
func (j *JWK) toECPublicKey() (*ecdsa.PublicKey, error) {
+
var curve elliptic.Curve
+
curve = elliptic.P256()
+
curve = elliptic.P384()
+
curve = elliptic.P521()
+
return nil, fmt.Errorf("unsupported EC curve: %s", j.Crv)
+
xBytes, err := base64.RawURLEncoding.DecodeString(j.X)
+
return nil, fmt.Errorf("failed to decode EC x coordinate: %w", err)
+
yBytes, err := base64.RawURLEncoding.DecodeString(j.Y)
+
return nil, fmt.Errorf("failed to decode EC y coordinate: %w", err)
+
return &ecdsa.PublicKey{
+
X: new(big.Int).SetBytes(xBytes),
+
Y: new(big.Int).SetBytes(yBytes),
+
// JWKS represents a JSON Web Key Set
+
Keys []JWK `json:"keys"`
+
// FindKeyByID finds a key in the JWKS by its key ID
+
func (j *JWKS) FindKeyByID(kid string) (*JWK, error) {
+
for _, key := range j.Keys {
+
return nil, fmt.Errorf("key with kid %s not found", kid)
+
// ExtractKeyID extracts the key ID from a JWT token header
+
func ExtractKeyID(tokenString string) (string, error) {
+
tokenString = strings.TrimPrefix(tokenString, "Bearer ")
+
parts := strings.Split(tokenString, ".")
+
return "", fmt.Errorf("invalid JWT format")
+
headerBytes, err := base64.RawURLEncoding.DecodeString(parts[0])
+
return "", fmt.Errorf("failed to decode header: %w", err)
+
Kid string `json:"kid"`
+
if err := json.Unmarshal(headerBytes, &header); err != nil {
+
return "", fmt.Errorf("failed to unmarshal header: %w", err)
+
return "", fmt.Errorf("missing kid in token header")