| 1 | package | ||||||||||||||||||||
| 2 | Mojo::ATProto::OAuth::DPoP; | ||||||||||||||||||||
| 3 | 9 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 9 | ▶ | use Crypt::PK::ECC; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 9 | ▶ | use Crypt::URandom qw/urandom/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 9 | ▶ | use Digest::SHA qw/sha256/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 9 | ▶ | use MIME::Base64 qw/encode_base64url/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 9 | ▶ | use Mojo::JSON qw/encode_json decode_json true false/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 9 | ▶ | use Mojo::Log qw//; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # DPoP (RFC 9449) proof JWTs and OAuth client-assertion JWTs for ATProto | ||||||||||||||||||||
| 13 | # OAuth - a fresh P-256 keypair per session, a signed sender-constrained | ||||||||||||||||||||
| 14 | # proof on every PAR/token/resource-server call. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 9 | ▶ | use constant JWT_TTL => 30; # seconds | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 9 | ▶ | use constant DEBUG => $ENV{MOJO_OAUTH_DEBUG} || 0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'MOJO_OAUTH_DEBUG'} || 0
MC/DC0%0/1
$ENV{'MOJO_OAUTH_DEBUG'} || 0
$ENV{'MOJO_OAUTH_DEBUG'}
Truth table50%1/2
A
A= $ENV{'MOJO_OAUTH_DEBUG'}
| |||||||||||||||||||||
| 19 | 9 | my $LOG = Mojo::Log->new; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # Generates a fresh P-256 keypair for a new OAuth session/DPoP proof | ||||||||||||||||||||
| 22 | # chain. Returns a Crypt::PK::ECC object (holds both private and public | ||||||||||||||||||||
| 23 | # key material). | ||||||||||||||||||||
| 24 | 45 | ▶ | sub generate_keypair($class) { | ||||||||||||||||||
|
Subroutine
generate_keypair: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 25 | 45 | my $pk = Crypt::PK::ECC->new; | |||||||||||||||||||
| 26 | 45 | $pk->generate_key('secp256r1'); | |||||||||||||||||||
| 27 | 45 | $LOG->debug('DPoP: generated a fresh P-256 keypair') if DEBUG; | |||||||||||||||||||
| 28 | 45 | return $pk; | |||||||||||||||||||
| 29 | } | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 39 | ▶ | sub export_private_pem($class, $pk) { | ||||||||||||||||||
|
Subroutine
export_private_pem: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 32 | 39 | return $pk->export_key_pem('private'); | |||||||||||||||||||
| 33 | } | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 38 | ▶ | sub import_private_pem($class, $pem) { | ||||||||||||||||||
|
Subroutine
import_private_pem: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 36 | 38 | return Crypt::PK::ECC->new(\$pem); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 326 | ▶ | sub _b64url($data) { | ||||||||||||||||||
|
Subroutine
_b64url: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 40 | 326 | return encode_base64url($data, ''); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 61 | ▶ | sub _jwk_public($pk) { | ||||||||||||||||||
|
Subroutine
_jwk_public: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 44 | 61 | return decode_json($pk->export_key_jwk('public')); | |||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # A cryptographically random base64url token - used for PKCE verifiers, | ||||||||||||||||||||
| 48 | # the OAuth `state` parameter, and JWT `jti` values. | ||||||||||||||||||||
| 49 | 99 | ▶ | sub secure_random_base64 ($class, $nbytes = 32) { | ||||||||||||||||||
|
Subroutine
secure_random_base64: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 50 | 99 | return _b64url(urandom($nbytes)); | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | # SHA-256 + base64url, as used for PKCE code challenges (S256) and for | ||||||||||||||||||||
| 54 | # the DPoP 'ath' (access token hash) claim. | ||||||||||||||||||||
| 55 | 38 | ▶ | sub s256_challenge($class, $raw) { | ||||||||||||||||||
|
Subroutine
s256_challenge: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 56 | 38 | return _b64url(sha256($raw)); | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # Builds and signs an ES256 JWT from an arbitrary header/claims pair - | ||||||||||||||||||||
| 60 | # the shared envelope logic under both proof() and client_assertion(). | ||||||||||||||||||||
| 61 | 63 | ▶ | sub sign_jwt($class, $pk, $header, $claims) { | ||||||||||||||||||
|
Subroutine
sign_jwt: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 62 | 63 | my $encoded_header = _b64url(encode_json($header)); | |||||||||||||||||||
| 63 | 63 | my $encoded_payload = _b64url(encode_json($claims)); | |||||||||||||||||||
| 64 | 63 | my $signing_input = "$encoded_header.$encoded_payload"; | |||||||||||||||||||
| 65 | 63 | my $sig = $pk->sign_message_rfc7518($signing_input, 'SHA256'); | |||||||||||||||||||
| 66 | 63 | return "$signing_input." . _b64url($sig); | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | # Builds a DPoP proof JWT. Used for PAR, initial token request, and | ||||||||||||||||||||
| 70 | # refresh requests (all against the Auth Server, no 'ath'/'iss' claim), | ||||||||||||||||||||
| 71 | # and for resource-server (PDS) requests once a session exists | ||||||||||||||||||||
| 72 | # | ||||||||||||||||||||
| 73 | # %args: key (required, a Crypt::PK::ECC private key), method (required), | ||||||||||||||||||||
| 74 | # url (required), nonce (optional server-issued DPoP-Nonce), access_token | ||||||||||||||||||||
| 75 | # (optional, resource-server calls only), issuer (optional, resource- | ||||||||||||||||||||
| 76 | # server calls only - the auth server URL, per indigo's NewHostDPoP). | ||||||||||||||||||||
| 77 | 61 | ▶ | sub proof($class, %args) { | ||||||||||||||||||
|
Subroutine
proof: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||
| 78 | 61 | ▶ | my $pk = $args{key} // die "proof: 'key' required\n"; | ||||||||||||||||||
|
Condition50%1/2
$args{"key"} // die("proof: 'key' required\n")
MC/DC0%0/1
$args{"key"} // die("proof: 'key' required\n")
$args{"key"}
Truth table50%1/2
A
A= $args{"key"}
| |||||||||||||||||||||
| 79 | 61 | ▶ | my $method = $args{method} // die "proof: 'method' required\n"; | ||||||||||||||||||
|
Condition50%1/2
$args{"method"} // die("proof: 'method' required\n")
MC/DC0%0/1
$args{"method"} // die("proof: 'method' required\n")
$args{"method"}
Truth table50%1/2
A
A= $args{"method"}
| |||||||||||||||||||||
| 80 | 61 | ▶ | my $url = $args{url} // die "proof: 'url' required\n"; | ||||||||||||||||||
|
Condition50%1/2
$args{"url"} // die("proof: 'url' required\n")
MC/DC0%0/1
$args{"url"} // die("proof: 'url' required\n")
$args{"url"}
Truth table50%1/2
A
A= $args{"url"}
| |||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 61 | my $claims = { | |||||||||||||||||||
| 83 | htm => uc($method), | ||||||||||||||||||||
| 84 | htu => $url, | ||||||||||||||||||||
| 85 | jti => $class->secure_random_base64(16), | ||||||||||||||||||||
| 86 | iat => time, | ||||||||||||||||||||
| 87 | exp => time + JWT_TTL, | ||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||
| 89 | 61 | ▶ | $claims->{nonce} = $args{nonce} if length($args{nonce} // ''); | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args{"nonce"} // ""
MC/DC100%1/1
$args{"nonce"} // ""
$args{"nonce"}
Truth table100%2/2
A
A= $args{"nonce"}
| |||||||||||||||||||||
| 90 | 61 | ▶ | $claims->{ath} = $class->s256_challenge($args{access_token}) if length($args{access_token} // ''); | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args{"access_token"} // ""
MC/DC100%1/1
$args{"access_token"} // ""
$args{"access_token"}
Truth table100%2/2
A
A= $args{"access_token"}
| |||||||||||||||||||||
| 91 | 61 | ▶ | $claims->{iss} = $args{issuer} if length($args{issuer} // ''); | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args{"issuer"} // ""
MC/DC100%1/1
$args{"issuer"} // ""
$args{"issuer"}
Truth table100%2/2
A
A= $args{"issuer"}
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 61 | my $header = { | |||||||||||||||||||
| 94 | typ => 'dpop+jwt', | ||||||||||||||||||||
| 95 | alg => 'ES256', | ||||||||||||||||||||
| 96 | jwk => _jwk_public($pk), | ||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | # Metadata only, never the signed JWT itself - it's presented over | ||||||||||||||||||||
| 100 | # the wire regardless, but there's no reason to also let it pile up | ||||||||||||||||||||
| 101 | # in logs. | ||||||||||||||||||||
| 102 | $LOG->debug(sprintf('DPoP: proof for %s %s (nonce=%s, resource-server=%s)', | ||||||||||||||||||||
| 103 | 61 | uc($method), $url, (length($args{nonce} // '') ? 'yes' : 'no'), (length($args{issuer} // '') ? 'yes' : 'no'))) if DEBUG; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 61 | return $class->sign_jwt($pk, $header, $claims); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | # Builds a confidential-client assertion JWT (RFC 7523), for the | ||||||||||||||||||||
| 109 | # `client_assertion` form parameter on PAR/token/refresh/revocation | ||||||||||||||||||||
| 110 | # requests. Not used for public clients (no key configured). | ||||||||||||||||||||
| 111 | # | ||||||||||||||||||||
| 112 | # %args: key (required, a Crypt::PK::ECC private key), key_id (required), | ||||||||||||||||||||
| 113 | # client_id (required), audience (required - the auth server's issuer | ||||||||||||||||||||
| 114 | # URL). | ||||||||||||||||||||
| 115 | 2 | ▶ | sub client_assertion($class, %args) { | ||||||||||||||||||
|
Subroutine
client_assertion: called CC=5 SCAR=16.8
Pod
undocumented
| |||||||||||||||||||||
| 116 | 2 | ▶ | my $pk = $args{key} // die "client_assertion: 'key' required\n"; | ||||||||||||||||||
|
Condition50%1/2
$args{"key"} // die("client_assertion: 'key' required\n")
MC/DC0%0/1
$args{"key"} // die("client_assertion: 'key' required\n")
$args{"key"}
Truth table50%1/2
A
A= $args{"key"}
| |||||||||||||||||||||
| 117 | 2 | ▶ | my $key_id = $args{key_id} // die "client_assertion: 'key_id' required\n"; | ||||||||||||||||||
|
Condition50%1/2
$args{"key_id"} // die("client_assertion: 'key_id' required\n")
MC/DC0%0/1
$args{"key_id"} // die("client_assertion: 'key_id' required\n")
$args{"key_id"}
Truth table50%1/2
A
A= $args{"key_id"}
| |||||||||||||||||||||
| 118 | 2 | ▶ | my $client_id = $args{client_id} // die "client_assertion: 'client_id' required\n"; | ||||||||||||||||||
|
Condition50%1/2
$args{"client_id"} // die("client_assertion: 'client_id' required\n")
MC/DC0%0/1
$args{"client_id"} // die("client_assertion: 'client_id' required\n")
$args{"client_id"}
Truth table50%1/2
A
A= $args{"client_id"}
| |||||||||||||||||||||
| 119 | 2 | ▶ | my $audience = $args{audience} // die "client_assertion: 'audience' required\n"; | ||||||||||||||||||
|
Condition50%1/2
$args{"audience"} // die("client_assertion: 'audience' required\n")
MC/DC0%0/1
$args{"audience"} // die("client_assertion: 'audience' required\n")
$args{"audience"}
Truth table50%1/2
A
A= $args{"audience"}
| |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 2 | my $claims = { | |||||||||||||||||||
| 122 | iss => $client_id, | ||||||||||||||||||||
| 123 | sub => $client_id, | ||||||||||||||||||||
| 124 | aud => $audience, | ||||||||||||||||||||
| 125 | jti => $class->secure_random_base64(16), | ||||||||||||||||||||
| 126 | iat => time, | ||||||||||||||||||||
| 127 | exp => time + JWT_TTL, | ||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||
| 129 | 2 | my $header = {typ => 'JWT', alg => 'ES256', kid => $key_id}; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 2 | $LOG->debug("DPoP: client_assertion for client_id=$client_id audience=$audience key_id=$key_id") if DEBUG; | |||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 2 | return $class->sign_jwt($pk, $header, $claims); | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 9 | 1; | |||||||||||||||||||