Add OAuth

This commit is contained in:
mschuepbach
2024-03-27 15:25:17 +01:00
parent 088fdb2b16
commit 8c0319f293
6 changed files with 398 additions and 27 deletions

View File

@@ -0,0 +1,51 @@
package YTMusicAPI::Auth::OAuth::OAuthToken;
use strict;
use warnings;
use JSON qw(decode_json);
use Path::Tiny qw(path);
use parent 'YTMusicAPI::Auth::OAuth::Token';
sub new {
my ( $class, $args ) = @_;
my $self = $class->SUPER::new($args);
bless $self, $class;
return $self;
}
sub is_oauth {
my ($headers) = @_;
my @required_keys = YTMusicAPI::Auth::OAuth::Token::members();
foreach my $key (@required_keys) {
return 0 unless exists $headers->{$key};
}
return 1;
}
sub update {
my ( $self, $fresh_access ) = @_;
$self->{access_token} = $fresh_access->{access_token};
$self->{expires_at} = int( time() ) + $fresh_access->{expires_in};
}
sub is_expiring {
my ($self) = @_;
return $self->{expires_at} - int( time() ) < 60 ? 1 : 0;
}
sub from_json {
my ( $class, $file_path ) = @_;
my $file_pack;
if ( path($file_path)->is_file ) {
$file_pack = decode_json( path($file_path)->slurp_utf8 );
}
return $class->new(%$file_pack);
}
1;