OpenIdCredentials.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Anton E. Lebedevich                             *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: OpenIdCredentials.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class OpenIdCredentials
00017     {
00018         private $claimedId  = null;
00019         private $realId     = null;
00020         private $server     = null;
00021         private $httpClient = null;
00022         
00023         public function __construct(
00024             HttpUrl $claimedId,
00025             HttpClient $httpClient
00026         )
00027         {
00028             $this->claimedId = $claimedId->makeComparable();
00029             
00030             if (!$claimedId->isValid())
00031                 throw new OpenIdException('invalid claimed id');
00032             
00033             $this->httpClient = $httpClient;
00034             
00035             $response = $httpClient->send(
00036                 HttpRequest::create()->
00037                 setMethod(HttpMethod::get())->
00038                 setUrl($claimedId)
00039             );
00040             
00041             if ($response->getStatus()->getId() != 200) {
00042                 throw new OpenIdException('can\'t fetch document');
00043             }
00044             
00045             $tokenizer = HtmlTokenizer::create(
00046                     StringInputStream::create($response->getBody())
00047                 )->
00048                 lowercaseTags(true)->
00049                 lowercaseAttributes(true);
00050             
00051             $insideHead = false;
00052             while ($token = $tokenizer->nextToken()) {
00053                 if (!$insideHead) {
00054                     if ($token instanceof SgmlOpenTag
00055                         && $token->getId() == 'head'
00056                     ) {
00057                         $insideHead = true;
00058                         continue;
00059                     }
00060                 }
00061                 
00062                 if ($insideHead) {
00063                     if ($token instanceof SgmlEndTag && $token->getId() == 'head')
00064                         break;
00065                     
00066                     if (
00067                         $token instanceof SgmlOpenTag
00068                         && $token->getId() == 'link'
00069                         && $token->hasAttribute('rel')
00070                         && $token->hasAttribute('href')
00071                     ) {
00072                         if ($token->getAttribute('rel') == 'openid.server')
00073                             $this->server = HttpUrl::create()->parse(
00074                                 $token->getAttribute('href')
00075                             );
00076                         
00077                         if ($token->getAttribute('rel') == 'openid.delegate')
00078                             $this->realId = HttpUrl::create()->parse(
00079                                 $token->getAttribute('href')
00080                             );
00081                     }
00082                 }
00083             }
00084             
00085             if (!$this->server || !$this->server->isValid())
00086                 throw new OpenIdException('bad server');
00087             else
00088                 $this->server->makeComparable();
00089             
00090             if (!$this->realId)
00091                 $this->realId = $claimedId;
00092             elseif (!$this->realId->isValid())
00093                 throw new OpenIdException('bad delegate');
00094             else
00095                 $this->realId->makeComparable();
00096         }
00097         
00101         public static function create(
00102             HttpUrl $claimedId,
00103             HttpClient $httpClient
00104         )
00105         {
00106             return new self($claimedId, $httpClient);
00107         }
00108         
00112         public function getRealId()
00113         {
00114             return $this->realId;
00115         }
00116         
00120         public function getServer()
00121         {
00122             return $this->server;
00123         }
00124     }
00125 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4