Azure-AD-Auth

 view release on metacpan or  search on metacpan

lib/Azure/AD/ClientCredentials.pm  view on Meta::CPAN

  );

  has ad_url => (
    is => 'ro',
    isa => Str,
    default => sub {
      'https://login.microsoftonline.com'
    },
  );

  has token_endpoint => (
    is => 'ro',
    isa => Str,
    lazy => 1,
    default => sub {
      my $self = shift;
      sprintf "%s/%s/oauth2/token", $self->ad_url, $self->tenant_id;
    }
  );

  sub access_token {

lib/Azure/AD/ClientCredentials.pm  view on Meta::CPAN

    my $self = shift;

    if (not defined $self->current_creds) {
      $self->_refresh_from_cache;
      return $self->current_creds if (defined $self->current_creds);
    }

    return if $self->expiration >= time;

    my $auth_response = $self->ua->post_form(
      $self->token_endpoint,
      {
        grant_type    => 'client_credentials',
        client_id     => $self->client_id,
        client_secret => $self->secret_id,
        resource      => $self->resource_id,
      }
    );

    if (not $auth_response->{ success }) {
      Azure::AD::RemoteError->throw(

lib/Azure/AD/ClientCredentials.pm  view on Meta::CPAN


The Client ID (also referred to as the Application ID) of an application

=head2 secret_id

A Key assigned to the Client Id.

=head2 ad_url

This defaults to C<https://login.microsoftonline.com>, and generally doesn't need to
be specified. Azure AD has more endpoints for some clouds: 

C<https://login.chinacloudapi.cn> China Cloud

C<https://login.microsoftonline.us> US Gov Cloud

C<https://login.microsoftonline.de> German Cloud

=head1 METHODS

=head2 access_token

lib/Azure/AD/ClientCredentialsV2.pm  view on Meta::CPAN

  );

  has ad_url => (
    is => 'ro',
    isa => Str,
    default => sub {
      'https://login.microsoftonline.com'
    },
  );

  has token_endpoint => (
    is => 'ro',
    isa => Str,
    lazy => 1,
    default => sub {
      my $self = shift;
      sprintf "%s/%s/oauth2/v2.0/token", $self->ad_url, $self->tenant_id;
    }
  );

  sub access_token {

lib/Azure/AD/ClientCredentialsV2.pm  view on Meta::CPAN

    my $self = shift;

    if (not defined $self->current_creds) {
      $self->_refresh_from_cache;
      return $self->current_creds if (defined $self->current_creds);
    }

    return if $self->expiration >= time;

    my $auth_response = $self->ua->post_form(
      $self->token_endpoint,
      {
        grant_type    => 'client_credentials',
        client_id     => $self->client_id,
        client_secret => $self->secret_id,
        scope         => $self->scope,
      }
    );

    if (not $auth_response->{success}) {
      Azure::AD::RemoteError->throw(

lib/Azure/AD/ClientCredentialsV2.pm  view on Meta::CPAN

  my $creds = Azure::AD::ClientCredentialsV2->new(
    resource_id => 'https://management.core.windows.net/',
    client_id => '',
    secret_id => '',
    tenant_id => '',
  );
  say $creds->access_token;

=head1 DESCRIPTION

Implements the Azure AD Client Credentials flow using the V2 Oauth endpoint. See L<Azure::AD::Auth> for more
information and alternative flows.

=head1 ATTRIBUTES

=head2 scope

Defines the set of permissions being requested by the application. Scopes can be either static (using .default) or dynamic. This set can include the OpenID Connect scopes (openid, profile, email). If you need application permissions, you must use .de...

=head2 tenant_id

lib/Azure/AD/ClientCredentialsV2.pm  view on Meta::CPAN


The Client ID (also referred to as the Application ID) of an application

=head2 secret_id

A Key assigned to the Client Id.

=head2 ad_url

This defaults to C<https://login.microsoftonline.com>, and generally doesn't need to
be specified. Azure AD has more endpoints for some clouds:

C<https://login.chinacloudapi.cn> China Cloud

C<https://login.microsoftonline.us> US Gov Cloud

C<https://login.microsoftonline.de> German Cloud

=head1 METHODS

=head2 access_token

lib/Azure/AD/DeviceLogin.pm  view on Meta::CPAN

  );

  has ad_url => (
    is => 'ro',
    isa => Str,
    default => sub {
      'https://login.microsoftonline.com'
    },
  );

  has device_endpoint => (
    is => 'ro',
    isa => Str,
    lazy => 1,
    default => sub {
      my $self = shift;
      sprintf '%s/%s/oauth2/devicecode', $self->ad_url, $self->tenant_id;
    }
  );

  has token_endpoint => (
    is => 'ro',
    isa => Str,
    lazy => 1,
    default => sub {
      my $self = shift;
      sprintf "%s/%s/oauth2/token", $self->ad_url, $self->tenant_id;
    }
  );

  sub access_token {

lib/Azure/AD/DeviceLogin.pm  view on Meta::CPAN

  }

  sub _save_to_cache {
    my $self = shift;
    #TODO: implement caching strategy
  }

  sub get_device_payload {
    my $self = shift;
    my $device_response = $self->ua->post_form(
      $self->device_endpoint,
      {
        client_id => $self->client_id,
        resource  => $self->resource_id,
      }
    );

    if (not $device_response->{ success }) {
      Azure::AD::RemoteError->throw(
        message => $device_response->{ content },
        code => 'GetDeviceCodeFailed',

lib/Azure/AD/DeviceLogin.pm  view on Meta::CPAN


  sub get_auth_payload_for {
    my ($self, $device_payload) = @_;

    my $code_expiration = time + $device_payload->{ expires_in };
    my $auth_response;
    while ($code_expiration > time and not $auth_response->{ success }) {
      sleep($device_payload->{ interval });

      $auth_response = $self->ua->post_form(
        $self->token_endpoint,
        {
          grant_type => 'device_code',
          code       => $device_payload->{ device_code },
          client_id  => $self->client_id,
          resource   => $self->resource_id,
        }
      );
    }
 
    if (not $auth_response->{ success }) {

lib/Azure/AD/DeviceLogin.pm  view on Meta::CPAN


The ID of the Azure Active Directory Tenant

=head2 client_id

The Client ID (also referred to as the Application ID) of an application

=head2 ad_url

This defaults to C<https://login.microsoftonline.com>, and generally doesn't need to
be specified. Azure AD has more endpoints for some clouds: 

C<https://login.chinacloudapi.cn> China Cloud

C<https://login.microsoftonline.us> US Gov Cloud

C<https://login.microsoftonline.de> German Cloud

=head1 METHODS

=head2 access_token

lib/Azure/AD/Password.pm  view on Meta::CPAN

  );

  has ad_url => (
    is => 'ro',
    isa => Str,
    default => sub {
      'https://login.microsoftonline.com'
    },
  );

  has token_endpoint => (
    is => 'ro',
    isa => Str,
    lazy => 1,
    default => sub {
      my $self = shift;
      sprintf "%s/%s/oauth2/token", $self->ad_url, $self->tenant_id;
    }
  );

  sub access_token {

lib/Azure/AD/Password.pm  view on Meta::CPAN

    my $self = shift;

    if (not defined $self->current_creds) {
      $self->_refresh_from_cache;
      return $self->current_creds if (defined $self->current_creds);
    }

    return if $self->expiration >= time;

    my $auth_response = $self->ua->post_form(
      $self->token_endpoint,
      {
        grant_type    => 'password',
        client_id     => $self->client_id,
        resource      => $self->resource_id,
        username      => $self->username,
        password      => $self->password,
      }
    );

    if (not $auth_response->{ success }) {

lib/Azure/AD/Password.pm  view on Meta::CPAN


The user name to use for authentication.

=head2 password

The password of the user.

=head2 ad_url

This defaults to C<https://login.microsoftonline.com>, and generally doesn't need to
be specified. Azure AD has more endpoints for some clouds: 

C<https://login.chinacloudapi.cn> China Cloud

C<https://login.microsoftonline.us> US Gov Cloud

C<https://login.microsoftonline.de> German Cloud

=head1 METHODS

=head2 access_token

t/01_load.t  view on Meta::CPAN

use_ok('Azure::AD::DeviceLogin');
use_ok('Azure::AD::Password');

{
  my $auth = Azure::AD::ClientCredentials->new(
    resource_id => 'random',
    client_id => 'cid1',
    tenant_id => 'ten1',
    secret_id => 'sec1',
  );
  like($auth->token_endpoint, qr|^https://login.microsoftonline.com|, 'Got default endpoint');
}

{
  my $auth = Azure::AD::ClientCredentials->new(
    resource_id => 'random',
    client_id => 'cid1',
    tenant_id => 'ten1',
    secret_id => 'sec1',
    ad_url => 'https://login.microsoftonline.us',
  );
  like($auth->token_endpoint, qr|^https://login.microsoftonline.us|, 'Got custom US endpoint');
}

{
  my $auth = Azure::AD::ClientCredentialsV2->new(
    scope     => 'scope',
    client_id => 'cid1',
    tenant_id => 'ten1',
    secret_id => 'sec1',
  );
  like($auth->token_endpoint, qr|^https://login.microsoftonline.com|, 'Got default endpoint');
}

{
  my $auth = Azure::AD::ClientCredentialsV2->new(
    scope     => 'scope',
    client_id => 'cid1',
    tenant_id => 'ten1',
    secret_id => 'sec1',
    ad_url => 'https://login.microsoftonline.us',
  );
  like($auth->token_endpoint, qr|^https://login.microsoftonline.us|, 'Got custom US endpoint');
}

{
  my $auth = Azure::AD::DeviceLogin->new(
    resource_id => 'random',
    client_id => 'cid1',
    tenant_id => 'ten1',
    message_handler => sub { },
  );
  like($auth->token_endpoint, qr|^https://login.microsoftonline.com|, 'Got default endpoint');
  like($auth->device_endpoint, qr|^https://login.microsoftonline.com|, 'Got default endpoint');
}

{
  my $auth = Azure::AD::DeviceLogin->new(
    resource_id => 'random',
    client_id => 'cid1',
    tenant_id => 'ten1',
    message_handler => sub { },
    ad_url => 'https://login.microsoftonline.us',
  );
  like($auth->token_endpoint, qr|^https://login.microsoftonline.us|, 'Got custom US endpoint');
  like($auth->device_endpoint, qr|^https://login.microsoftonline.us|, 'Got default endpoint');
}

{
  my $auth = Azure::AD::Password->new(
    resource_id => 'random',
    client_id => 'cid1',
    tenant_id => 'ten1',
    username => 'user',
    password => 'pass',
  );
  like($auth->token_endpoint, qr|^https://login.microsoftonline.com|, 'Got default endpoint');
}

{
  my $auth = Azure::AD::Password->new(
    resource_id => 'random',
    client_id => 'cid1',
    tenant_id => 'ten1',
    username => 'user',
    password => 'pass',
    ad_url => 'https://login.microsoftonline.us',
  );
  like($auth->token_endpoint, qr|^https://login.microsoftonline.us|, 'Got custom US endpoint');
}

done_testing;



( run in 0.875 second using v1.01-cache-2.11-cpan-49f99fa48dc )