Mojo-ATProto-OAuth

 view release on metacpan or  search on metacpan

lib/Mojo/ATProto/OAuth/SessionStore/Pg.pm  view on Meta::CPAN

    die "no session found for did/session_id\n" unless $rows;
    return;
}

sub update_session_p($self, $account_did, $session_id, $fields) {
    my $set = $self->_validated_session_update($fields);
    return $self->pg->db->update_p('sessions', $set, {account_did => $account_did, session_id => $session_id})->then(sub($results) {
        die "no session found for did/session_id\n" unless $results->rows;
        return;
    });
}

# Serializes $code per (account_did, session_id) across every process
# sharing this database, using a transaction-scoped advisory lock on a
# dedicated connection. An advisory lock rather than SELECT ... FOR
# UPDATE on the row itself, because $code writes through this store's
# ordinary methods on a *different* pooled connection, which a row lock
# held here would block - deadlocking the caller against itself. The
# lock is released when the transaction ends (commit here, or rollback
# when $tx goes out of scope on an exception, or the connection dying).
# hashtext() collisions between two different sessions only cause
# unnecessary waiting, never a missed lock.
sub lock_session($self, $account_did, $session_id, $code) {
    my $db = $self->pg->db;
    my $tx = $db->begin;
    $db->query('SELECT pg_advisory_xact_lock(hashtext(?), hashtext(?))', $account_did, $session_id);
    my $result = $code->($self->get_session($account_did, $session_id));
    $tx->commit;
    return $result;
}

sub lock_session_p($self, $account_did, $session_id, $code) {
    my $db = $self->pg->db;
    my $tx = $db->begin;
    return $db->query_p('SELECT pg_advisory_xact_lock(hashtext(?), hashtext(?))', $account_did, $session_id)
        ->then(sub { return $self->get_session_p($account_did, $session_id) })
        ->then($code)
        ->finally(sub { $tx->commit });
}

sub _session_upsert_args($self, $session_data) {
    my $row    = $self->_row_from_session($session_data);
    my $update = {%$row};
    delete @{$update}{qw/account_did session_id/};
    return ($row, $update);
}

sub _row_from_auth_request($self, $info) {
    return {
        state                            => $info->{state},
        account_did                      => $info->{account_did},
        handle                           => $info->{handle},
        host_url                         => $info->{host_url},
        auth_server_url                  => $info->{auth_server_url},
        auth_server_token_endpoint       => $info->{auth_server_token_endpoint},
        auth_server_revocation_endpoint  => $info->{auth_server_revocation_endpoint},
        scopes                           => $self->_encode_scopes($info->{scopes}),
        request_uri                      => $info->{request_uri},
        pkce_verifier                    => $info->{pkce_verifier},
        dpop_authserver_nonce            => $info->{dpop_authserver_nonce},
        dpop_private_key_pem             => $info->{dpop_private_key_pem},
        upgrade_session_id               => $info->{upgrade_session_id},
        client_state                     => $self->_encode_json($info->{client_state}),
        extra                            => $self->_encode_json($info->{extra}),
    };
}

# client_state/extra come back already-decoded to Perl values by
# ->expand (called on the results object in get_auth_request(_p) above) -
# a JSONB column decodes to undef on its own when the stored value is
# SQL NULL, so no manual decode step is needed here.
sub _auth_request_from_row($self, $row) {
    return {
        state                            => $row->{state},
        account_did                      => $row->{account_did},
        handle                           => $row->{handle},
        host_url                         => $row->{host_url},
        auth_server_url                  => $row->{auth_server_url},
        auth_server_token_endpoint       => $row->{auth_server_token_endpoint},
        auth_server_revocation_endpoint  => $row->{auth_server_revocation_endpoint},
        scopes                           => $self->_decode_scopes($row->{scopes}),
        request_uri                      => $row->{request_uri},
        pkce_verifier                    => $row->{pkce_verifier},
        dpop_authserver_nonce            => $row->{dpop_authserver_nonce},
        dpop_private_key_pem             => $row->{dpop_private_key_pem},
        upgrade_session_id               => $row->{upgrade_session_id},
        client_state                     => $row->{client_state},
        extra                            => $row->{extra},
    };
}

