summaryrefslogtreecommitdiffstats
path: root/Auth/OpenID/HTTPFetcher.php
blob: ec3944df256e14a1756deb13820942540502be7f (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
<?php
/**
 * This module contains the HTTP fetcher interface
 *
 * 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
 */

/**
 * This is the status code beginAuth returns when it is unable to
 * fetch the OpenID URL the user entered.
 */
define('Auth_OpenID_HTTP_FAILURE', 'http failure');

/**
 * This class is the interface for HTTP fetchers the OpenID consumer
 * library uses.  This interface is only important if you need to
 * write a new fetcher for some reason.
 *
 * @access private
 * @package OpenID
 */
class Auth_OpenID_HTTPFetcher {

    /**
     * Return whether a URL should be allowed. Override this method to
     * conform to your local policy.
     *
     * By default, will attempt to fetch any http or https URL.
     */
    function allowedURL($url)
    {
        return Auth_OpenID_URLHasAllowedScheme($url);
    }

    /**
     * This performs an HTTP get, following redirects along the way.
     *
     * @return array $tuple This returns a three-tuple on success.
     * The first value is the http return code. The second value is
     * the final url that was fetched, after following any redirects.
     * The third value is the data that was retrieved from the site.
     * If the fetch didn't succeed, return null.
    */
    function get($url)
    {
        trigger_error("not implemented", E_USER_ERROR);
    }

    /**
     * This performs an HTTP post.  If it makes sense, it will follow
     * redirects along the way.
     *
     * @return array $tuple This returns a three-tuple on success.
     * The first value is the http return code. The second value is
     * the final url that was fetched, after following any redirects.
     * The third value is the data that was retrieved from the site.
     * If the fetch didn't succeed, return null.
     */
    function post($url, $body)
    {
        trigger_error("not implemented", E_USER_ERROR);
    }

    /**
     * Retrieve the given URL and return the identity information
     * contained therein. That is, perform OpenID discovery.
     *
     * @param string $identity_url The URL that the user entered
     *
     * @return array list($status, $info) The information parsed from
     * the page or an error. If the status is Auth_OpenID_SUCCESS, the
     * information returned is array($consumer_id, $server_id,
     * $server_url). The $server_url is the OpenID server's URL. The
     * consumer ID is the identifier by which the user should be known
     * to the consumer. The server ID is the identifier by which the
     * user should be known to the server.
     */
    function findIdentityInfo($identity_url)
    {
        $url = Auth_OpenID_normalizeURL($identity_url);
        $ret = @$this->get($url);
        if ($ret === null) {
            return array(Auth_OpenID_HTTP_FAILURE, null);
        }

        list($http_code, $consumer_id, $data) = $ret;
        if ($http_code != 200) {
            return array(Auth_OpenID_HTTP_FAILURE, $http_code);
        }

        $link_attrs = Auth_OpenID_parseLinkAttrs($data);
        $server = Auth_OpenID_findFirstHref($link_attrs, 'openid.server');
        $delegate = Auth_OpenID_findFirstHref($link_attrs, 'openid.delegate');

        if ($server === null) {
            return array(Auth_OpenID_PARSE_ERROR, null);
        } else {
            $server_id = $delegate ? $delegate : $consumer_id;
            $urls = array($consumer_id, $server_id, $server);
            return array(Auth_OpenID_SUCCESS, $urls);
        }
    }
}

?>