Mojo-UserAgent-CookieJar-ChromeMacOS

 view release on metacpan or  search on metacpan

lib/Mojo/UserAgent/CookieJar/ChromeMacOS.pm  view on Meta::CPAN

    }

    return $plaintext;
}

sub _db_version {
    my( $self ) = @_;

    state $version;
    return $version if defined $version;

    my $dbh = $self->__get_dbh;

    my $has_meta = eval {
        my $sql = q(SELECT 1 FROM sqlite_master WHERE type='table' AND name='meta');
        my $array = $dbh->selectall_arrayref( $sql );
        @$array > 0;
    };
    if( $@ ) { warn $@ }

    return ($version = 0) unless $has_meta;

    $version = eval {
        my $sql = q(SELECT * FROM meta WHERE key = 'version');
        my $rv = $dbh->selectall_arrayref( $sql );
        @$rv ? $rv->[0][1] : 0;
    } // 0;
    if( $@ ) { warn $@ }

    return $version;
}

sub __get_dbh {
    my ($self) = @_;

    state $dbh;
    return $dbh if $dbh && $dbh->ping;

    # copy to read
    my ($fh, $filename) = tempfile();
    close $fh;
    File::Copy::copy($self->file, $filename);
    my $sqlite_file = -e $filename ? $filename : $self->file; # make sure copy works
    # warn "READ $sqlite_file\n";

    $dbh = DBI->connect( "dbi:SQLite:dbname=" . $sqlite_file, '', '', {
      sqlite_see_if_its_a_number => 1,
    } );

    return $dbh;
}

sub _get_pass {
    my ($self) = @_;

    return $self->pass if $self->pass; # for Linux which passed in ->new

    my $pass;
    if ($^O eq 'linux') {
        # # secret-tool search application chrome
        # [/org/freedesktop/secrets/collection/Default_5fkeyring/1]
        # label = Chrome Safe Storage
        # secret = 5B9eGeijTg1xQTh+K70Czg==
        # created = 2022-02-18 02:23:25
        # modified = 2022-02-18 02:23:25
        # schema = chrome_libsecret_os_crypt_password_v2
        # attribute.application = chrome
        my $text = `secret-tool search application chrome`;
        ($pass) = ($text =~ /secret\s*\=\s*(\S+)/m);
    } else {
        $pass = `security find-generic-password -w -s "Chrome Safe Storage"`;
        chomp( $pass );
    }

    $self->pass($pass);
    return $pass;
}

# copied from Mojo::UserAgent::CookieJar
sub _path { $_[0] eq '/' || $_[0] eq $_[1] || index($_[1], "$_[0]/") == 0 }

1;
__END__

=encoding utf-8

=head1 NAME

Mojo::UserAgent::CookieJar::ChromeMacOS - readonly Chrome(MacOSx) cookies for Mojo::UserAgent

=head1 SYNOPSIS

    use Mojo::UserAgent;
    use Mojo::UserAgent::CookieJar::ChromeMacOS;

    my $ua = Mojo::UserAgent->new;
    $ua->cookie_jar(Mojo::UserAgent::CookieJar::ChromeMacOS->new);

    # For Linux
    Mojo::UserAgent::CookieJar::ChromeMacOS->new(
        file => '~/.config/google-chrome/Default/Cookies',
        pass => 'peanuts', # hardcode for Linux
    );

=head1 DESCRIPTION

Mojo::UserAgent::CookieJar::ChromeMacOS tries to read the cookie from Chrome on MacOSx.

it would be useful when you need handle tricky logins or captchas.

=head1 AUTHOR

Fayland Lam E<lt>fayland@gmail.comE<gt>

=head1 COPYRIGHT

Copyright 2016- Fayland Lam

=head1 LICENSE

This library is free software; you can redistribute it and/or modify



( run in 0.921 second using v1.01-cache-2.11-cpan-751830e7986 )