Skip to content

Commit e38d466

Browse files
committed
* document all sample server implementation.
1 parent 747f865 commit e38d466

10 files changed

+131
-58
lines changed

server/examples/mongo/addclient.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
/*
4-
*
3+
/**
4+
* @file
55
* Sample client add script.
6-
* Obviously not production-ready code, just simple and to the point.
76
*
7+
* Obviously not production-ready code, just simple and to the point.
88
*/
99

1010
include "lib/MongoOAuth2.inc";
@@ -32,7 +32,6 @@
3232
<label for="redirect_uri">Redirect URI:</label>
3333
<input type="text" name="redirect_uri" id="redirect_uri" />
3434
</p>
35-
3635
<input type="submit" value="Submit" />
3736
</form>
3837
</body>

server/examples/mongo/authorize.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2-
/*
2+
3+
/**
4+
* @file
5+
* Sample authorize endpoint.
36
*
4-
* Sample authorize endpoint
57
* Obviously not production-ready code, just simple and to the point.
6-
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
78
*
9+
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
810
*/
911

1012
require "lib/MongoOAuth2.inc";

server/examples/mongo/lib/MongoOAuth2.inc

+43-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
<?php
22

3-
/*
4-
*
5-
* Sample OAuth2 Library Mongo DB Implementation
6-
*
7-
*/
8-
93
// Set these values to your Mongo database
104
define("MONGO_CONNECTION", "mongodb://user:pass@mongoserver/mydb");
115
define("MONGO_DB", "mydb");
126

137
include "../../../lib/oauth.php";
148

