1: <?php
2:
3: 4: 5: 6: 7:
8:
9: 10: 11:
12: require_once 'Auth/OpenID/CryptUtil.php';
13:
14: 15: 16:
17: define('Auth_OpenID_Nonce_CHRS',"abcdefghijklmnopqrstuvwxyz" .
18: "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
19:
20:
21:
22:
23: global $Auth_OpenID_SKEW;
24: $Auth_OpenID_SKEW = 60 * 60 * 5;
25:
26: define('Auth_OpenID_Nonce_REGEX',
27: '/(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z(.*)/');
28:
29: define('Auth_OpenID_Nonce_TIME_FMT',
30: '%Y-%m-%dT%H:%M:%SZ');
31:
32: function Auth_OpenID_splitNonce($nonce_string)
33: {
34:
35: $result = preg_match(Auth_OpenID_Nonce_REGEX, $nonce_string, $matches);
36: if ($result != 1 || count($matches) != 8) {
37: return null;
38: }
39:
40: list($unused,
41: $tm_year,
42: $tm_mon,
43: $tm_mday,
44: $tm_hour,
45: $tm_min,
46: $tm_sec,
47: $uniquifier) = $matches;
48:
49: $timestamp =
50: @gmmktime($tm_hour, $tm_min, $tm_sec, $tm_mon, $tm_mday, $tm_year);
51:
52: if ($timestamp === false || $timestamp < 0) {
53: return null;
54: }
55:
56: return array($timestamp, $uniquifier);
57: }
58:
59: function Auth_OpenID_checkTimestamp($nonce_string,
60: $allowed_skew = null,
61: $now = null)
62: {
63:
64:
65: global $Auth_OpenID_SKEW;
66:
67: if ($allowed_skew === null) {
68: $allowed_skew = $Auth_OpenID_SKEW;
69: }
70:
71: $parts = Auth_OpenID_splitNonce($nonce_string);
72: if ($parts == null) {
73: return false;
74: }
75:
76: if ($now === null) {
77: $now = time();
78: }
79:
80: $stamp = $parts[0];
81:
82:
83: $past = $now - $allowed_skew;
84:
85:
86: $future = $now + $allowed_skew;
87:
88:
89:
90: return (($past <= $stamp) && ($stamp <= $future));
91: }
92:
93: function Auth_OpenID_mkNonce($when = null)
94: {
95:
96: $salt = Auth_OpenID_CryptUtil::randomString(
97: 6, Auth_OpenID_Nonce_CHRS);
98: if ($when === null) {
99:
100:
101:
102:
103: $when = time();
104: }
105: $time_str = gmstrftime(Auth_OpenID_Nonce_TIME_FMT, $when);
106: return $time_str . $salt;
107: }
108:
109: