4 * Nonce-related functionality.
10 * Need CryptUtil to generate random strings.
12 require_once 'Auth/OpenID/CryptUtil.php';
15 * This is the characters that the nonces are made from.
17 define('Auth_OpenID_Nonce_CHRS',"abcdefghijklmnopqrstuvwxyz" .
18 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
20 // Keep nonces for five hours (allow five hours for the combination of
21 // request time and clock skew). This is probably way more than is
22 // necessary, but there is not much overhead in storing nonces.
23 global $Auth_OpenID_SKEW;
24 $Auth_OpenID_SKEW = 60 * 60 * 5;
26 define('Auth_OpenID_Nonce_REGEX',
27 '/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z(.*)/');
29 define('Auth_OpenID_Nonce_TIME_FMT',
30 '%Y-%m-%dT%H:%M:%SZ');
32 function Auth_OpenID_splitNonce($nonce_string)
34 // Extract a timestamp from the given nonce string
35 $result = preg_match(Auth_OpenID_Nonce_REGEX, $nonce_string, $matches);
36 if ($result != 1 || count($matches) != 8) {
47 $uniquifier) = $matches;
50 @gmmktime($tm_hour, $tm_min, $tm_sec, $tm_mon, $tm_mday, $tm_year);
52 if ($timestamp === false || $timestamp < 0) {
56 return array($timestamp, $uniquifier);
59 function Auth_OpenID_checkTimestamp($nonce_string,
63 // Is the timestamp that is part of the specified nonce string
64 // within the allowed clock-skew of the current time?
65 global $Auth_OpenID_SKEW;
67 if ($allowed_skew === null) {
68 $allowed_skew = $Auth_OpenID_SKEW;
71 $parts = Auth_OpenID_splitNonce($nonce_string);
82 // Time after which we should not use the nonce
83 $past = $now - $allowed_skew;
85 // Time that is too far in the future for us to allow
86 $future = $now + $allowed_skew;
88 // the stamp is not too far in the future and is not too far
90 return (($past <= $stamp) && ($stamp <= $future));
93 function Auth_OpenID_mkNonce($when = null)
95 // Generate a nonce with the current timestamp
96 $salt = Auth_OpenID_CryptUtil::randomString(
97 6, Auth_OpenID_Nonce_CHRS);
99 // It's safe to call time() with no arguments; it returns a
100 // GMT unix timestamp on PHP 4 and PHP 5. gmmktime() with no
101 // args returns a local unix timestamp on PHP 4, so don't use
105 $time_str = gmstrftime(Auth_OpenID_Nonce_TIME_FMT, $when);
106 return $time_str . $salt;