From 11d68a1b6011140f777e73876796f81edb40e6b1 Mon Sep 17 00:00:00 2001 From: "Zane C. Bowers-Hadley" Date: Tue, 1 Dec 2020 00:23:29 -0600 Subject: [PATCH] add auth_header method --- Changes | 2 +- README.md | 10 ---- lib/LogicMonitor/REST/Signature.pm | 83 ++++++++++++++++++------------ t/sig.t | 18 +++---- 4 files changed, 61 insertions(+), 52 deletions(-) diff --git a/Changes b/Changes index b23e1c9..bc76ecf 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,5 @@ Revision history for LogicMonitor-REST-Signature -0.01 Date/time +0.0.1 Date/time First version, released on an unsuspecting world. diff --git a/README.md b/README.md index 366abd0..c6846cb 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,5 @@ LogicMonitor-REST-Signature -The README is used to introduce the module and provide instructions on -how to install the module, any machine dependencies it may have (for -example C compilers and installed libraries) and any other information -that should be provided before the module is installed. - -A README file is required for CPAN modules since CPAN extracts the README -file from a module distribution so that people browsing the archive -can use it to get an idea of the module's uses. It is usually a good idea -to provide version information here so that people can decide whether -fixes for the module are worth downloading. INSTALLATION diff --git a/lib/LogicMonitor/REST/Signature.pm b/lib/LogicMonitor/REST/Signature.pm index 4f74f1c..5480434 100644 --- a/lib/LogicMonitor/REST/Signature.pm +++ b/lib/LogicMonitor/REST/Signature.pm @@ -144,7 +144,6 @@ sub new { bless $self; return $self; - } =head2 signature @@ -154,30 +153,15 @@ This generates the signature for a request. This requires variables below. HTTPverb + timestamp path -The value below is optional and will be automatically generated if not -specified, or in the case of data set to ''. +If data is not specified, it is assumed to be ''. - timestamp data Example... - my $sig; - eval{ - $sig=$lmsig_helper->signature({ - HTTPverb=>'GET', - path=>/foo/bar', - data=>'foo foo', - }); - }; - if (!defined($sig)){ - die("Failed to generate the signature... ".$@); - } - -Example with timestamp... - my $sig; eval{ $sig=$lmsig_helper->signature({ @@ -201,8 +185,9 @@ sub signature { # a list of all required keys my $args_valid_keys = { - HTTPverb => 1, - path => 1, + HTTPverb => 1, + path => 1, + timestamp => 1, }; # make sure are the required variables are present @@ -217,18 +202,6 @@ sub signature { $args->{data} = ''; } - # generate the timestamp if needed - # gettimeofday returns microseconds... convert to milliseconds - if ( !defined( $args->{timestamp} ) ) { - - # gettimeofday returns microseconds... convert to milliseconds - $args->{timestamp} = gettimeofday * 1000; - - # appears to only want the integer portion based on their examples - # https://www.logicmonitor.com/support/rest-api-developers-guide/v1/rest-api-v1-examples - $args->{timestamp} = int( $args->{timestamp} ); - } - # put together the string that will be used for the signature # https://www.logicmonitor.com/support/rest-api-developers-guide/overview/using-logicmonitors-rest-api#ss-header-24 my $string = $args->{HTTPverb} . $args->{timestamp} . $args->{data} . $args->{path}; @@ -248,6 +221,52 @@ sub signature { return $sig; } +=sub auth_header + +Generates the auth header. The usage is similiar to signature, but this does not +need a timestamp specified. + +This requires variables below. + + HTTPverb + path + +If data is not specified, it is assumed to be ''. + + data + +Example... + + my $auth_header; + eval{ + $sig=$lmsig_helper->auth_header({ + HTTPverb=>'GET', + path=>/foo/bar', + }); + }; + if (!defined($sig)){ + die("Failed to generate the auth header... ".$@); + } + +=cut + +sub auth_header{ + my $self = $_[0]; + my $args=$_[1]; + if ( !defined( $_[1] ) ) { + die('No argument hash ref passed'); + } + + my $timestamp = gettimeofday * 1000; + $timestamp = int($timestamp); + + $args->{timestamp}=$timestamp; + + my $header='LMv1 '.$self->{accessID}.':'.$self->signature($args).':'.$timestamp; + + return $header; +} + =head1 AUTHOR Zane C. Bowers-Hadley, C<< >> diff --git a/t/sig.t b/t/sig.t index 9efb9dc..9a8dada 100644 --- a/t/sig.t +++ b/t/sig.t @@ -50,19 +50,19 @@ eval{ }; ok( $worked eq '1', 'signature 0') or diag("Failed to create the expected signature... ".$@); -# make sure it can generate a known one, which requires a time stamp +# tests if it can call auth_header and generate a valid signature $worked=0; eval{ - my $sig=$lmsig_helper->signature({ - HTTPverb=>'GET', - path=>'/foo', - data=>'', - }); - if (! defined ( $sig ) ){ - die( 'Got a return of undef' ); + my $auth_header=$lmsig_helper->signature({ + HTTPverb=>'GET', + path=>'/foo', + data=>'', + }); + if ( $auth_header !~ /^LMv1\ .*\:[a-zA-Z0-9\+\/\=]*:[0-9]+$/ ){ + die 'Got "'.$auth_header.'" but was expecting "e0bb5OESDeQdMvtJy1Nr6Nju7Nd9axVXHUhMQjjA3f4="'; } $worked=1 }; -ok( $worked eq '1', 'signature 1') or diag("Failed to create the a signature when auto generating a timestamp... ".$@); +ok( $worked eq '1', 'auth_header 0') or diag("Failed to create a auth_header... ".$@); done_testing(5);