]> git.sesse.net Git - www-csrf/blobdiff - lib/WWW/CSRF.pm
Change the return value to be more detailed when something fails. (API break, but...
[www-csrf] / lib / WWW / CSRF.pm
index 77d8e797cb05b352fa5247878b48cddb99e6efc1..095073382ec5d9486c50075f0f3933241a3e268d 100644 (file)
@@ -1,13 +1,19 @@
+package WWW::CSRF;
 
 use strict;
 use warnings;
 use Bytes::Random::Secure;
 use Digest::HMAC_SHA1;
+use constant {
+       CSRF_OK => 0,
+       CSRF_MALFORMED_TOKEN => 1,
+       CSRF_INVALID_SIGNATURE => 2,
+       CSRF_EXPIRED => 3,
+};
 
-package WWW::CSRF;
 require Exporter;
 our @ISA = qw(Exporter);
-our @EXPORT_OK = qw(generate_csrf_token check_csrf_token);
+our @EXPORT_OK = qw(generate_csrf_token check_csrf_token CSRF_OK CSRF_MALFORMED_TOKEN CSRF_INVALID_SIGNATURE CSRF_EXPIRED);
 our $VERSION = '1.00';
 
 sub generate_csrf_token {
@@ -41,8 +47,7 @@ sub check_csrf_token {
        my ($id, $secret, $csrf_token, $options) = @_;
 
        if ($csrf_token !~ /^([0-9a-f]+),([0-9a-f]+),([0-9]+)$/) {
-               # Malformed token.
-               return 0;
+               return CSRF_MALFORMED_TOKEN;
        }
 
        my $ref_time = $options->{'Time'} // time;
@@ -50,8 +55,7 @@ sub check_csrf_token {
        my ($masked_token, $mask, $time) = ($1, $2, $3);
        my $max_age = $options->{'MaxAge'};
        if (defined($max_age) && $ref_time - $time > $max_age) {
-               # Timed out.
-               return 0;
+               return CSRF_EXPIRED;
        }
 
        my @masked_bytes = _to_byte_array(pack('H*', $masked_token));
@@ -62,7 +66,7 @@ sub check_csrf_token {
 
        if ($#masked_bytes != $#mask_bytes || $#masked_bytes != $#correct_bytes) {
                # Malformed token (wrong number of characters).
-               return 0;
+               return CSRF_MALFORMED_TOKEN;
        }
 
        # Compare in a way that should make timing attacks hard.
@@ -70,7 +74,11 @@ sub check_csrf_token {
        for my $i (0..$#masked_bytes) {
                $mismatches += $masked_bytes[$i] ^ $mask_bytes[$i] ^ $correct_bytes[$i];
        }
-       return ($mismatches == 0);
+       if ($mismatches == 0) {
+               return CSRF_OK;
+       } else {
+               return CSRF_INVALID_SIGNATURE;
+       }
 }
 
 # Converts each byte in the given string to its numeric value,