Updated examples

This commit is contained in:
Alex Bilbie 2015-04-05 21:11:10 +01:00
parent eabcf82268
commit 5840ace38f
5 changed files with 159 additions and 1 deletions

47
examples/composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
"hash": "8d3e15426ebda5d273b960d5c9f373b5",
"hash": "9ae3d11ba275cce8764cfa3002ec7c93",
"packages": [
{
"name": "alexbilbie/proton",
@ -187,6 +187,51 @@
],
"time": "2014-12-20 21:24:13"
},
{
"name": "firebase/php-jwt",
"version": "2.0.0",
"target-dir": "Firebase/PHP-JWT",
"source": {
"type": "git",
"url": "https://github.com/firebase/php-jwt.git",
"reference": "ffcfd888ce1e4f2d70cac2dc9b7301038332fe57"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/firebase/php-jwt/zipball/ffcfd888ce1e4f2d70cac2dc9b7301038332fe57",
"reference": "ffcfd888ce1e4f2d70cac2dc9b7301038332fe57",
"shasum": ""
},
"require": {
"php": ">=5.2.0"
},
"type": "library",
"autoload": {
"classmap": [
"Authentication/",
"Exceptions/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Neuman Vong",
"email": "neuman+pear@twilio.com",
"role": "Developer"
},
{
"name": "Anant Narayanan",
"email": "anant@php.net",
"role": "Developer"
}
],
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
"homepage": "https://github.com/firebase/php-jwt",
"time": "2015-04-01 18:46:38"
},
{
"name": "illuminate/container",
"version": "v5.0.26",

40
examples/public/jwt.php Normal file
View File

@ -0,0 +1,40 @@
<?php
use League\OAuth2\Server\Exception\OAuthException;
use League\OAuth2\Server\Server;
use League\OAuth2\Server\TokenTypes\JsonWebTokenType;
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository;
use OAuth2ServerExamples\Repositories\UserRepository;
use Symfony\Component\HttpFoundation\Request;
include(__DIR__ . '/../vendor/autoload.php');
// Setup the authorization server
$server = new Server();
$server->addRepository(new ClientRepository());
$server->addRepository(new ScopeRepository());
$server->addRepository(new AccessTokenRepository());
$server->addRepository(new UserRepository());
// Enable the password grant, respond with JWTs
$server->enableGrantType('PasswordGrant', new JsonWebTokenType());
// Setup JWT params
JsonWebTokenType::setIssuer('http://example.com/');
JsonWebTokenType::setAudience('http://myawesomeapp.com/');
JsonWebTokenType::setEncryptionKey('foobar123');
// Setup app + routing
$application = new \Proton\Application();
$application->post('/access_token', function (Request $request) use ($server) {
try {
return $server->getAccessTokenResponse($request);
} catch (OAuthException $e) {
return $e->generateHttpResponse();
}
});
// Run the app
$application->run();

View File

@ -0,0 +1,34 @@
<?php
use League\OAuth2\Server\Exception\OAuthException;
use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository;
use OAuth2ServerExamples\Repositories\UserRepository;
use Symfony\Component\HttpFoundation\Request;
include(__DIR__ . '/../vendor/autoload.php');
// Setup the authorization server
$server = new Server();
$server->addRepository(new ClientRepository());
$server->addRepository(new ScopeRepository());
$server->addRepository(new AccessTokenRepository());
$server->addRepository(new UserRepository());
// Enable the password grant
$server->enableGrantType('PasswordGrant');
// Setup app + routing
$application = new \Proton\Application();
$application->post('/access_token', function (Request $request) use ($server) {
try {
return $server->getAccessTokenResponse($request);
} catch (OAuthException $e) {
return $e->generateHttpResponse();
}
});
// Run the app
$application->run();

View File

@ -0,0 +1,17 @@
<?php
namespace OAuth2ServerExamples\Entities;
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
class UserEntity implements UserEntityInterface
{
/**
* Return the user's identifier
* @return mixed
*/
public function getIdentifier()
{
return 1;
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace OAuth2ServerExamples\Repositories;
use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface;
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use OAuth2ServerExamples\Entities\UserEntity;
class UserRepository implements UserRepositoryInterface
{
/**
* Get a user
*
* @param string $username
* @param string $password
*
* @return UserEntityInterface
*/
public function getByCredentials($username, $password)
{
return new UserEntity();
}
}