# client_state/extra deliberately omitted - Mojo::ATProto::OAuth never
# reads them back off a persisted session, only off an auth request (see
# oauth-store-interface memory).
sub _row_from_session($self, $session_data) {
    return {
        account_did                      => $session_data->{account_did},
        session_id                       => $session_data->{session_id},
        handle                           => $session_data->{handle},
        host_url                         => $session_data->{host_url},
        auth_server_url                  => $session_data->{auth_server_url},
        auth_server_token_endpoint       => $session_data->{auth_server_token_endpoint},
        auth_server_revocation_endpoint  => $session_data->{auth_server_revocation_endpoint},
        scopes                           => $self->_encode_scopes($session_data->{scopes}),
        access_token                     => $session_data->{access_token},
        refresh_token                    => $session_data->{refresh_token},
        dpop_authserver_nonce            => $session_data->{dpop_authserver_nonce},
        dpop_host_nonce                  => $session_data->{dpop_host_nonce},
        dpop_private_key_pem             => $session_data->{dpop_private_key_pem},
    };
}

sub _session_from_row($self, $row) {
    return {
        account_did                      => $row->{account_did},
        session_id                       => $row->{session_id},
        handle                           => $row->{handle},
        host_url                         => $row->{host_url},
        auth_server_url                  => $row->{auth_server_url},
        auth_server_token_endpoint       => $row->{auth_server_token_endpoint},
        auth_server_revocation_endpoint  => $row->{auth_server_revocation_endpoint},
        scopes                           => $self->_decode_scopes($row->{scopes}),
        access_token                     => $row->{access_token},
        refresh_token                    => $row->{refresh_token},
        dpop_authserver_nonce            => $row->{dpop_authserver_nonce},
        dpop_host_nonce                  => $row->{dpop_host_nonce},
        dpop_private_key_pem             => $row->{dpop_private_key_pem},
    };
}

sub _encode_scopes($self, $scopes) {
    return join(' ', @{$scopes // []});
}

sub _decode_scopes($self, $text) {
    return [split(/ /, $text // '')];
}

# Wraps a defined value for Mojo::Pg's -json bind-type handling, so it's
# stored as real JSONB rather than a plain TEXT column. undef stays plain
# undef (real SQL NULL), not JSON's 'null' text.
sub _encode_json($self, $value) {
    return defined($value) ? {-json => $value} : undef;
}

1;

=head1 NAME

Mojo::ATProto::OAuth::SessionStore::Pg - Postgres-backed session store for Mojo::ATProto::OAuth

=head1 SYNOPSIS

    my $oauth = Mojo::ATProto::OAuth->new(..., store => [Pg => 'postgresql://user:pass@host/dbname']);

=head1 DESCRIPTION

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.

=head2 Session locking

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 e...

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...

=head1 ATTRIBUTES

=head2 pg

The underlying L<Mojo::Pg> instance.

=head1 SEE ALSO

L<Mojo::ATProto::OAuth>, L<Mojo::ATProto::OAuth::SessionStore::SQLite>

=cut

__DATA__
@@ sessionstore

-- 1 up
CREATE TABLE auth_requests (
    state                            TEXT PRIMARY KEY,
    account_did                      TEXT,
    handle                           TEXT,
    host_url                         TEXT,
    auth_server_url                  TEXT NOT NULL,
    auth_server_token_endpoint       TEXT NOT NULL,
    auth_server_revocation_endpoint  TEXT,
    scopes                           TEXT NOT NULL,
    request_uri                      TEXT NOT NULL,
    pkce_verifier                    TEXT NOT NULL,
    dpop_authserver_nonce            TEXT,
    dpop_private_key_pem             TEXT NOT NULL,
    upgrade_session_id               TEXT,
    client_state                     JSONB,
    extra                            JSONB
);

CREATE TABLE sessions (
    account_did                      TEXT NOT NULL,
    session_id                       TEXT NOT NULL,
    handle                           TEXT,
    host_url                         TEXT,
    auth_server_url                  TEXT NOT NULL,
    auth_server_token_endpoint       TEXT NOT NULL,
    auth_server_revocation_endpoint  TEXT,
    scopes                           TEXT NOT NULL,
    access_token                     TEXT NOT NULL,
    refresh_token                    TEXT,
    dpop_authserver_nonce            TEXT,
    dpop_host_nonce                  TEXT,
    dpop_private_key_pem             TEXT NOT NULL,
    PRIMARY KEY (account_did, session_id)
);

-- 1 down
DROP TABLE sessions;
DROP TABLE auth_requests;



( run in 2.009 seconds using v1.01-cache-2.11-cpan-8379b9b8ec2 )