summaryrefslogtreecommitdiffstats
path: root/Net/OpenID/Association.php
blob: d10139be16f592b655a78bb6a2bc7754c97adf62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php

/**
 * This module contains code for dealing with associations between
 * consumers and servers.
 *
 * PHP versions 4 and 5
 *
 * LICENSE: See the COPYING file included in this distribution.
 *
 * @package OpenID
 * @author JanRain, Inc. <openid@janrain.com>
 * @copyright 2005 Janrain, Inc.
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
 */

/**
 * Includes for utility functions.
 */
require_once('CryptUtil.php');
require_once('KVForm.php');
require_once('OIDUtil.php');

/*
 * This class represents an association between a server and a
 * consumer.  In general, users of this library will never see
 * instances of this object.  The only exception is if you implement a
 * custom Net_OpenID_OpenIDStore.
 *
 * If you do implement such a store, it will need to store the values
 * of the handle, secret, issued, lifetime, and assoc_type instance
 * variables.
*/
class Net_OpenID_Association {

    // This is a HMAC-SHA1 specific value.
    var $SIG_LENGTH = 20;

    // The ordering and name of keys as stored by serialize
    var $assoc_keys = array(
                            'version',
                            'handle',
                            'secret',
                            'issued',
                            'lifetime',
                            'assoc_type'
                            );

    /**
     * This is an alternate constructor used by the OpenID consumer
     * library to create associations.  C{L{OpenIDStore
     * <openid.store.interface.OpenIDStore>}} implementations
     * shouldn't use this constructor.
     *
     * @param integer $expires_in This is the amount of time this
     * association is good for, measured in seconds since the
     * association was issued.
     *
     * @param string $handle This is the handle the server gave this
     * association.
     *
     * @param string secret This is the shared secret the server
     * generated for this association.
     *
     * @param assoc_type: This is the type of association this
     * instance represents.  The only valid value of this field
     * at this time is C{'HMAC-SHA1'}, but new types may be
     * defined in the future.
     */
    function fromExpiresIn($cls, $expires_in, $handle, $secret, $assoc_type) {
        $issued = time();
        $lifetime = $expires_in;
        return $class_name($handle, $secret, $issued, $lifetime, $assoc_type);
    }

    /**
     * This is the standard constructor for creating an association.
     *
     * @param string $handle This is the handle the server gave this
     * association.
     *
     * @param string $secret This is the shared secret the server
     * generated for this association.
     *
     * @param integer $issued This is the time this association was
     * issued, in seconds since 00:00 GMT, January 1, 1970.  (ie, a
     * unix timestamp)
     *
     * @param integer $lifetime This is the amount of time this
     * association is good for, measured in seconds since the
     * association was issued.
     *
     * @param string $assoc_type This is the type of association this
     * instance represents.  The only valid value of this field at
     * this time is C{'HMAC-SHA1'}, but new types may be defined in
     * the future.
     */
    function Net_OpenID_Association(
        $handle, $secret, $issued, $lifetime, $assoc_type) {
        if ($assoc_type != 'HMAC-SHA1') {
            $fmt = 'HMAC-SHA1 is the only supported association type (got %s)';
            trigger_error(sprintf($fmt, $assoc_type), E_USER_ERROR);
        }

        $this->handle = $handle;
        $this->secret = $secret;
        $this->issued = $issued;
        $this->lifetime = $lifetime;
        $this->assoc_type = $assoc_type;
    }

    /**
     * This returns the number of seconds this association is still
     * valid for, or 0 if the association is no longer valid.
     *
     * @return integer $seconds The number of seconds this association
     * is still valid for, or 0 if the association is no longer valid.
     */
    function getExpiresIn($now = null) {
        if ($now == null) {
            $now = time();
        }

        return max(0, $this->issued + $this->lifetime - $now);
    }

    /**
     * This checks to see if two C{L{Association}} instances represent
     * the same association.
     *
     * @return bool $result true if the two instances represent the
     * same association, false otherwise.
     */
    function equal($other) {
        return ((get_type($this) == get_type($other))
                && ($this->handle == $other->handle)
                && ($this->secret == $other->secret)
                && ($this->issued == $other->issued)
                && ($this->lifetime == $other->lifetime)
                && ($this->assoc_type == $other->assoc_type));
    }

    /**
     * This checks to see if two C{L{Association}} instances
     * represent different associations.
     *
     * @return bool $result true if the two instances represent
     * different associations, false otherwise.
     */
    function not_equal($other) {
        return !($this->equal($other));
    }

    /**
     * Convert an association to KV form.
     *
     * @return string $result String in KV form suitable for
     * deserialization by deserialize.
     */
    function serialize() {
        $data = array(
                     'version' => '2',
                     'handle' => $this->handle,
                     'secret' => Net_OpenID_toBase64($this->secret),
                     'issued' => strval(intval($this->issued)),
                     'lifetime' => strval(intval($this->lifetime)),
                     'assoc_type' => $this->assoc_type
                     );

        assert(count($data) == count($this->assoc_keys));

        $pairs = array();
        foreach ($this->assoc_keys as $field_name) {
            $pairs[] = array($field_name, $data[$field_name]);
        }

        return Net_OpenID_KVForm::arrayToKV($pairs, $strict = true);
    }

    /**
     * Parse an association as stored by serialize().  This is the
     * inverse of serialize.
     *
     * @param string $assoc_s Association as serialized by serialize()
     * @return Net_OpenID_Association $result instance of this class
     */
    function deserialize($class_name, $assoc_s) {
        $pairs = Net_OpenID_KVForm::kvToArray($assoc_s, $strict = true);
        $keys = array();
        $values = array();
        foreach ($pairs as $key => $value) {
            if (is_array($value)) {
                list($key, $value) = $value;
            }
            $keys[] = $key;
            $values[] = $value;
        }

        $class_vars = get_class_vars($class_name);
        $class_assoc_keys = $class_vars['assoc_keys'];
        if ($keys != $class_assoc_keys) {
            trigger_error('Unexpected key values: ' . strval($keys),
                          E_USER_WARNING);
            return null;
        }

        list($version, $handle, $secret, $issued, $lifetime, $assoc_type) =
            $values;

        if ($version != '2') {
            trigger_error('Unknown version: ' . $version, E_USER_WARNING);
            return null;
        }

        $issued = intval($issued);
        $lifetime = intval($lifetime);
        $secret = Net_OpenID_fromBase64($secret);

        return new $class_name(
            $handle, $secret, $issued, $lifetime, $assoc_type);
    }

    /**
     * Generate a signature for a sequence of (key, value) pairs
     *
     * @param array $pairs The pairs to sign, in order.  This is an
     * array of two-tuples.
     * @return string $signature The binary signature of this sequence
     * of pairs
     */
    function sign($pairs) {
        assert($this->assoc_type == 'HMAC-SHA1');
        $kv = Net_OpenID_arrayToKV($pairs);
        return Net_OpenID_hmacSha1($this->secret, $kv);
    }

    /**
     * Generate a signature for some fields in a dictionary
     *
     * @param array $fields The fields to sign, in order; this is an
     * array of strings.
     * @param array $data Dictionary of values to sign (an array of
     * string => string pairs).
     * @return string $signature The signature, base64 encoded
     */
    function signDict($fields, $data, $prefix = 'openid.') {
        $pairs = array();
        foreach ($fields as $field) {
            $pairs[] = array($field, $data[$prefix . $field]);
        }

        return Net_OpenID_toBase64($this->sign($pairs));
    }

    function addSignature($fields, $data, $prefix = 'openid.') {
        $sig = self.signDict($fields, $data, $prefix);
        $signed = implode(",", $fields);
        $data[$prefix . 'sig'] = $sig;
        $data[$prefix . 'signed'] = $signed;
    }

    function checkSignature($data, $prefix = 'openid.') {
        $signed = $data[$prefix . 'signed'];
        $fields = explode(",", $signed);
        $expected_sig = $this->signDict($fields, $data, $prefix);
        $request_sig = $data[$prefix . 'sig'];

        return ($request_sig == $expected_sig);
    }
}

?>