| 1 | package Paws::Credential::STS; | ||
| 2 | use Moose; | ||
| 3 | use DateTime::Format::ISO8601; | ||
| 4 | with 'Paws::Credential'; | ||
| 5 | |||
| 6 | has expiration => ( | ||
| 7 | is => 'rw', | ||
| 8 | isa => 'Int', | ||
| 9 | lazy => 1, | ||
| 10 | default => sub { 0 } | ||
| 11 | ); | ||
| 12 | |||
| 13 | has actual_creds => (is => 'rw'); | ||
| 14 | |||
| 15 | sub access_key { | ||
| 16 | my $self = shift; | ||
| 17 | $self->_refresh; | ||
| 18 | $self->actual_creds->AccessKeyId; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub secret_key { | ||
| 22 | my $self = shift; | ||
| 23 | $self->_refresh; | ||
| 24 | $self->actual_creds->SecretAccessKey; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub session_token { | ||
| 28 | my $self = shift; | ||
| 29 | $self->_refresh; | ||
| 30 | $self->actual_creds->SessionToken; | ||
| 31 | } | ||
| 32 | |||
| 33 | has sts_region => (is => 'ro', isa => 'Str|Undef', default => sub { undef }); | ||
| 34 | |||
| 35 | has sts => (is => 'ro', isa => 'Paws::STS', lazy => 1, default => sub { | ||
| 36 | my $self = shift; | ||
| 37 | Paws->service('STS', region => $self->sts_region); | ||
| 38 | }); | ||
| 39 | |||
| 40 | has Name => (is => 'rw', isa => 'Str', required => 1); | ||
| 41 | has DurationSeconds => (is => 'rw', isa => 'Maybe[Int]'); | ||
| 42 | has Policy => (is => 'rw', isa => 'Maybe[Str]'); | ||
| 43 | |||
| 44 | sub _refresh { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | return if $self->expiration >= time; | ||
| 48 | |||
| 49 | my $result = $self->sts->GetFederationToken( | ||
| 50 | Name => $self->Name, | ||
| 51 | (defined $self->DurationSeconds) ? (DurationSeconds => $self->DurationSeconds) : (), | ||
| 52 | (defined $self->Policy) ? (Policy => $self->Policy) : (), | ||
| 53 | ); | ||
| 54 | |||
| 55 | my $creds = $self->actual_creds($result->Credentials); | ||
| 56 | $self->expiration(DateTime::Format::ISO8601->parse_datetime($result->Credentials->Expiration)->epoch); | ||
| 57 | } | ||
| 58 | |||
| 59 | no Moose; | ||
| 60 | 1; | ||
| 61 | ### main pod documentation begin ### | ||
| 62 | |||
| 63 | =encoding UTF-8 | ||
| 64 | |||
| 65 | =head1 NAME | ||
| 66 | |||
| 67 | Paws::Credential::STS | ||
| 68 | |||
| 69 | =head1 SYNOPSIS | ||
| 70 | |||
| 71 | use Paws::Credential::STS; | ||
| 72 | |||
| 73 | my $paws = Paws->new(config => { | ||
| 74 | credentials => Paws::Credential::STS->new( | ||
| 75 | sts_region => 'eu-west-1', | ||
| 76 | DurationSeconds => 60, | ||
| 77 | Name => 'MyName', | ||
| 78 | ) | ||
| 79 | }); | ||
| 80 | |||
| 81 | =head1 DESCRIPTION | ||
| 82 | |||
| 83 | The STS provider is used to obtain temporary credentials with the GetFederationToken STS call. These credentials | ||
| 84 | can further be limited by a Policy document. | ||
| 85 | |||
| 86 | Credentials are refreshed with a re-call to STS when they have expired | ||
| 87 | |||
| 88 | =head2 sts_region: Str (optional) | ||
| 89 | |||
| 90 | The STS region to attack. Defaults to the global STS region | ||
| 91 | |||
| 92 | =head2 sts: Paws::STS object (optional) | ||
| 93 | |||
| 94 | A Paws::STS object initialized with some credentials. Defaults to a Paws::STS object initialized with the default | ||
| 95 | credential provider. | ||
| 96 | |||
| 97 | =head2 DurationSeconds: Int (optional) | ||
| 98 | |||
| 99 | The number of seconds for which the credentials will be valid | ||
| 100 | |||
| 101 | =head2 Policy: Str (optional) | ||
| 102 | |||
| 103 | A string with an IAM policy that further restricts the users capabilities | ||
| 104 | |||
| 105 | =head2 Name: Str | ||
| 106 | |||
| 107 | The name of the session (will appear in CloudTrail logs, for example) | ||
| 108 | |||
| 109 | =cut |