9+
/**
10+
* Sample OAuth2 Library Mongo DB Implementation.
11+
*/
1512
class MongoOAuth2 extends OAuth2 {
13+
1614
private $db;
1715

16+
/**
17+
* Overrides OAuth2::__construct().
18+
*/
1819
public function __construct() {
1920
parent::__construct();
2021

2122
$mongo = new Mongo(MONGO_CONNECTION);
2223
$this->db = $mongo->selectDB(MONGO_DB);
2324
}
2425

25-
// Do NOT use this in production! This sample code stores the secret in plaintext!
26+
/**
27+
* Little helper function to add a new client to the database.
28+
*
29+
* Do NOT use this in production! This sample code stores the secret
30+
* in plaintext!
31+
*
32+
* @param $client_id
33+
* Client identifier to be stored.
34+
* @param $client_secret
35+
* Client secret to be stored.
36+
* @param $redirect_uri
37+
* Redirect URI to be stored.
38+
*/
2639
public function addClient($client_id, $client_secret, $redirect_uri) {
2740
$this->db->clients->insert(array(
2841
"_id" => $client_id,
@@ -31,29 +44,35 @@ class MongoOAuth2 extends OAuth2 {
3144
));
3245
}
3346

34-
/*
35-
*
36-
* Below, we've implemented the required OAuth2 methods
37-
* which are either declared as abstract or meant to be
38-
* overridden in the base class.
47+
/**
48+
* Implements OAuth2::checkClientCredentials().
3949
*
50+
* Do NOT use this in production! This sample code stores the secret
51+
* in plaintext!
4052
*/
41-
42-
// Do NOT use this in production! This sample code stores the secret in plaintext!
4353
protected function checkClientCredentials($client_id, $client_secret = NULL) {
44-
$client = $this->db->clients->findOne(array("_id" => $client_id, "pw" => $client_secret));
54+
$client = $this->db->clients->findOne(array("_id" => $client_id, "pw" => $client_secret));
4555
return $client !== NULL;
4656
}
4757

58+
/**
59+
* Implements OAuth2::getRedirectUri().
60+
*/
4861
protected function getRedirectUri($client_id) {
4962
$uri = $this->db->clients->findOne(array("_id" => $client_id), array("redirect_uri"));
5063
return $uri !== NULL ? $uri["redirect_uri"] : FALSE;
5164
}
5265

66+
/**
67+
* Implements OAuth2::getAccessToken().
68+
*/
5369
protected function getAccessToken($oauth_token) {
5470
return $this->db->tokens->findOne(array("_id" => $oauth_token));
5571
}
5672

73+
/**
74+
* Implements OAuth2::setAccessToken().
75+
*/
5776
protected function setAccessToken($oauth_token, $client_id, $expires, $scope = NULL) {
5877
$this->db->tokens->insert(array(
5978
"_id" => $oauth_token,
@@ -63,19 +82,26 @@ class MongoOAuth2 extends OAuth2 {
6382
));
6483
}
6584

85+
/**
86+
* Overrides OAuth2::getSupportedGrantTypes().
87+
*/
6688
protected function getSupportedGrantTypes() {
6789
return array(
6890
OAUTH2_GRANT_TYPE_AUTH_CODE,
6991
);
7092
}
7193

94+
/**
95+
* Overrides OAuth2::getAuthCode().
96+
*/
7297
protected function getAuthCode($code) {
7398
$stored_code = $this->db->auth_codes->findOne(array("_id" => $code));
7499
return $stored_code !== NULL ? $stored_code : FALSE;
75100
}
76101

77-
// Take the provided authorization code values and store them somewhere (db, etc.)
78-
// Required for OAUTH2_GRANT_TYPE_AUTH_CODE
102+
/**
103+
* Overrides OAuth2::setAuthCode().
104+
*/
79105
protected function setAuthCode($code, $client_id, $redirect_uri, $expires, $scope = NULL) {
80106
$this->db->auth_codes->insert(array(
81107
"_id" => $code,

server/examples/mongo/protected_resource.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2-
/*
2+
3+
/**
4+
* @file
5+
* Sample protected resource.
36
*
4-
* Sample protected resource
57
* Obviously not production-ready code, just simple and to the point.
6-
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
78
*
9+
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
810
*/
911

1012
require "lib/MongoOAuth2.inc";

server/examples/mongo/token.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2-
/*
2+
3+
/**
4+
* @file
5+
* Sample token endpoint.
36
*
4-
* Sample token endpoint
57
* Obviously not production-ready code, just simple and to the point.
6-
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
78
*
9+
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
810
*/
911

1012
require "lib/MongoOAuth2.inc";

server/examples/pdo/addclient.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
/*
4-
*
3+
/**
4+
* @file
55
* Sample client add script.
6-
* Obviously not production-ready code, just simple and to the point.
76
*
7+
* Obviously not production-ready code, just simple and to the point.
88
*/
99

1010
include "lib/PDOOAuth2.inc";

server/examples/pdo/authorize.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2-
/*
2+
3+
/**
4+
* @file
5+
* Sample authorize endpoint.
36
*
4-
* Sample authorize endpoint
57
* Obviously not production-ready code, just simple and to the point.
6-
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
78
*
9+
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
810
*/
911

1012
require "lib/PDOOAuth2.inc";

server/examples/pdo/lib/PDOOAuth2.inc

+52-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
<?php
22

3-
/*
4-
*
5-
* Sample OAuth2 Library PDO DB Implementation
6-
*
3+
/**
4+
* @file
5+
* Sample OAuth2 Library PDO DB Implementation.
76
*/
87

9-
// Set these values to your database access info
8+
// Set these values to your database access info.
109
define("PDO_DSN", "mysql:dbname=mydb;host=localhost");
1110
define("PDO_USER", "user");
1211
define("PDO_PASS", "pass");
1312

1413
include "../../../lib/oauth.php";
1514

15+
/**
16+
* OAuth2 Library PDO DB Implementation.
17+
*/
1618
class PDOOAuth2 extends OAuth2 {
19+
1720
private $db;
1821

22+
/**
23+
* Overrides OAuth2::__construct().
24+
*/
1925
public function __construct() {
2026
parent::__construct();
2127

@@ -26,17 +32,34 @@ class PDOOAuth2 extends OAuth2 {
2632
}
2733
}
2834

35+
/**
36+
* Release DB connection during destruct.
37+
*/
2938
function __destruct() {
3039
$this->db = NULL; // Release db connection
3140
}
3241

42+
/**
43+
* Handle PDO exceptional cases.
44+
*/
3345
private function handleException($e) {
3446
echo "Database error: " . $e->getMessage();
3547
exit;
3648
}
3749

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+
*/
4063
public function addClient($client_id, $client_secret, $redirect_uri) {
4164
try {
4265
$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 {
5073
}
5174
}
5275

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().
5878
*
79+
* Do NOT use this in production! This sample code stores the secret
80+
* in plaintext!
5981
*/
60-
61-
// Do NOT use this in production! This sample code stores the secret in plaintext!
6282
protected function checkClientCredentials($client_id, $client_secret = NULL) {
6383
try {
6484
$sql = "SELECT client_secret FROM clients WHERE client_id = :client_id";
@@ -77,6 +97,9 @@ class PDOOAuth2 extends OAuth2 {
7797
}
7898
}
7999

100+
/**
101+
* Implements OAuth2::getRedirectUri().
102+
*/
80103
protected function getRedirectUri($client_id) {
81104
try {
82105
$sql = "SELECT redirect_uri FROM clients WHERE client_id = :client_id";
@@ -95,6 +118,9 @@ class PDOOAuth2 extends OAuth2 {
95118
}
96119
}
97120

121+
/**
122+
* Implements OAuth2::getAccessToken().
123+
*/
98124
protected function getAccessToken($oauth_token) {
99125
try {
100126
$sql = "SELECT client_id, expires, scope FROM tokens WHERE oauth_token = :oauth_token";
@@ -110,6 +136,9 @@ class PDOOAuth2 extends OAuth2 {
110136
}
111137
}
112138

139+
/**
140+
* Implements OAuth2::setAccessToken().
141+
*/
113142
protected function setAccessToken($oauth_token, $client_id, $expires, $scope = NULL) {
114143
try {
115144
$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 {
125154
}
126155
}
127156

157+
/**
158+
* Overrides OAuth2::getSupportedGrantTypes().
159+
*/
128160
protected function getSupportedGrantTypes() {
129161
return array(
130162
OAUTH2_GRANT_TYPE_AUTH_CODE,
131163
);
132164
}
133165

166+
/**
167+
* Overrides OAuth2::getAuthCode().
168+
*/
134169
protected function getAuthCode($code) {
135170
try {
136171
$sql = "SELECT code, client_id, redirect_uri, expires, scope FROM auth_codes WHERE code = :code";
@@ -146,8 +181,9 @@ class PDOOAuth2 extends OAuth2 {
146181
}
147182
}
148183

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+
*/
151187
protected function setAuthCode($code, $client_id, $redirect_uri, $expires, $scope = NULL) {
152188
try {
153189
$sql = "INSERT INTO auth_codes (code, client_id, redirect_uri, expires, scope) VALUES (:code, :client_id, :redirect_uri, :expires, :scope)";

server/examples/pdo/protected_resource.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
2-
/*
2+
3+
/**
4+
* @file
5+
* Sample protected resource.
36
*
4-
* Sample protected resource
57
* Obviously not production-ready code, just simple and to the point.
6-
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
78
*
9+
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
810
*/
911

1012
require "lib/PDOOAuth2.inc";

0 commit comments

Comments
 (0)