summaryrefslogtreecommitdiffstats
path: root/Tests/Auth/OpenID/MemStore.php
blob: aa00ba9d63309aa59f8a58e156b5d7ffa3d4b11c (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
<?php

/**
 * In-memory OpenID store implementation for testing only
 */
require_once "Auth/OpenID/Interface.php";

class Tests_Auth_OpenID_MemStore extends Auth_OpenID_OpenIDStore {
    var $assocs = null;
    var $nonces = null;

    function Tests_Auth_OpenID_MemStore($auth_key=null)
    {
        $this->assocs = array();
        $this->nonces = array();
        $this->auth_key = $auth_key;
    }

    function getKey($server_url, $handle)
    {
        return serialize(array($server_url, $handle));
    }

    function getAssocPairs()
    {
        $pairs = array();
        foreach ($this->assocs as $key => $assoc) {
            list($assoc_url, $_) = unserialize($key);
            $pairs[] = array($assoc_url, $assoc);
        }
        return $pairs;
    }

    function getServerAssocs($server_url)
    {
        $matches = array();
        foreach ($this->getAssocPairs() as $pair) {
            list($assoc_url, $assoc) = $pair;
            if ($assoc_url == $server_url) {
                $matches[] = $assoc;
            }
        }
        return $matches;
    }

    function getAssociation($server_url, $handle=null)
    {
        $assocs = $this->getServerAssocs($server_url);
        if ($handle === null) {
            $best = null;
            foreach ($assocs as $assoc) {
                if (!isset($best) ||
                    $best->issued < $assoc->issued) {

                    $best = $assoc;
                }
            }
            return $best;
        } else {
            foreach ($assocs as $assoc) {
                if ($assoc->handle == $handle) {
                    return $assoc;
                }
            }
            return null;
        }
    }

    function storeAssociation($server_url, $association)
    {
        $key = $this->getKey($server_url, $association->handle);
        $this->assocs[$key] = $association;
    }

    function removeAssociation($server_url, $handle)
    {
        $key = $this->getKey($server_url, $handle);
        $present = isset($this->assocs[$key]);
        unset($this->assocs[$key]);
        return $present;
    }

    function storeNonce($nonce)
    {
        if (!in_array($nonce, $this->nonces)) {
            $this->nonces[] = $nonce;
        }
    }

    function useNonce($nonce)
    {
        $index = array_search($nonce, $this->nonces);
        $present = $index !== false;
        if ($present) {
            unset($this->nonces[$index]);
        }
        return $present;
    }

    function reset()
    {
        $this->assocs = array();
        $this->nonces = array();
    }

    function getAuthKey()
    {
        return $this->auth_key;
    }
}