#! /usr/bin/perl -W
use strict;
use WWW::Curl::Easy;
use Data::Dumper;
use JSON;
use Config::Simple;
use FindBin;
#use lib "$FindBin::Bin/../lib";
my %config;
Config::Simple->import_from("$FindBin::Bin/groups.cfg",\%config) or die("No config: $!");
my $c = WWW::Curl::Easy->new();
sub login_app { # {{{1
my $response;
my $response_body;
print "==LogIn==\n";
my @Headers = (
"Content-Type: application/x-www-form-urlencoded"
);
$c->setopt(CURLOPT_URL, "https://$config{'LOGIN_ENDPOINT'}/$config{'TENANT_ID'}/oauth2/token");
$c->setopt(CURLOPT_HEADER, "0");
$c->setopt(CURLOPT_WRITEDATA, \$response_body);
$c->setopt(CURLOPT_SSL_VERIFYHOST, "1");
$c->setopt(CURLOPT_SSL_VERIFYPEER, "1");
$c->setopt(CURLOPT_POSTFIELDS, "grant_type=client_credentials&client_id=$config{'APP_ID'}&client_secret=$config{'APP_PASS'}");
$c->setopt(CURLOPT_HTTPHEADER, \@Headers);
$c->setopt(CURLOPT_CUSTOMREQUEST, 'POST');
my $retcode = $c->perform();
print "Return code login: $retcode\n";
if ($retcode == 0){
my $response = decode_json($response_body);
#print Dumper $response;
return $response;
}else{
print("An error occured: $retcode\n".$c->strerror($retcode)." ".$c->errbuf."\n");
return 0;
}
}# }}}
sub fetch {
my $token = shift;
my $url = shift;
my $method = shift;
my $response;
my $response_body;
print "==Fetch==\n";
print "$url\n";
print "$token\n";
my @Headers = (
"Authorization: Bearer $token",
'Content-Type: application/json'
);
#print Dumper \@Headers;
$c->setopt(CURLOPT_URL, "$url");
$c->setopt(CURLOPT_HEADER, "0");
$c->setopt(CURLOPT_WRITEDATA, \$response_body);
$c->setopt(CURLOPT_SSL_VERIFYHOST, "0");
$c->setopt(CURLOPT_SSL_VERIFYPEER, "0");
$c->setopt(CURLOPT_HTTPHEADER, \@Headers);
$c->setopt(CURLOPT_CUSTOMREQUEST, $method);
my $retcode = $c->perform();
print "Return code fetch: $retcode\n";
if ($retcode == 0){
my $response = decode_json($response_body);
return $response;
}else{
print("An error occured: $retcode\n".$c->strerror($retcode)." ".$c->errbuf."\n");
return 0;
}}
my $token_request = login_app();
if ($token_request){
if ($$token_request{'access_token'}){
my $url = "https://$config{'GRAPH_ENDPOINT'}/v1.0/groups";
my $groups = fetch($$token_request{'access_token'},$url,'GET');
print Dumper $groups;
}else{
print "Geen token\n";
print Dumper $token_request;
}
}
####
Bareword "Types::Serialiser::Error::" refers to nonexistent package at /usr/share/perl5/Types/Serialiser.pm line 136.
Bareword "Types::Serialiser::Error::" refers to nonexistent package at /usr/share/perl5/Types/Serialiser.pm line 147.
==LogIn==
Return code login: 0
==Fetch==
https://graph.microsoft.com/v1.0/groups
eyJ0eXAiOi[edited out most of the token]eRppGoilg
Return code fetch: 0
$VAR1 = {
'error' => {
'innerError' => {
'client-request-id' => 'f49ce921-3ceb-4613-a901-3eb75a9126a2',
'date' => '2023-01-18T10:52:43',
'request-id' => 'f49ce921-3ceb-4613-a901-3eb75a9126a2'
},
'code' => 'InvalidAuthenticationToken',
'message' => 'Access token validation failure. Invalid audience.'
}
};
####
curl -X GET -H "Authorization: Bearer [TOKEN]" -H "Content-Type: application/json" https://management.azure.com/subscriptions/[SUBSCRIPTION_ID]/providers/Microsoft.Web/sites?api-version=2016-08-01