Skip to content

Commit

Permalink
Try to load credentials from /etc/youtube-viewer/api.json. (if this…
Browse files Browse the repository at this point in the history
… file exists) (#308)

Personal credentials can be specified in `~/.config/youtube-viewer/api.json`.
  • Loading branch information
trizen committed Feb 25, 2020
1 parent c3819da commit a487b21
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 105 deletions.
41 changes: 6 additions & 35 deletions bin/gtk2-youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ my $youtube_users_file = catfile($config_dir, 'youtube_users.txt');
my $history_file = catfile($config_dir, 'history.txt');
my $authentication_file = catfile($config_dir, 'reg.dat');
my $api_file = catfile($config_dir, 'api.json');
my $default_api_file = '/etc/youtube-viewer/api.json';

# Create the configuration directory
foreach my $dir ($config_dir) {
Expand Down Expand Up @@ -749,42 +750,12 @@ my $yv_obj = WWW::YoutubeViewer->new(
authentication_file => $authentication_file,
);

if (-f $api_file) {

open(my $fh, '<', $api_file) or die "[!] Can't open file <<$api_file>> for reading: $!\n";
my $content = do { local $/; <$fh> };
my $api = $yv_obj->parse_json_string($content);

if (ref($api) ne 'HASH') {
die "[!] Invalid format inside file 'api.json'.\n";
}

my $orig_key = $yv_obj->get_key;
my $orig_client_id = $yv_obj->get_client_id;
my $orig_client_secret = $yv_obj->get_client_secret;

my $key = $api->{key};
my $client_id = $api->{client_id};
my $client_secret = $api->{client_secret};
if (-f $default_api_file) {
$yv_obj->load_credentials($default_api_file);
}

if (defined($key)) {
$yv_obj->set_key($key) // do {
warn "[!] Invalid key: $key\n" if $key ne 'API_KEY';
$yv_obj->set_key($orig_key);
};
}
if (defined($client_id)) {
$yv_obj->set_client_id($client_id) // do {
warn "[!] Invalid client_id: $client_id\n" if $client_id ne 'CLIENT_ID';
$yv_obj->set_client_id($orig_client_id);
};
}
if (defined($client_secret)) {
$yv_obj->set_client_secret($client_secret) // do {
warn "[!] Invalid client_secret: $client_secret\n" if $client_secret ne 'CLIENT_SECRET';
$yv_obj->set_client_secret($orig_client_secret);
};
}
if (-f $api_file) {
$yv_obj->load_credentials($api_file);
}
else {
open(my $fh, '>', $api_file) or warn "[!] Can't create file <<$api_file>>: $!\n";
Expand Down
41 changes: 6 additions & 35 deletions bin/gtk3-youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ my $history_file = catfile($config_dir, 'history.txt');
my $session_file = catfile($config_dir, 'session.dat');
my $authentication_file = catfile($config_dir, 'reg.dat');
my $api_file = catfile($config_dir, 'api.json');
my $default_api_file = '/etc/youtube-viewer/api.json';

# Create the configuration directory
foreach my $dir ($config_dir) {
Expand Down Expand Up @@ -812,42 +813,12 @@ my $yv_obj = WWW::YoutubeViewer->new(
authentication_file => $authentication_file,
);

if (-f $api_file) {

open(my $fh, '<', $api_file) or die "[!] Can't open file <<$api_file>> for reading: $!\n";
my $content = do { local $/; <$fh> };
my $api = $yv_obj->parse_json_string($content);

if (ref($api) ne 'HASH') {
die "[!] Invalid format inside file 'api.json'.\n";
}

my $orig_key = $yv_obj->get_key;
my $orig_client_id = $yv_obj->get_client_id;
my $orig_client_secret = $yv_obj->get_client_secret;

my $key = $api->{key};
my $client_id = $api->{client_id};
my $client_secret = $api->{client_secret};
if (-f $default_api_file) {
$yv_obj->load_credentials($default_api_file);
}

if (defined($key)) {
$yv_obj->set_key($key) // do {
warn "[!] Invalid key: $key\n" if $key ne 'API_KEY';
$yv_obj->set_key($orig_key);
};
}
if (defined($client_id)) {
$yv_obj->set_client_id($client_id) // do {
warn "[!] Invalid client_id: $client_id\n" if $client_id ne 'CLIENT_ID';
$yv_obj->set_client_id($orig_client_id);
};
}
if (defined($client_secret)) {
$yv_obj->set_client_secret($client_secret) // do {
warn "[!] Invalid client_secret: $client_secret\n" if $client_secret ne 'CLIENT_SECRET';
$yv_obj->set_client_secret($orig_client_secret);
};
}
if (-f $api_file) {
$yv_obj->load_credentials($api_file);
}
else {
open(my $fh, '>', $api_file) or warn "[!] Can't create file <<$api_file>>: $!\n";
Expand Down
41 changes: 6 additions & 35 deletions bin/youtube-viewer
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ my $authentication_file = catfile($config_dir, 'reg.dat');
my $history_file = catfile($config_dir, 'history.txt');
my $watched_file = catfile($config_dir, 'watched.txt');
my $api_file = catfile($config_dir, 'api.json');
my $default_api_file = '/etc/youtube-viewer/api.json';

if (not -d $config_dir) {
require File::Path;
Expand Down Expand Up @@ -632,42 +633,12 @@ my $yv_obj = WWW::YoutubeViewer->new(
authentication_file => $authentication_file,
);

if (-f $api_file) {

open(my $fh, '<', $api_file) or die "[!] Can't open file <<$api_file>> for reading: $!\n";
my $content = do { local $/; <$fh> };
my $api = $yv_obj->parse_json_string($content);

if (ref($api) ne 'HASH') {
die "[!] Invalid format inside file 'api.json'.\n";
}

my $orig_key = $yv_obj->get_key;
my $orig_client_id = $yv_obj->get_client_id;
my $orig_client_secret = $yv_obj->get_client_secret;

my $key = $api->{key};
my $client_id = $api->{client_id};
my $client_secret = $api->{client_secret};
if (-f $default_api_file) {
$yv_obj->load_credentials($default_api_file);
}

if (defined($key)) {
$yv_obj->set_key($key) // do {
warn "[!] Invalid key: $key\n" if $key ne 'API_KEY';
$yv_obj->set_key($orig_key);
};
}
if (defined($client_id)) {
$yv_obj->set_client_id($client_id) // do {
warn "[!] Invalid client_id: $client_id\n" if $client_id ne 'CLIENT_ID';
$yv_obj->set_client_id($orig_client_id);
};
}
if (defined($client_secret)) {
$yv_obj->set_client_secret($client_secret) // do {
warn "[!] Invalid client_secret: $client_secret\n" if $client_secret ne 'CLIENT_SECRET';
$yv_obj->set_client_secret($orig_client_secret);
};
}
if (-f $api_file) {
$yv_obj->load_credentials($api_file);
}
else {
open(my $fh, '>', $api_file) or warn "[!] Can't create file <<$api_file>>: $!\n";
Expand Down
60 changes: 60 additions & 0 deletions lib/WWW/YoutubeViewer/Authentication.pm
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,66 @@ sub save_authentication_tokens {
return;
}

=head2 load_credentials($file)
Load the API key and the client ID/SECRET values from a given JSON file having the following format:
{
"key": "API_KEY",
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET"
}
Returns true on success and false otherwise.
=cut

sub load_credentials {
my ($self, $api_file) = @_;

open(my $fh, '<', $api_file) or do {
warn "[!] Can't open file <<$api_file>> for reading: $!\n";
return;
};

my $content = do { local $/; <$fh> };
my $api = $self->parse_json_string($content);

if (ref($api) ne 'HASH') {
warn "[!] Invalid format inside file: $api_file\n";
return;
}

my $orig_key = $self->get_key;
my $orig_client_id = $self->get_client_id;
my $orig_client_secret = $self->get_client_secret;

my $key = $api->{key};
my $client_id = $api->{client_id};
my $client_secret = $api->{client_secret};

if (defined($key)) {
$self->set_key($key) // do {
warn "[!] Invalid key: $key\n" if $key ne 'API_KEY';
$self->set_key($orig_key);
};
}
if (defined($client_id)) {
$self->set_client_id($client_id) // do {
warn "[!] Invalid client_id: $client_id\n" if $client_id ne 'CLIENT_ID';
$self->set_client_id($orig_client_id);
};
}
if (defined($client_secret)) {
$self->set_client_secret($client_secret) // do {
warn "[!] Invalid client_secret: $client_secret\n" if $client_secret ne 'CLIENT_SECRET';
$self->set_client_secret($orig_client_secret);
};
}

return 1;
}

=head1 AUTHOR
Trizen, C<< <echo dHJpemVuQHByb3Rvbm1haWwuY29tCg== | base64 -d> >>
Expand Down

0 comments on commit a487b21

Please sign in to comment.