-
Notifications
You must be signed in to change notification settings - Fork 28
/
auth.php
137 lines (104 loc) · 2.95 KB
/
auth.php
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
<?php
/**
* Stores tokens
*/
abstract class AuthTokenStore {
abstract public function exists();
abstract public function set($tokens);
abstract public function get();
};
/**
* Authentication Provider
*/
abstract class AuthProvider {
/**
* Login with session tokens to resume an existing session
*
*/
abstract public function login_with_tokens($tokens);
/**
* Returns tokens from the current opened session.
*
* Note that this method will be called only after login
*
*/
abstract public function get_tokens();
/**
* Returns True/False whether credentials are valid and session created.
*
* Note that some sort of a session must be created as we will ask for
* tokens of this session with ``get_tokens()``.
*
*/
abstract public function login($credentials);
/**
* Closes the current session and returns boolean upon success.
*
*/
abstract public function logout();
};
/**
* Propagate tokens
*/
abstract class AuthWebTransmitter {
abstract public function read_tokens_from_request();
abstract public function js_propagation_code($tokens);
};
/**
* Manages an oe connection and it's relation with php session,
* provides also facilities to send authentication to other domains.
*/
abstract class Auth {
private $auth_cache = NULL;
private $auth_cache_dirty = True;
private $enable_propagation = False;
/**
* returns True if login is accepted
*/
public function is_auth() {
if ($this->auth_cache !== NULL &&
$this->auth_cache_dirty === False)
return $this->auth_cache;
$this->auth_cache = $this->authTokenStore->exists() &&
$this->authProvider->login_with_tokens($this->authTokenStore->get());
$this->auth_cache_dirty = False;
return $this->auth_cache;
}
/** authenticating by credential
*
* This function must validate authentication of given credentials
*/
public function authenticate($credentials) {
$login_success = $this->authProvider->login($credentials);
if ($login_success) {
$tokens = $this->authProvider->get_tokens();
$this->authTokenStore->set($tokens);
$this->auth_cache = True;
$this->auth_cache_dirty = False;
$this->enable_propagation = True;
};
return $login_success;
}
/** deauthenticating
*
* This function must unlog current session
*/
public function deauthenticate() {
$this->authProvider->logout();
$this->authTokenStore->set(null); // deleting tokens
$this->auth_cache = False;
$this->auth_cache_dirty = False;
$this->enable_propagation = True;
return True; // logout succeeded
}
/** returns javascript code to define the ``get_session_ids()``
* function and urls variable
*
*/
public function js_code_for_propagate() {
if (!$this->enable_propagation) return "";
$tokens = $this->authTokenStore->get(); // returns NULL if no tokens
return $this->authWebTransmitter->js_propagation_code($tokens);
}
}
?>