1
1
<?php
2
2
3
- /*
4
- *
5
- * Sample OAuth2 Library PDO DB Implementation
6
- *
3
+ /**
4
+ * @file
5
+ * Sample OAuth2 Library PDO DB Implementation.
7
6
*/
8
7
9
- // Set these values to your database access info
8
+ // Set these values to your database access info.
10
9
define ("PDO_DSN " , "mysql:dbname=mydb;host=localhost " );
11
10
define ("PDO_USER " , "user " );
12
11
define ("PDO_PASS " , "pass " );
13
12
14
13
include "../../../lib/oauth.php " ;
15
14
15
+ /**
16
+ * OAuth2 Library PDO DB Implementation.
17
+ */
16
18
class PDOOAuth2 extends OAuth2 {
19
+
17
20
private $ db ;
18
21
22
+ /**
23
+ * Overrides OAuth2::__construct().
24
+ */
19
25
public function __construct () {
20
26
parent ::__construct ();
21
27
@@ -26,17 +32,34 @@ class PDOOAuth2 extends OAuth2 {
26
32
}
27
33
}
28
34
35
+ /**
36
+ * Release DB connection during destruct.
37
+ */
29
38
function __destruct () {
30
39
$ this ->db = NULL ; // Release db connection
31
40
}
32
41
42
+ /**
43
+ * Handle PDO exceptional cases.
44
+ */
33
45
private function handleException ($ e ) {
34
46
echo "Database error: " . $ e ->getMessage ();
35
47
exit ;
36
48
}
37
49
38
- // Little helper function to add a new client to the database
39
- // Do NOT use this in production! This sample code stores the secret in plaintext!
50
+ /**
51
+ * Little helper function to add a new client to the database.
52
+ *
53
+ * Do NOT use this in production! This sample code stores the secret
54
+ * in plaintext!
55
+ *
56
+ * @param $client_id
57
+ * Client identifier to be stored.
58
+ * @param $client_secret
59
+ * Client secret to be stored.
60
+ * @param $redirect_uri
61
+ * Redirect URI to be stored.
62
+ */
40
63
public function addClient ($ client_id , $ client_secret , $ redirect_uri ) {
41
64
try {
42
65
$ sql = "INSERT INTO clients (client_id, client_secret, redirect_uri) VALUES (:client_id, :client_secret, :redirect_uri) " ;
@@ -50,15 +73,12 @@ class PDOOAuth2 extends OAuth2 {
50
73
}
51
74
}
52
75
53
- /*
54
- *
55
- * Below, we've implemented the required OAuth2 methods
56
- * which are either declared as abstract or meant to be
57
- * overridden in the base class.
76
+ /**
77
+ * Implements OAuth2::checkClientCredentials().
58
78
*
79
+ * Do NOT use this in production! This sample code stores the secret
80
+ * in plaintext!
59
81
*/
60
-
61
- // Do NOT use this in production! This sample code stores the secret in plaintext!
62
82
protected function checkClientCredentials ($ client_id , $ client_secret = NULL ) {
63
83
try {
64
84
$ sql = "SELECT client_secret FROM clients WHERE client_id = :client_id " ;
@@ -77,6 +97,9 @@ class PDOOAuth2 extends OAuth2 {
77
97
}
78
98
}
79
99
100
+ /**
101
+ * Implements OAuth2::getRedirectUri().
102
+ */
80
103
protected function getRedirectUri ($ client_id ) {
81
104
try {
82
105
$ sql = "SELECT redirect_uri FROM clients WHERE client_id = :client_id " ;
@@ -95,6 +118,9 @@ class PDOOAuth2 extends OAuth2 {
95
118
}
96
119
}
97
120
121
+ /**
122
+ * Implements OAuth2::getAccessToken().
123
+ */
98
124
protected function getAccessToken ($ oauth_token ) {
99
125
try {
100
126
$ sql = "SELECT client_id, expires, scope FROM tokens WHERE oauth_token = :oauth_token " ;
@@ -110,6 +136,9 @@ class PDOOAuth2 extends OAuth2 {
110
136
}
111
137
}
112
138
139
+ /**
140
+ * Implements OAuth2::setAccessToken().
141
+ */
113
142
protected function setAccessToken ($ oauth_token , $ client_id , $ expires , $ scope = NULL ) {
114
143
try {
115
144
$ sql = "INSERT INTO tokens (oauth_token, client_id, expires, scope) VALUES (:oauth_token, :client_id, :expires, :scope) " ;
@@ -125,12 +154,18 @@ class PDOOAuth2 extends OAuth2 {
125
154
}
126
155
}
127
156
157
+ /**
158
+ * Overrides OAuth2::getSupportedGrantTypes().
159
+ */
128
160
protected function getSupportedGrantTypes () {
129
161
return array (
130
162
OAUTH2_GRANT_TYPE_AUTH_CODE ,
131
163
);
132
164
}
133
165
166
+ /**
167
+ * Overrides OAuth2::getAuthCode().
168
+ */
134
169
protected function getAuthCode ($ code ) {
135
170
try {
136
171
$ sql = "SELECT code, client_id, redirect_uri, expires, scope FROM auth_codes WHERE code = :code " ;
@@ -146,8 +181,9 @@ class PDOOAuth2 extends OAuth2 {
146
181
}
147
182
}
148
183
149
- // Take the provided authorization code values and store them somewhere (db, etc.)
150
- // Required for OAUTH2_GRANT_TYPE_AUTH_CODE
184
+ /**
185
+ * Overrides OAuth2::setAuthCode().
186
+ */
151
187
protected function setAuthCode ($ code , $ client_id , $ redirect_uri , $ expires , $ scope = NULL ) {
152
188
try {
153
189
$ sql = "INSERT INTO auth_codes (code, client_id, redirect_uri, expires, scope) VALUES (:code, :client_id, :redirect_uri, :expires, :scope) " ;
0 commit comments