| 1 | package | ||||||||||
| 2 | Mojo::ATProto::OAuth::SessionStore::Pg; | ||||||||||
| 3 | 1 | ▶ | use Mojo::Base 'Mojo::ATProto::OAuth::SessionStore', -signatures; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use feature 'try'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Mojo::Loader qw/load_class/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Mojo::JSON qw/encode_json decode_json/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | # Mojo::Pg is an optional prerequisite of this distribution (see | ||||||||||
| 9 | # dist.ini's RuntimeRecommends) - only consumers who actually load this | ||||||||||
| 10 | # class need it installed. Fail loudly and specifically here at compile | ||||||||||
| 11 | # time, rather than with Perl's own bare "Can't locate Mojo/Pg.pm" once | ||||||||||
| 12 | # something below tries to call it. | ||||||||||
| 13 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.4
| |||||||||||
| 14 | 1 | my $e = load_class('Mojo::Pg'); | |||||||||
| 15 | 1 | ▶ | if ($e) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 1 | ▶ | my $reason = ref($e) ? "$e" : 'module not found in @INC'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 1 | die "Mojo::ATProto::OAuth::SessionStore::Pg requires the optional 'Mojo::Pg' module, which is not installed ($reason).\n" | |||||||||
| 18 | . "Install it separately, e.g.: cpanm Mojo::Pg\n"; | ||||||||||
| 19 | } | ||||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | has 'pg' => undef; | ||||||||||
| 23 | |||||||||||
| 24 | sub new { | ||||||||||
| 25 | my $self = shift->SUPER::new(pg => Mojo::Pg->new(@_)); | ||||||||||
| 26 | $self->pg->auto_migrate(1)->migrations->name('sessionstore')->from_data; | ||||||||||
| 27 | return $self; | ||||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | #-- add after this line | ||||||||||
| 31 | |||||||||||
| 32 | sub get_auth_request($self, $state) { | ||||||||||
| 33 | my $row = $self->pg->db->select('auth_requests', undef, {state => $state})->expand->hash; | ||||||||||
| 34 | die "no auth request found for state\n" unless defined $row; | ||||||||||
| 35 | return $self->_auth_request_from_row($row); | ||||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | sub get_auth_request_p($self, $state) { | ||||||||||
| 39 | return $self->pg->db->select_p('auth_requests', undef, {state => $state})->then(sub($results) { | ||||||||||
| 40 | my $row = $results->expand->hash; | ||||||||||
| 41 | die "no auth request found for state\n" unless defined $row; | ||||||||||
| 42 | return $self->_auth_request_from_row($row); | ||||||||||
| 43 | }); | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | sub save_auth_request($self, $info) { | ||||||||||
| 47 | $self->pg->db->insert('auth_requests', $self->_row_from_auth_request($info)); | ||||||||||
| 48 | return; | ||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | sub save_auth_request_p($self, $info) { | ||||||||||
| 52 | return $self->pg->db->insert_p('auth_requests', $self->_row_from_auth_request($info))->then(sub { return }); | ||||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | sub delete_auth_request($self, $state) { | ||||||||||
| 56 | $self->pg->db->delete('auth_requests', {state => $state}); | ||||||||||
| 57 | return; | ||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | sub delete_auth_request_p($self, $state) { | ||||||||||
| 61 | return $self->pg->db->delete_p('auth_requests', {state => $state})->then(sub { return }); | ||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | sub get_session($self, $account_did, $session_id) { | ||||||||||
| 65 | my $row = $self->pg->db->select('sessions', undef, {account_did => $account_did, session_id => $session_id})->hash; | ||||||||||
| 66 | die "no session found for did/session_id\n" unless defined $row; | ||||||||||
| 67 | return $self->_session_from_row($row); | ||||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | sub get_session_p($self, $account_did, $session_id) { | ||||||||||
| 71 | return $self->pg->db->select_p('sessions', undef, {account_did => $account_did, session_id => $session_id})->then(sub($results) { | ||||||||||
| 72 | my $row = $results->hash; | ||||||||||
| 73 | die "no session found for did/session_id\n" unless defined $row; | ||||||||||
| 74 | return $self->_session_from_row($row); | ||||||||||
| 75 | }); | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | # save_session must be an upsert keyed on (account_did, session_id) - both | ||||||||||
| 79 | # an ordinary login and a scope-upgrade callback may call this on what's | ||||||||||
| 80 | # already an existing row. Postgres's native ON CONFLICT DO UPDATE handles | ||||||||||
| 81 | # this in one statement. | ||||||||||
| 82 | sub save_session($self, $session_data) { | ||||||||||
| 83 | my ($row, $update) = $self->_session_upsert_args($session_data); | ||||||||||
| 84 | $self->pg->db->insert('sessions', $row, {on_conflict => [['account_did', 'session_id'] => $update]}); | ||||||||||
| 85 | return; | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | sub save_session_p($self, $session_data) { | ||||||||||
| 89 | my ($row, $update) = $self->_session_upsert_args($session_data); | ||||||||||
| 90 | return $self->pg->db->insert_p('sessions', $row, {on_conflict => [['account_did', 'session_id'] => $update]})->then(sub { return }); | ||||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | sub delete_session($self, $account_did, $session_id) { | ||||||||||
| 94 | $self->pg->db->delete('sessions', {account_did => $account_did, session_id => $session_id}); | ||||||||||
| 95 | return; | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | sub delete_session_p($self, $account_did, $session_id) { | ||||||||||
| 99 | return $self->pg->db->delete_p('sessions', {account_did => $account_did, session_id => $session_id})->then(sub { return }); | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | # Updates only the given fields of an existing session - a caller that | ||||||||||
| 103 | # only learned a new DPoP nonce must not write back the tokens it read | ||||||||||
| 104 | # earlier, which another process may have rotated since. | ||||||||||
| 105 | sub update_session($self, $account_did, $session_id, $fields) { | ||||||||||
| 106 | my $set = $self->_validated_session_update($fields); | ||||||||||
| 107 | my $rows = $self->pg->db->update('sessions', $set, {account_did => $account_did, session_id => $session_id})->rows; | ||||||||||
| 108 | die "no session found for did/session_id\n" unless $rows; | ||||||||||
| 109 | return; | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | sub update_session_p($self, $account_did, $session_id, $fields) { | ||||||||||
| 113 | my $set = $self->_validated_session_update($fields); | ||||||||||
| 114 | return $self->pg->db->update_p('sessions', $set, {account_did => $account_did, session_id => $session_id})->then(sub($results) { | ||||||||||
| 115 | die "no session found for did/session_id\n" unless $results->rows; | ||||||||||
| 116 | return; | ||||||||||
| 117 | }); | ||||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | # Serializes $code per (account_did, session_id) across every process | ||||||||||
| 121 | # sharing this database, using a transaction-scoped advisory lock on a | ||||||||||
| 122 | # dedicated connection. An advisory lock rather than SELECT ... FOR | ||||||||||
| 123 | # UPDATE on the row itself, because $code writes through this store's | ||||||||||
| 124 | # ordinary methods on a *different* pooled connection, which a row lock | ||||||||||
| 125 | # held here would block - deadlocking the caller against itself. The | ||||||||||
| 126 | # lock is released when the transaction ends (commit here, or rollback | ||||||||||
| 127 | # when $tx goes out of scope on an exception, or the connection dying). | ||||||||||
| 128 | # hashtext() collisions between two different sessions only cause | ||||||||||
| 129 | # unnecessary waiting, never a missed lock. | ||||||||||
| 130 | sub lock_session($self, $account_did, $session_id, $code) { | ||||||||||
| 131 | my $db = $self->pg->db; | ||||||||||
| 132 | my $tx = $db->begin; | ||||||||||
| 133 | $db->query('SELECT pg_advisory_xact_lock(hashtext(?), hashtext(?))', $account_did, $session_id); | ||||||||||
| 134 | my $result = $code->($self->get_session($account_did, $session_id)); | ||||||||||
| 135 | $tx->commit; | ||||||||||
| 136 | return $result; | ||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | sub lock_session_p($self, $account_did, $session_id, $code) { | ||||||||||
| 140 | my $db = $self->pg->db; | ||||||||||
| 141 | my $tx = $db->begin; | ||||||||||
| 142 | return $db->query_p('SELECT pg_advisory_xact_lock(hashtext(?), hashtext(?))', $account_did, $session_id) | ||||||||||
| 143 | ->then(sub { return $self->get_session_p($account_did, $session_id) }) | ||||||||||
| 144 | ->then($code) | ||||||||||
| 145 | ->finally(sub { $tx->commit }); | ||||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | sub _session_upsert_args($self, $session_data) { | ||||||||||
| 149 | my $row = $self->_row_from_session($session_data); | ||||||||||
| 150 | my $update = {%$row}; | ||||||||||
| 151 | delete @{$update}{qw/account_did session_id/}; | ||||||||||
| 152 | return ($row, $update); | ||||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | sub _row_from_auth_request($self, $info) { | ||||||||||
| 156 | return { | ||||||||||
| 157 | state => $info->{state}, | ||||||||||
| 158 | account_did => $info->{account_did}, | ||||||||||
| 159 | handle => $info->{handle}, | ||||||||||
| 160 | host_url => $info->{host_url}, | ||||||||||
| 161 | auth_server_url => $info->{auth_server_url}, | ||||||||||
| 162 | auth_server_token_endpoint => $info->{auth_server_token_endpoint}, | ||||||||||
| 163 | auth_server_revocation_endpoint => $info->{auth_server_revocation_endpoint}, | ||||||||||
| 164 | scopes => $self->_encode_scopes($info->{scopes}), | ||||||||||
| 165 | request_uri => $info->{request_uri}, | ||||||||||
| 166 | pkce_verifier => $info->{pkce_verifier}, | ||||||||||
| 167 | dpop_authserver_nonce => $info->{dpop_authserver_nonce}, | ||||||||||
| 168 | dpop_private_key_pem => $info->{dpop_private_key_pem}, | ||||||||||
| 169 | upgrade_session_id => $info->{upgrade_session_id}, | ||||||||||
| 170 | client_state => $self->_encode_json($info->{client_state}), | ||||||||||
| 171 | extra => $self->_encode_json($info->{extra}), | ||||||||||
| 172 | }; | ||||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | # client_state/extra come back already-decoded to Perl values by | ||||||||||
| 176 | # ->expand (called on the results object in get_auth_request(_p) above) - | ||||||||||
| 177 | # a JSONB column decodes to undef on its own when the stored value is | ||||||||||
| 178 | # SQL NULL, so no manual decode step is needed here. | ||||||||||
| 179 | sub _auth_request_from_row($self, $row) { | ||||||||||
| 180 | return { | ||||||||||
| 181 | state => $row->{state}, | ||||||||||
| 182 | account_did => $row->{account_did}, | ||||||||||
| 183 | handle => $row->{handle}, | ||||||||||
| 184 | host_url => $row->{host_url}, | ||||||||||
| 185 | auth_server_url => $row->{auth_server_url}, | ||||||||||
| 186 | auth_server_token_endpoint => $row->{auth_server_token_endpoint}, | ||||||||||
| 187 | auth_server_revocation_endpoint => $row->{auth_server_revocation_endpoint}, | ||||||||||
| 188 | scopes => $self->_decode_scopes($row->{scopes}), | ||||||||||
| 189 | request_uri => $row->{request_uri}, | ||||||||||
| 190 | pkce_verifier => $row->{pkce_verifier}, | ||||||||||
| 191 | dpop_authserver_nonce => $row->{dpop_authserver_nonce}, | ||||||||||
| 192 | dpop_private_key_pem => $row->{dpop_private_key_pem}, | ||||||||||
| 193 | upgrade_session_id => $row->{upgrade_session_id}, | ||||||||||
| 194 | client_state => $row->{client_state}, | ||||||||||
| 195 | extra => $row->{extra}, | ||||||||||
| 196 | }; | ||||||||||
| 197 | } | ||||||||||
| 198 | |||||||||||
| 199 | # client_state/extra deliberately omitted - Mojo::ATProto::OAuth never | ||||||||||
| 200 | # reads them back off a persisted session, only off an auth request (see | ||||||||||
| 201 | # oauth-store-interface memory). | ||||||||||
| 202 | sub _row_from_session($self, $session_data) { | ||||||||||
| 203 | return { | ||||||||||
| 204 | account_did => $session_data->{account_did}, | ||||||||||
| 205 | session_id => $session_data->{session_id}, | ||||||||||
| 206 | handle => $session_data->{handle}, | ||||||||||
| 207 | host_url => $session_data->{host_url}, | ||||||||||
| 208 | auth_server_url => $session_data->{auth_server_url}, | ||||||||||
| 209 | auth_server_token_endpoint => $session_data->{auth_server_token_endpoint}, | ||||||||||
| 210 | auth_server_revocation_endpoint => $session_data->{auth_server_revocation_endpoint}, | ||||||||||
| 211 | scopes => $self->_encode_scopes($session_data->{scopes}), | ||||||||||
| 212 | access_token => $session_data->{access_token}, | ||||||||||
| 213 | refresh_token => $session_data->{refresh_token}, | ||||||||||
| 214 | dpop_authserver_nonce => $session_data->{dpop_authserver_nonce}, | ||||||||||
| 215 | dpop_host_nonce => $session_data->{dpop_host_nonce}, | ||||||||||
| 216 | dpop_private_key_pem => $session_data->{dpop_private_key_pem}, | ||||||||||
| 217 | }; | ||||||||||
| 218 | } | ||||||||||
| 219 | |||||||||||
| 220 | sub _session_from_row($self, $row) { | ||||||||||
| 221 | return { | ||||||||||
| 222 | account_did => $row->{account_did}, | ||||||||||
| 223 | session_id => $row->{session_id}, | ||||||||||
| 224 | handle => $row->{handle}, | ||||||||||
| 225 | host_url => $row->{host_url}, | ||||||||||
| 226 | auth_server_url => $row->{auth_server_url}, | ||||||||||
| 227 | auth_server_token_endpoint => $row->{auth_server_token_endpoint}, | ||||||||||
| 228 | auth_server_revocation_endpoint => $row->{auth_server_revocation_endpoint}, | ||||||||||
| 229 | scopes => $self->_decode_scopes($row->{scopes}), | ||||||||||
| 230 | access_token => $row->{access_token}, | ||||||||||
| 231 | refresh_token => $row->{refresh_token}, | ||||||||||
| 232 | dpop_authserver_nonce => $row->{dpop_authserver_nonce}, | ||||||||||
| 233 | dpop_host_nonce => $row->{dpop_host_nonce}, | ||||||||||
| 234 | dpop_private_key_pem => $row->{dpop_private_key_pem}, | ||||||||||
| 235 | }; | ||||||||||
| 236 | } | ||||||||||
| 237 | |||||||||||
| 238 | sub _encode_scopes($self, $scopes) { | ||||||||||
| 239 | return join(' ', @{$scopes // []}); | ||||||||||
| 240 | } | ||||||||||
| 241 | |||||||||||
| 242 | sub _decode_scopes($self, $text) { | ||||||||||
| 243 | return [split(/ /, $text // '')]; | ||||||||||
| 244 | } | ||||||||||
| 245 | |||||||||||
| 246 | # Wraps a defined value for Mojo::Pg's -json bind-type handling, so it's | ||||||||||
| 247 | # stored as real JSONB rather than a plain TEXT column. undef stays plain | ||||||||||
| 248 | # undef (real SQL NULL), not JSON's 'null' text. | ||||||||||
| 249 | sub _encode_json($self, $value) { | ||||||||||
| 250 | return defined($value) ? {-json => $value} : undef; | ||||||||||
| 251 | } | ||||||||||
| 252 | |||||||||||
| 253 | 1; | ||||||||||
| 254 | |||||||||||
| 255 | =head1 NAME | ||||||||||
| 256 | |||||||||||
| 257 | Mojo::ATProto::OAuth::SessionStore::Pg - Postgres-backed session store for Mojo::ATProto::OAuth | ||||||||||
| 258 | |||||||||||
| 259 | =head1 SYNOPSIS | ||||||||||
| 260 | |||||||||||
| 261 | my $oauth = Mojo::ATProto::OAuth->new(..., store => [Pg => 'postgresql://user:pass@host/dbname']); | ||||||||||
| 262 | |||||||||||
| 263 | =head1 DESCRIPTION | ||||||||||
| 264 | |||||||||||
| 265 | Implements the full store interface described in L<Mojo::ATProto::OAuth/THE STORE INTERFACE> on top of L<Mojo::Pg>. Constructor arguments are passed straight to C<< Mojo::Pg->new >>; the schema is created and migrated automatically. | ||||||||||
| 266 | |||||||||||
| 267 | =head2 Session locking | ||||||||||
| 268 | |||||||||||
| 269 | C<lock_session>/C<lock_session_p> (which L<Mojo::ATProto::OAuth/refresh_tokens> runs under) take a transaction-scoped advisory lock (C<pg_advisory_xact_lock>) keyed on the session's C<(account_did, session_id)>, on a dedicated connection. That lock excludes every other process using the same database, waits without polling (the async form doesn't block the event loop while it waits), and is released when the code finishes, fails, or the connection drops. It doesn't lock the C<sessions> row itself, so ordinary reads and writes of the session carry on while it's held. | ||||||||||
| 270 | |||||||||||
| 271 | The locking connection sits idle in a transaction while the lock is held (for the length of one auth-server round trip), so a server-side C<idle_in_transaction_session_timeout> shorter than that will abort the refresh, and each concurrently held lock uses one connection from the pool. | ||||||||||
| 272 | |||||||||||
| 273 | =head1 ATTRIBUTES | ||||||||||
| 274 | |||||||||||
| 275 | =head2 pg | ||||||||||
| 276 | |||||||||||
| 277 | The underlying L<Mojo::Pg> instance. | ||||||||||
| 278 | |||||||||||
| 279 | =head1 SEE ALSO | ||||||||||
| 280 | |||||||||||
| 281 | L<Mojo::ATProto::OAuth>, L<Mojo::ATProto::OAuth::SessionStore::SQLite> | ||||||||||
| 282 | |||||||||||
| 283 | =cut | ||||||||||
| 284 | |||||||||||
| 285 | __DATA__ | ||||||||||