enh(office365): better management of secret change option (#3193)
This commit is contained in:
parent
509af287a9
commit
3adecf8428
|
@ -127,8 +127,14 @@ sub get_access_token {
|
|||
my $has_cache_file = $options{statefile}->read(statefile => 'office365_graphapi_' . md5_hex($self->{tenant}) . '_' . md5_hex($self->{client_id}));
|
||||
my $expires_on = $options{statefile}->get(name => 'expires_on');
|
||||
my $access_token = $options{statefile}->get(name => 'access_token');
|
||||
my $md5_secret_cache = $options{statefile}->get(name => 'md5_secret');
|
||||
my $md5_secret = md5_hex($self->{client_secret});
|
||||
|
||||
if ($has_cache_file == 0 || !defined($access_token) || (($expires_on - time()) < 10)) {
|
||||
if ($has_cache_file == 0 ||
|
||||
!defined($access_token) ||
|
||||
(($expires_on - time()) < 10) ||
|
||||
(defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret)
|
||||
) {
|
||||
my $uri = URI::Encode->new({encode_reserved => 1});
|
||||
my $encoded_client_secret = $uri->encode($self->{client_secret});
|
||||
my $encoded_graph_endpoint = $uri->encode($self->{graph_endpoint} . '/.default');
|
||||
|
@ -139,34 +145,39 @@ sub get_access_token {
|
|||
|
||||
$self->settings();
|
||||
|
||||
my $content = $self->{http}->request(method => 'POST', query_form_post => $post_data,
|
||||
full_url => $self->{login_endpoint} . '/' . $self->{tenant} . '/oauth2/v2.0/token',
|
||||
hostname => '');
|
||||
my $content = $self->{http}->request(
|
||||
method => 'POST', query_form_post => $post_data,
|
||||
full_url => $self->{login_endpoint} . '/' . $self->{tenant} . '/oauth2/v2.0/token',
|
||||
hostname => ''
|
||||
);
|
||||
|
||||
my $decoded;
|
||||
eval {
|
||||
$decoded = JSON::XS->new->utf8->decode($content);
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->output_add(long_msg => $content, debug => 1);
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
if (defined($decoded->{error})) {
|
||||
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error_description}, debug => 1);
|
||||
$self->{output}->add_option_msg(short_msg => "Login endpoint API return error code '" . $decoded->{error} . "' (add --debug option for detailed message)");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
$access_token = $decoded->{access_token};
|
||||
my $datas = { last_timestamp => time(), access_token => $decoded->{access_token}, expires_on => time() + $decoded->{expires_in} };
|
||||
my $datas = {
|
||||
last_timestamp => time(),
|
||||
access_token => $decoded->{access_token},
|
||||
expires_on => time() + $decoded->{expires_in},
|
||||
md5_secret => $md5_secret
|
||||
};
|
||||
$options{statefile}->write(data => $datas);
|
||||
}
|
||||
|
||||
return $access_token;
|
||||
}
|
||||
|
||||
sub request_api_json { #so lame for now
|
||||
sub request_api_json {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
if (!defined($self->{access_token})) {
|
||||
|
@ -179,8 +190,6 @@ sub request_api_json { #so lame for now
|
|||
my %local_options = %options;
|
||||
|
||||
while (1) {
|
||||
$self->{output}->output_add(long_msg => "URL: '" . $local_options{full_url} . "'", debug => 1);
|
||||
|
||||
my $content = $self->{http}->request(%local_options);
|
||||
|
||||
if ($self->{http}->get_code() == 429) {
|
||||
|
@ -192,12 +201,10 @@ sub request_api_json { #so lame for now
|
|||
$decoded = JSON::XS->new->utf8->decode($content);
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->output_add(long_msg => $content, debug => 1);
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
if (defined($decoded->{error})) {
|
||||
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}->{message}, debug => 1);
|
||||
$self->{output}->add_option_msg(short_msg => "Graph endpoint API return error code '" . $decoded->{error}->{code} . "' (add --debug option for detailed message)");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
@ -218,7 +225,6 @@ sub request_api_csv {
|
|||
}
|
||||
|
||||
$self->settings();
|
||||
$self->{output}->output_add(long_msg => "URL: '" . $options{full_url} . "'", debug => 1);
|
||||
|
||||
my $content = $self->{http}->request(%options);
|
||||
|
||||
|
@ -228,12 +234,10 @@ sub request_api_csv {
|
|||
$decoded = JSON::XS->new->utf8->decode($content);
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->output_add(long_msg => $content, debug => 1);
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
if (defined($decoded->{error})) {
|
||||
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}->{message}, debug => 1);
|
||||
$self->{output}->add_option_msg(short_msg => "Graph endpoint API return error code '" . $decoded->{error}->{code} . "' (add --debug option for detailed message)");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
|
|
@ -125,8 +125,14 @@ sub get_access_token {
|
|||
my $has_cache_file = $options{statefile}->read(statefile => 'office365_managementapi_' . md5_hex($self->{tenant}) . '_' . md5_hex($self->{client_id}));
|
||||
my $expires_on = $options{statefile}->get(name => 'expires_on');
|
||||
my $access_token = $options{statefile}->get(name => 'access_token');
|
||||
my $md5_secret_cache = $options{statefile}->get(name => 'md5_secret');
|
||||
my $md5_secret = md5_hex($self->{client_secret});
|
||||
|
||||
if ($has_cache_file == 0 || !defined($access_token) || (($expires_on - time()) < 10)) {
|
||||
if ($has_cache_file == 0 ||
|
||||
!defined($access_token) ||
|
||||
(($expires_on - time()) < 10) ||
|
||||
(defined($md5_secret_cache) && $md5_secret_cache ne $md5_secret)
|
||||
) {
|
||||
my $uri = URI::Encode->new({encode_reserved => 1});
|
||||
my $encoded_client_secret = $uri->encode($self->{client_secret});
|
||||
my $encoded_management_endpoint = $uri->encode($self->{management_endpoint});
|
||||
|
@ -137,9 +143,11 @@ sub get_access_token {
|
|||
|
||||
$self->settings();
|
||||
|
||||
my $content = $self->{http}->request(method => 'POST', query_form_post => $post_data,
|
||||
full_url => $self->{login_endpoint} . '/' . $self->{tenant} . '/oauth2/token?api-version=1.0',
|
||||
hostname => '');
|
||||
my $content = $self->{http}->request(
|
||||
method => 'POST', query_form_post => $post_data,
|
||||
full_url => $self->{login_endpoint} . '/' . $self->{tenant} . '/oauth2/token?api-version=1.0',
|
||||
hostname => ''
|
||||
);
|
||||
|
||||
my $decoded;
|
||||
eval {
|
||||
|
@ -157,7 +165,12 @@ sub get_access_token {
|
|||
}
|
||||
|
||||
$access_token = $decoded->{access_token};
|
||||
my $datas = { last_timestamp => time(), access_token => $decoded->{access_token}, expires_on => $decoded->{expires_on} };
|
||||
my $datas = {
|
||||
last_timestamp => time(),
|
||||
access_token => $decoded->{access_token},
|
||||
expires_on => $decoded->{expires_on},
|
||||
md5_secret => $md5_secret
|
||||
};
|
||||
$options{statefile}->write(data => $datas);
|
||||
}
|
||||
|
||||
|
@ -172,7 +185,6 @@ sub request_api {
|
|||
}
|
||||
|
||||
$self->settings();
|
||||
|
||||
my $content = $self->{http}->request(%options);
|
||||
|
||||
my $decoded;
|
||||
|
@ -180,12 +192,10 @@ sub request_api {
|
|||
$decoded = JSON::XS->new->utf8->decode($content);
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->output_add(long_msg => $content, debug => 1);
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
if (defined($decoded->{error})) {
|
||||
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}->{message}, debug => 1);
|
||||
$self->{output}->add_option_msg(short_msg => "Management endpoint API return error code '" . $decoded->{error}->{code} . "' (add --debug option for detailed message)");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue