aboutsummaryrefslogblamecommitdiffstats
path: root/library/openid/provider/provider.php
blob: 03fbe1c81d294e67d0de0d0805e1a74a16c837cf (plain) (tree)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845












































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                       
<?php
/**
 * Using this class, you can easily set up an OpenID Provider.
 * It's independent of LightOpenID class.
 * It requires either GMP or BCMath for session encryption, 
 * but will work without them (although either via SSL, or in stateless mode only).
 * Also, it requires PHP >= 5.1.2
 * 
 * This is an alpha version, using it in production code is not recommended,
 * until you are *sure* that it works and is secure.
 *
 * Please send me messages about your testing results 
 * (even if successful, so I know that it has been tested).
 * Also, if you think there's a way to make it easier to use, tell me -- it's an alpha for a reason.
 * Same thing applies to bugs in code, suggestions, 
 * and everything else you'd like to say about the library.
 *
 * There's no usage documentation here, see the examples.
 *
 * @author Mewp
 * @copyright Copyright (c) 2010, Mewp
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 */
ini_set('error_log','log');
abstract class LightOpenIDProvider
{
    # URL-s to XRDS and server location.
    public $xrdsLocation, $serverLocation;
    
    # Should we operate in server, or signon mode?
    public $select_id = false;
    
    # Lifetime of an association.
    protected $assoc_lifetime = 600;
    
    # Variables below are either set automatically, or are constant.
    # -----
    # Can we support DH?
    protected $dh = true;
    protected $ns = 'http://specs.openid.net/auth/2.0';
    protected $data, $assoc;
    
    # Default DH parameters as defined in the specification.
    protected $default_modulus;
    protected $default_gen = 'Ag==';
    
    # AX <-> SREG transform
    protected $ax_to_sreg = array(
        'namePerson/friendly'     => 'nickname',
        'contact/email'           => 'email',
        'namePerson'              => 'fullname',
        'birthDate'               => 'dob',
        'person/gender'           => 'gender',
        'contact/postalCode/home' => 'postcode',
        'contact/country/home'    => 'country',
        'pref/language'           => 'language',
        'pref/timezone'           => 'timezone',
        );
    
    # Math
    private $add, $mul, $pow, $mod, $div, $powmod;
    # -----
    
    # ------------------------------------------------------------------------ #
    #  Functions you probably want to implement when extending the class.
    
    /**
     * Checks whether an user is authenticated.
     * The function should determine what fields it wants to send to the RP, 
     * and put them in the $attributes array.
     * @param Array $attributes
     * @param String $realm Realm used for authentication.
     * @return String OP-local identifier of an authenticated user, or an empty value.
     */
    abstract function checkid($realm, &$attributes);
    
    /**
     * Displays an user interface for inputting user's login and password.
     * Attributes are always AX field namespaces, with stripped host part.
     * For example, the $attributes array may be:
     * array( 'required' => array('namePerson/friendly', 'contact/email'),
     *        'optional' => array('pref/timezone', 'pref/language')
     * @param String $identity Discovered identity string. May be used to extract login, unless using $this->select_id
     * @param String $realm Realm used for authentication.
     * @param String Association handle. must be sent as openid.assoc_handle in $_GET or $_POST in subsequent requests.
     * @param Array User attributes requested by the RP.
     */
    abstract function setup($identity, $realm, $assoc_handle, $attributes);
    
    /**
     * Stores an association.
     * If you want to use php sessions in your provider code, you have to replace it.
     * @param String $handle Association handle -- should be used as a key.
     * @param Array $assoc Association data.
     */
    protected function setAssoc($handle, $assoc)
    {
        $oldSession = session_id();
        session_commit();
        session_id($assoc['handle']);
        session_start();
        $_SESSION['assoc'] = $assoc;
        session_commit();
        if($oldSession) {
            session_id($oldSession);
            session_start();
        }
    }
    
    /**
     * Retreives association data.
     * If you want to use php sessions in your provider code, you have to replace it.
     * @param String $handle Association handle.
     * @return Array Association data.
     */
    protected function getAssoc($handle)
    {
        $oldSession = session_id();
        session_commit();
        session_id($handle);
        session_start();
        if(empty($_SESSION['assoc'])) {
            return null;
        }
        return $_SESSION['assoc'];
        session_commit();
        if($oldSession) {
            session_id($oldSession);
            session_start();
        }
    }
    
    /**
     * Deletes an association.
     * If you want to use php sessions in your provider code, you have to replace it.
     * @param String $handle Association handle.
     */
    protected function delAssoc($handle)
    {
        $oldSession = session_id();
        session_commit();
        session_id($handle);
        session_start();
        session_destroy();
        if($oldSession) {
            session_id($oldSession);
            session_start();
        }
    }
    
    # ------------------------------------------------------------------------ #
    # Functions that you might want to implement.
    
    /**
     * Redirects the user to an url.
     * @param String $location The url that the user will be redirected to.
     */
    protected function redirect($location)
    {
        header('Location: ' . $location);
        die();
    }
    
    /**
     * Generates a new association handle.
     * @return string
     */
    protected function assoc_handle()
    {
        return sha1(microtime());
    }
    
    /**
     * Generates a random shared secret.
     * @return string
     */
    protected function shared_secret($hash)
    {
        $length = 20;
        if($hash == 'sha256') {
            $length = 256;
        }
        
        $secret = '';
        for($i = 0; $i < $length; $i++) {
            $secret .= mt_rand(0,255);
        }
        
        return $secret;
    }
    
    /**
     * Generates a private key.
     * @param int $length Length of the key.
     */
    protected function keygen($length)
    {
        $key = '';
        for($i = 1; $i < $length; $i++) {
            $key .= mt_rand(0,9);
        }
        $key .= mt_rand(1,9);
        
        return $key;
    }
    
    # ------------------------------------------------------------------------ #
    # Functions that you probably shouldn't touch.
    
    function __construct()
    {
        $this->default_modulus = 
            'ANz5OguIOXLsDhmYmsWizjEOHTdxfo2Vcbt2I3MYZuYe91ouJ4mLBX+YkcLiemOcPy'
          . 'm2CBRYHNOyyjmG0mg3BVd9RcLn5S3IHHoXGHblzqdLFEi/368Ygo79JRnxTkXjgmY0'
          . 'rxlJ5bU1zIKaSDuKdiI+XUkKJX8Fvf8W8vsixYOr';

        $location = (!empty($_SERVER['HTTPS']) ? 'https' : 'http') . '://'
                  . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $location = preg_replace('/\?.*/','',$location);
        $this->serverLocation = $location;
        $location .= (strpos($location, '?') ? '&' : '?') . 'xrds';
        $this->xrdsLocation = $location;
        
        $this->data = $_GET + $_POST;
        
        # We choose GMP if avaiable, and bcmath otherwise
        if(function_exists('gmp_add')) {
            $this->add = 'gmp_add';
            $this->mul = 'gmp_mul';
            $this->pow = 'gmp_pow';
            $this->mod = 'gmp_mod';
            $this->div = 'gmp_div';
            $this->powmod = 'gmp_powm';
        } elseif(function_exists('bcadd')) {
            $this->add = 'bcadd';
            $this->mul = 'bcmul';
            $this->pow = 'bcpow';
            $this->mod = 'bcmod';
            $this->div = 'bcdiv';
            $this->powmod = 'bcpowmod';
        } else {
            # If neither are avaiable, we can't use DH
            $this->dh = false;
        }
        
        # However, we do require the hash functions.
        # They should be built-in anyway.
        if(!function_exists('hash_algos')) {
            $this->dh = false;
        }
    }
    
    /**
     * Displays an XRDS document, or redirects to it.
     * By default, it detects whether it should display or redirect automatically.
     * @param bool|null $force When true, always display the document, when false always redirect.
     */
    function xrds($force=null)
    {
        if($force) {
            echo $this->xrdsContent();
            die();
        } elseif($force === false) {
            header('X-XRDS-Location: '. $this->xrdsLocation);
            return;
        }
        
        if (isset($_GET['xrds'])
            || (isset($_SERVER['HTTP_ACCEPT']) &&  strpos($_SERVER['HTTP_ACCEPT'], 'application/xrds+xml') !== false)
        ) {
            header('Content-Type: application/xrds+xml');
            echo $this->xrdsContent();
            die();
        }
        
        header('X-XRDS-Location: ' . $this->xrdsLocation);
    }
    
    /**
     * Returns the content of the XRDS document
     * @return String The XRDS document.
     */
    protected function xrdsContent()
    {
        $lines = array(
            '<?xml version="1.0" encoding="UTF-8"?>',
            '<xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)">',
            '<XRD>',
            '    <Service>',
            '        <Type>' . $this->ns . '/' . ($this->select_id ? 'server' : 'signon') .'</Type>',
            '        <URI>' . $this->serverLocation . '</URI>',
            '    </Service>',
            '</XRD>',
            '</xrds:XRDS>'
            );
        return implode("\n", $lines);
    }
    
    /**
     * Does everything that a provider has to -- in one function.
     */
    function server()
    {
        if(isset($this->data['openid_assoc_handle'])) {
            $this->assoc = $this->getAssoc($this->data['openid_assoc_handle']);
            if(isset($this->assoc['data'])) {
                # We have additional data stored for setup.
                $this->data += $this->assoc['data'];
                unset($this->assoc['data']);
            }
        }
            
        if (isset($this->data['openid_ns'])
            && $this->data['openid_ns'] == $this->ns
        ) {
            if(!isset($this->data['openid_mode'])) $this->errorResponse();
            
            switch($this->data['openid_mode'])
            { 
            case 'checkid_immediate':
            case 'checkid_setup':
                $this->checkRealm();
                # We support AX xor SREG.
                $attributes = $this->ax();
                if(!$attributes) {
                    $attributes = $this->sreg();
                }
                
                # Even if some user is authenticated, we need to know if it's
                # the same one that want's to authenticate.
                # Of course, if we use select_id, we accept any user.
                if (($identity = $this->checkid($this->data['openid_realm'], $attrValues))
                    && ($this->select_id || $identity == $this->data['openid_identity'])
                ) {
                    $this->positiveResponse($identity, $attrValues);
                } elseif($this->data['openid_mode'] == 'checkid_immediate') {
                    $this->redirect($this->response(array('openid.mode' => 'setup_needed')));
                } else {
                    if(!$this->assoc) {
                        $this->generateAssociation();
                        $this->assoc['private'] = true;
                    }
                    $this->assoc['data'] = $this->data;
                    $this->setAssoc($this->assoc['handle'], $this->assoc);
                    $this->setup($this->data['openid_identity'],
                                 $this->data['openid_realm'],
                                 $this->assoc['handle'],
                                 $attributes);
                }
                break;
            case 'associate':
                $this->associate();
                break;
            case 'check_authentication':
                $this->checkRealm();
                if($this->verify()) {
                    echo "ns:$this->ns\nis_valid:true";
                    if(strpos($this->data['openid_signed'],'invalidate_handle') !== false) {
                        echo "\ninvalidate_handle:" . $this->data['openid_invalidate_handle'];
                    }
                } else {
                    echo "ns:$this->ns\nis_valid:false";
                }
                die();
                break;
            default:
                $this->errorResponse();
            }
        } else {
            $this->xrds();
        }
    }
    
    protected function checkRealm()
    {
        if (!isset($this->data['openid_return_to'], $this->data['openid_realm'])) {
            $this->errorResponse();
        }
        
        $realm = str_replace('\*', '[^/]', preg_quote($this->data['openid_realm']));
        if(!preg_match("#^$realm#", $this->data['openid_return_to'])) {
            $this->errorResponse();
        }
    }
    
    protected function ax()
    {
        # Namespace prefix that the fields must have.
        $ns = 'http://axschema.org/';
        
        # First, we must find out what alias is used for AX.
        # Let's check the most likely one
        $alias = null;
        if (isset($this->data['openid_ns_ax'])
            && $this->data['openid_ns_ax'] == 'http://openid.net/srv/ax/1.0'
        ) {
            $alias = 'ax';
        } else {
            foreach($this->data as $name => $value) {
                if ($value == 'http://openid.net/srv/ax/1.0'
                    && preg_match('/openid_ns_(.+)/', $name, $m)
                ) {
                    $alias = $m[1];
                    break;
                }
            }
        }
        
        if(!$alias) {
            return null;
        }
        
        $fields = array();
        # Now, we must search again, this time for field aliases
        foreach($this->data as $name => $value) {
            if (strpos($name, 'openid_' . $alias . '_type') === false
                || strpos($value, $ns) === false) {
                continue;
            }
            
            $name = substr($name, strlen('openid_' . $alias . '_type_'));
            $value = substr($value, strlen($ns));
            
            $fields[$name] = $value;
        }
        
        # Then, we find out what fields are required and optional
        $required = array();
        $if_available = array();
        foreach(array('required','if_available') as $type) {
            if(empty($this->data["openid_{$alias}_{$type}"])) {
                continue;
            }
            $attributes = explode(',', $this->data["openid_{$alias}_{$type}"]);
            foreach($attributes as $attr) {
                if(empty($fields[$attr])) {
                    # There is an undefined field here, so we ignore it.
                    continue;
                }
                
                ${$type}[] = $fields[$attr];
            }
        }
        
        $this->data['ax'] = true;
        return array('required' => $required, 'optional' => $if_available);
    }
    
    protected function sreg()
    {
        $sreg_to_ax = array_flip($this->ax_to_sreg);
        
        $attributes = array('required' => array(), 'optional' => array());
        
        if (empty($this->data['openid_sreg_required'])
            && empty($this->data['openid_sreg_optional'])
        ) {
            return $attributes;
        }
        
        foreach(array('required', 'optional') as $type) {
            foreach(explode(',',$this->data['openid_sreg_' . $type]) as $attr) {
                if(empty($sreg_to_ax[$attr])) {
                    # Undefined attribute in SREG request.
                    # Shouldn't happen, but we check anyway.
                    continue;
                }
                
                $attributes[$type][] = $sreg_to_ax[$attr];
            }
        }
        
        return $attributes;
    }
    
    /**
     * Aids an RP in assertion verification.
     * @return bool Information whether the verification suceeded.
     */
    protected function verify()
    {
        # Firstly, we need to make sure that there's an association.
        # Otherwise the verification will fail, 
        # because we've signed assoc_handle in the assertion
        if(empty($this->assoc)) {
            return false;
        }
        
        # Next, we check that it's a private association, 
        # i.e. one made without RP input.
        # Otherwise, the RP shouldn't ask us to verify.
        if(empty($this->assoc['private'])) {
            return false;
        }
        
        # Now we have to check if the nonce is correct, to prevent replay attacks.
        if($this->data['openid_response_nonce'] != $this->assoc['nonce']) {
            return false;
        }
        
        # Getting the signed fields for signature.
        $sig = array();
        $signed = explode(',', $this->data['openid_signed']);
        foreach($signed as $field) {
            $name = strtr($field, '.', '_');
            if(!isset($this->data['openid_' . $name])) {
                return false;
            }
            
            $sig[$field] = $this->data['openid_' . $name];
        }
        
        # Computing the signature and checking if it matches.
        $sig = $this->keyValueForm($sig);
        if ($this->data['openid_sig'] != 
            base64_encode(hash_hmac($this->assoc['hash'], $sig, $this->assoc['mac'], true))
        ) {
            return false;
        }
        
        # Clearing the nonce, so that it won't be used again.
        $this->assoc['nonce'] = null;
        
        if(empty($this->assoc['private'])) {
            # Commiting changes to the association.
            $this->setAssoc($this->assoc['handle'], $this->assoc);
        } else {
            # Private associations shouldn't be used again, se we can as well delete them.
            $this->delAssoc($this->assoc['handle']);
        }
        
        # Nothing has failed, so the verification was a success.
        return true;
    }
    
    /**
     * Performs association with an RP.
     */
    protected function associate()
    {
        # Rejecting no-encryption without TLS.
        if(empty($_SERVER['HTTPS']) && $this->data['openid_session_type'] == 'no-encryption') {
            $this->directErrorResponse();
        }
        
        # Checking whether we support DH at all.
        if (!$this->dh && substr($this->data['openid_session_type'], 0, 2) == 'DH') {
            $this->redirect($this->response(array(
                'openid.error' => 'DH not supported',
                'openid.error_code' => 'unsupported-type',
                'openid.session_type' => 'no-encryption'
                )));
        }
        
        # Creating the association
        $this->assoc = array();
        $this->assoc['hash'] = $this->data['openid_assoc_type'] == 'HMAC-SHA256' ? 'sha256' : 'sha1';
        $this->assoc['handle'] = $this->assoc_handle();
        
        # Getting the shared secret
        if($this->data['openid_session_type'] == 'no-encryption') {
            $this->assoc['mac'] = base64_encode($this->shared_secret($this->assoc['hash']));
        } else {
            $this->dh();
        }
        
        # Preparing the direct response...
        $response = array(
            'ns'           => $this->ns,
            'assoc_handle' => $this->assoc['handle'],
            'assoc_type'   => $this->data['openid_assoc_type'],
            'session_type' => $this->data['openid_session_type'],
            'expires_in'   => $this->assoc_lifetime
            );
        
        if(isset($this->assoc['dh_server_public'])) {
            $response['dh_server_public'] = $this->assoc['dh_server_public'];
            $response['enc_mac_key'] = $this->assoc['mac'];
        } else {
            $response['mac_key'] = $this->assoc['mac'];
        }
        
        # ...and sending it.
        echo $this->keyValueForm($response);
        die();
    }
    
    /**
     * Creates a private association.
     */
    protected function generateAssociation()
    {
        $this->assoc = array();
        # We use sha1 by default.
        $this->assoc['hash']   = 'sha1';
        $this->assoc['mac']    = $this->shared_secret('sha1');
        $this->assoc['handle'] = $this->assoc_handle();
    }
    
    /**
     * Encrypts the MAC key using DH key exchange.
     */
    protected function dh()
    {
        if(empty($this->data['openid_dh_modulus'])) {
            $this->data['openid_dh_modulus'] = $this->default_modulus;
        }
        
        if(empty($this->data['openid_dh_gen'])) {
            $this->data['openid_dh_gen'] = $this->default_gen;
        }
        
        if(empty($this->data['openid_dh_consumer_public'])) {
            $this->directErrorResponse();
        }
        
        $modulus = $this->b64dec($this->data['openid_dh_modulus']);
        $gen = $this->b64dec($this->data['openid_dh_gen']);
        $consumerKey = $this->b64dec($this->data['openid_dh_consumer_public']);
        
        $privateKey = $this->keygen(strlen($modulus));
        $publicKey = $this->powmod($gen, $privateKey, $modulus);
        $ss = $this->powmod($consumerKey, $privateKey, $modulus);
        
        $mac = $this->x_or(hash($this->assoc['hash'], $ss, true), $this->shared_secret($this->assoc['hash']));
        $this->assoc['dh_server_public'] = $this->decb64($publicKey);
        $this->assoc['mac'] = base64_encode($mac);
    }
    
    /**
     * XORs two strings.
     * @param String $a
     * @param String $b
     * @return String $a ^ $b
     */
    protected function x_or($a, $b)
    {
        $length = strlen($a);
        for($i = 0; $i < $length; $i++) {
            $a[$i] = $a[$i] ^ $b[$i];
        }
        
        return $a;
    }
    
    /**
     * Prepares an indirect response url.
     * @param array $params Parameters to be sent.
     */
    protected function response($params)
    {
        $params += array('openid.ns' => $this->ns);
        return $this->data['openid_return_to']
             . (strpos($this->data['openid_return_to'],'?') ? '&' : '?')
             . http_build_query($params, '', '&');
    }
    
    /**
     * Outputs a direct error.
     */
    protected function errorResponse()
    {
        if(!empty($this->data['openid_return_to'])) {
            $response = array(
            'openid.mode'  => 'error',
            'openid.error' => 'Invalid request'
            );
            $this->redirect($this->response($response));
        } else {
            header('HTTP/1.1 400 Bad Request');
            $response = array(
                'ns' => $this->ns,
                'error' => 'Invalid request'
                );
            echo $this->keyValueForm($response);
        }
        die();
    }
    
    /**
     * Sends an positive assertion.
     * @param String $identity the OP-Local Identifier that is being authenticated.
     * @param Array $attributes User attributes to be sent.
     */
    protected function positiveResponse($identity, $attributes)
    {
        # We generate a private association if there is none established.
        if(!$this->assoc) {
            $this->generateAssociation();
            $this->assoc['private'] = true;
        }
        
        # We set openid.identity (and openid.claimed_id if necessary) to our $identity
        if($this->data['openid_identity'] == $this->data['openid_claimed_id'] || $this->select_id) {
            $this->data['openid_claimed_id'] = $identity;
        }
        $this->data['openid_identity'] = $identity;
        
        # Preparing fields to be signed
        $params = array(
            'op_endpoint'    => $this->serverLocation,
            'claimed_id'     => $this->data['openid_claimed_id'],
            'identity'       => $this->data['openid_identity'],
            'return_to'      => $this->data['openid_return_to'],
            'realm'          => $this->data['openid_realm'],
            'response_nonce' => gmdate("Y-m-d\TH:i:s\Z"),
            'assoc_handle'   => $this->assoc['handle'],
            );
        
        $params += $this->responseAttributes($attributes);
        
        # Has the RP used an invalid association handle?
        if (isset($this->data['openid_assoc_handle'])
            && $this->data['openid_assoc_handle'] != $this->assoc['handle']
        ) {
            $params['invalidate_handle'] = $this->data['openid_assoc_handle'];
        }
        
        # Signing the $params
        $sig = hash_hmac($this->assoc['hash'], $this->keyValueForm($params), $this->assoc['mac'], true);
        $req = array(
            'openid.mode'   => 'id_res',
            'openid.signed' => implode(',', array_keys($params)),
            'openid.sig'    => base64_encode($sig),
            );
        
        # Saving the nonce and commiting the association.
        $this->assoc['nonce'] = $params['response_nonce'];
        $this->setAssoc($this->assoc['handle'], $this->assoc);
        
        # Preparing and sending the response itself
        foreach($params as $name => $value) {
            $req['openid.' . $name] = $value;
        }
        
        $this->redirect($this->response($req));
    }
    
    /**
     * Prepares an array of attributes to send
     */
    protected function responseAttributes($attributes)
    {
        if(!$attributes) return array();
        
        $ns = 'http://axschema.org/';

        $response = array();
        if(isset($this->data['ax'])) {
            $response['ns.ax'] = 'http://openid.net/srv/ax/1.0';
            foreach($attributes as $name => $value) {
                $alias = strtr($name, '/', '_');
                $response['ax.type.' . $alias] = $ns . $name;
                $response['ax.value.' . $alias] = $value;
            }
            return $response;
        }
        
        foreach($attributes as $name => $value) {
            if(!isset($this->ax_to_sreg[$name])) {
                continue;
            }
            
            $response['sreg.' . $this->ax_to_sreg[$name]] = $value;
        }
        return $response;
    }
    
    /**
     * Encodes fields in key-value form.
     * @param Array $params Fields to be encoded.
     * @return String $params in key-value form.
     */
    protected function keyValueForm($params)
    {
        $str = '';
        foreach($params as $name => $value) {
            $str .= "$name:$value\n";
        }
        
        return $str;
    }
    
    /**
     * Responds with an information that the user has canceled authentication.
     */
    protected function cancel()
    {
        $this->redirect($this->response(array('openid.mode' => 'cancel')));
    }
    
    /**
     * Converts base64 encoded number to it's decimal representation.
     * @param String $str base64 encoded number.
     * @return String Decimal representation of that number.
     */
    protected function b64dec($str)
    {
        $bytes = unpack('C*', base64_decode($str));
        $n = 0;
        foreach($bytes as $byte) {
            $n = $this->add($this->mul($n, 256), $byte);
        }
        
        return $n;
    }
    
    /**
     * Complements b64dec.
     */
    protected function decb64($num)
    {
        $bytes = array();
        while($num) {
            array_unshift($bytes, $this->mod($num, 256));
            $num = $this->div($num, 256);
        }
        
        if($bytes && $bytes[0] > 127) {
            array_unshift($bytes,0);
        }
        
        array_unshift($bytes, 'C*');
        
        return base64_encode(call_user_func_array('pack', $bytes));
    }
    
    function __call($name, $args)
    {
        switch($name) {
        case 'add':
        case 'mul':
        case 'pow':
        case 'mod':
        case 'div':
        case 'powmod':
            if(function_exists('gmp_strval')) {
                return gmp_strval(call_user_func_array($this->$name, $args));
            }
            return call_user_func_array($this->$name, $args);
        default:
            throw new BadMethodCallException();
        }
    }
}