add auth_header method

This commit is contained in:
Zane C. B-H 2020-12-01 00:23:29 -06:00
parent 15ca300a67
commit 11d68a1b60
4 changed files with 61 additions and 52 deletions

View File

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

View File

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

View File

@ -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<< <vvelox at vvelox.net> >>

18
t/sig.t
View File

@ -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);