| 1 | package Crypt::OdinAuth; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Crypt::OdinAuth - Cryptographic calculations for the OdinAuth SSO system | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | Version 0.2.1 | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 3 | our $VERSION = '0.2.1'; | |||||||||
| 18 | |||||||||||
| 19 | 3 | ▶ | use Digest; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 3 | ▶ | use Digest::HMAC; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 3 | ▶ | use Digest::SHA256; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 3 | ▶ | use MIME::Base64 qw(encode_base64url decode_base64url); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 3 | ▶ | use constant OLD_COOKIE => 24*60*60; # cookie older than 24h is discarded | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | |||||||||||
| 26 | =head1 SYNOPSIS | ||||||||||
| 27 | |||||||||||
| 28 | This module exports functions for calculating and verifying signed | ||||||||||
| 29 | cookies for OdinAuth SSO Apache handler. | ||||||||||
| 30 | |||||||||||
| 31 | use Crypt::OdinAuth; | ||||||||||
| 32 | |||||||||||
| 33 | Crypt::OdinAuth::hmac_for('secret', 'login_name', 'role1,role2,role3', 1337357387, 'netcat') | ||||||||||
| 34 | #=> '349b7135f43bd4c0111564960e7d9d583dde0c5c' | ||||||||||
| 35 | |||||||||||
| 36 | Crypt::OdinAuth::cookie_for('secret', 'login_name', 'role1,role2,role3', 'netcat') | ||||||||||
| 37 | #=> 'login_name-role1,role2,role3-1337357638-7ec415a6816c8e9dab7b788e1262769ef80af7d8' | ||||||||||
| 38 | |||||||||||
| 39 | =head1 SUBROUTINES | ||||||||||
| 40 | |||||||||||
| 41 | =head2 hmac_for(secret, user, roles, timestamp, user_agent) | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | 16 | ▶ | sub hmac_for ($$$$$) { | ||||||||
|
Subroutine
hmac_for: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | 16 | my ( $secret, $user, $roles, $ts, $ua ) = @_; | |||||||||
| 46 | 16 | my $hmac = Digest::HMAC->new($secret, Digest->new("SHA-256")); | |||||||||
| 47 | |||||||||||
| 48 | 16 | $hmac->add(join(',', | |||||||||
| 49 | encode_base64url($user), | ||||||||||
| 50 | encode_base64url($roles), | ||||||||||
| 51 | $ts, | ||||||||||
| 52 | encode_base64url($ua))); | ||||||||||
| 53 | 16 | return $hmac->hexdigest; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | =head2 cookie_for(secret, user, roles, user_agent) | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | 8 | ▶ | sub cookie_for { | ||||||||
|
Subroutine
cookie_for: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 61 | 8 | my ( $secret, $user, $roles, $ua, $ts ) = @_; | |||||||||
| 62 | 8 | ▶ | $ts = time() unless defined($ts); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | |||||||||||
| 64 | 8 | my $hmac = hmac_for($secret, $user, $roles, $ts, $ua); | |||||||||
| 65 | 8 | return join(',', | |||||||||
| 66 | encode_base64url($user), | ||||||||||
| 67 | encode_base64url($roles), | ||||||||||
| 68 | $ts, | ||||||||||
| 69 | $hmac); | ||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | =head2 check_cookie(secret, cookie, user_agent) | ||||||||||
| 73 | |||||||||||
| 74 | =cut | ||||||||||
| 75 | |||||||||||
| 76 | 6 | ▶ | sub check_cookie ($$$) { | ||||||||
|
Subroutine
check_cookie: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 77 | 6 | my ( $secret, $cookie, $ua ) = @_; | |||||||||
| 78 | |||||||||||
| 79 | 6 | ▶ | $cookie =~ /^\s*([a-zA-Z0-9_-]+),([a-zA-Z0-9_-]+),(\d+),([0-9a-f]+)\s*$/ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | or die "Wrong cookie format\n"; | ||||||||||
| 81 | |||||||||||
| 82 | 6 | my $user = decode_base64url($1); | |||||||||
| 83 | 6 | my $roles = decode_base64url($2); | |||||||||
| 84 | 6 | my $ts = $3; | |||||||||
| 85 | 6 | my $hmac = $4; | |||||||||
| 86 | |||||||||||
| 87 | # Use double hmac to prevent timing attacks | ||||||||||
| 88 | # http://codahale.com/a-lesson-in-timing-attacks/ | ||||||||||
| 89 | # http://www.isecpartners.com/blog/2011/2/18/double-hmac-verification.html | ||||||||||
| 90 | 6 | my $hmac_received = Digest::HMAC->new($secret, Digest->new("SHA-256")); | |||||||||
| 91 | 6 | my $hmac_calculated = Digest::HMAC->new($secret, Digest->new("SHA-256")); | |||||||||
| 92 | |||||||||||
| 93 | 6 | $hmac_received->add($hmac); | |||||||||
| 94 | 6 | $hmac_calculated->add(hmac_for($secret, $user, $roles, $ts, $ua)); | |||||||||
| 95 | |||||||||||
| 96 | 6 | ▶ | die "Invalid signature\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | if ( $hmac_received->digest ne $hmac_calculated->digest ); | ||||||||||
| 98 | |||||||||||
| 99 | 4 | ▶ | die "Cookie is old\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | if ( $ts < time() - OLD_COOKIE ); | ||||||||||
| 101 | |||||||||||
| 102 | 3 | ▶ | die "Cookie is in future\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | if ( $ts > time() + 5*60 ); | ||||||||||
| 104 | |||||||||||
| 105 | 2 | return $user, $roles; | |||||||||
| 106 | } | ||||||||||
| 107 | |||||||||||
| 108 | =head1 AUTHOR | ||||||||||
| 109 | |||||||||||
| 110 | Maciej Pasternacki, C<< <maciej at pasternacki.net> >> | ||||||||||
| 111 | |||||||||||
| 112 | =head1 BUGS | ||||||||||
| 113 | |||||||||||
| 114 | Please report any bugs or feature requests to C<bug-apache2-authen-odinauth at rt.cpan.org>, or through | ||||||||||
| 115 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Apache2-Authen-OdinAuth>. I will be notified, and then you'll | ||||||||||
| 116 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 117 | |||||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | |||||||||||
| 121 | =head1 SUPPORT | ||||||||||
| 122 | |||||||||||
| 123 | You can find documentation for this module with the perldoc command. | ||||||||||
| 124 | |||||||||||
| 125 | perldoc Crypt::OdinAuth | ||||||||||
| 126 | |||||||||||
| 127 | |||||||||||
| 128 | You can also look for information at: | ||||||||||
| 129 | |||||||||||
| 130 | =over 4 | ||||||||||
| 131 | |||||||||||
| 132 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 133 | |||||||||||
| 134 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Apache2-Authen-OdinAuth> | ||||||||||
| 135 | |||||||||||
| 136 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 137 | |||||||||||
| 138 | L<http://annocpan.org/dist/Apache2-Authen-OdinAuth> | ||||||||||
| 139 | |||||||||||
| 140 | =item * CPAN Ratings | ||||||||||
| 141 | |||||||||||
| 142 | L<http://cpanratings.perl.org/d/Apache2-Authen-OdinAuth> | ||||||||||
| 143 | |||||||||||
| 144 | =item * Search CPAN | ||||||||||
| 145 | |||||||||||
| 146 | L<http://search.cpan.org/dist/Apache2-Authen-OdinAuth/> | ||||||||||
| 147 | |||||||||||
| 148 | =back | ||||||||||
| 149 | |||||||||||
| 150 | |||||||||||
| 151 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 152 | |||||||||||
| 153 | |||||||||||
| 154 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 155 | |||||||||||
| 156 | Copyright 2012 Maciej Pasternacki. | ||||||||||
| 157 | |||||||||||
| 158 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 159 | under the terms of either: the GNU General Public License as published | ||||||||||
| 160 | by the Free Software Foundation; or the Artistic License. | ||||||||||
| 161 | |||||||||||
| 162 | See http://dev.perl.org/licenses/ for more information. | ||||||||||
| 163 | |||||||||||
| 164 | |||||||||||
| 165 | =cut | ||||||||||
| 166 | |||||||||||
| 167 | 3 | 1; # End of Crypt::OdinAuth | |||||||||