| 1 | package Smolder::AuthInfo; | ||
| 2 | use strict; | ||
| 3 | use Digest::MD5 qw(md5_hex); | ||
| 4 | use Smolder::Conf qw(Secret); | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Smolder::AuthInfo | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Utility class to manage authentication tickets. | ||
| 13 | |||
| 14 | =head1 METHODS | ||
| 15 | |||
| 16 | =head2 new | ||
| 17 | |||
| 18 | Returns a new object. | ||
| 19 | |||
| 20 | my $ai = Smolder::AuthInfo->new(); | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | return bless {}, shift; | ||
| 26 | } | ||
| 27 | |||
| 28 | =head2 ticket | ||
| 29 | |||
| 30 | Return a new auth ticket given an id and groups. | ||
| 31 | |||
| 32 | $ai->ticket( | ||
| 33 | id => $dev->id, | ||
| 34 | groups => [qw(developer admin)], | ||
| 35 | ); | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | sub ticket { | ||
| 40 | my ($self, %args) = @_; | ||
| 41 | my $str = join(',', $args{id}, @{$args{groups}}); | ||
| 42 | return $str . '::::' . md5_hex($str, Secret); | ||
| 43 | } | ||
| 44 | |||
| 45 | =head2 parse | ||
| 46 | |||
| 47 | Parse an auth ticket. The user's id and groups | ||
| 48 | are then available in the C<id()> and C<groups()> methods | ||
| 49 | if the ticket was not tampered with. | ||
| 50 | |||
| 51 | $ai->parse($string); | ||
| 52 | my $id = $at->id; | ||
| 53 | my $groups = $at->groups; | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub parse { | ||
| 58 | my ($self, $str) = @_; | ||
| 59 | my ($data, $digest) = split('::::', $str); | ||
| 60 | if (md5_hex($data, Secret) eq $digest) { | ||
| 61 | my ($id, @groups) = split(',', $data); | ||
| 62 | $self->{id} = $id; | ||
| 63 | $self->{groups} = \@groups; | ||
| 64 | } else { | ||
| 65 | $self->{id} = undef; | ||
| 66 | $self->{groups} = undef; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | =head1 groups | ||
| 71 | |||
| 72 | Returns an array reference containing the groups of the | ||
| 73 | most recently parsed auth ticket. | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | sub groups { | ||
| 78 | return shift->{groups}; | ||
| 79 | } | ||
| 80 | |||
| 81 | =head1 id | ||
| 82 | |||
| 83 | Returns the id of the most recently parsed auth ticket. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | |||
| 87 | sub id { | ||
| 88 | return shift->{id}; | ||
| 89 | } | ||
| 90 | |||
| 91 | 1; |