I am trying to implement custom OAuth2 implementation in ThingsBoard instance for that i have implemented OAuth2 server in php from
https://github.com/bshaffer/oauth2-demo-php
http://brentertainment.com/oauth2/
It is working fine as per their demo of bshaffer, when i integrate it with the third party application thingboard instance first step is working fine upto authentication after that it is redirecting to login page stating Oauth2 error but dont know what it is here is the sample URLs and responses of OAuth2 server
http://34.226.xxx.xx/oauth2/web/lockdin/authorize
http://34.226.xxx.xx/oauth2/web/lockdin/token
http://34.226.xxx.xx/oauth2/web/lockdin/resource
Step 1:
Step 2:
Authorization happens after that with success response it sends back to
After this Its getting failed
I have tested the steps after this manaully to generate token and its working fine
http://34.226.xxx.xx/oauth2/web/lockdin/token it gives me response
{
"access_token": "4a01f8b9e8548420425c8f335eda2a3dbde7ef75",
"expires_in": 3600,
"token_type": "Bearer",
"scope": "email",
"refresh_token": "ccbb9b8a03949e0e013acdb7f8e79426aa1a0a58"
}
and resource api i get the following response
{
"email":"test@gmail.com",
"firstName":"Dave",
"lastName":"Johnson",
"profile":1828838378
}
Any help on this will be really appreciated
Edit: Please find the source of end point
Authorize.php
namespace OAuth2Demo\Server\Controllers;
use Silex\Application;
class Authorize
{
public static function addRoutes($routing)
{
$routing->get('/authorize', array(new self(), 'authorize'))->bind('authorize');
$routing->post('/authorize', array(new self(), 'authorizeFormSubmit'))->bind('authorize_post');
}
public function authorize(Application $app)
{
$server = $app['oauth_server'];
$response = $app['oauth_response'];
if (!$server->validateAuthorizeRequest($app['request'], $response)) {
return $server->getResponse();
}
return $app['twig']->render('server/authorize.twig', array(
'client_id' => $app['request']->query->get('client_id'),
'response_type' => $app['request']->query->get('response_type')
));
}
public function authorizeFormSubmit(Application $app)
{
$server = $app['oauth_server'];
$response = $app['oauth_response'];
$authorized = (bool) $app['request']->request->get('authorize');
return $server->handleAuthorizeRequest($app['request'], $response, $authorized);
}
}
Token.php
namespace OAuth2Demo\Server\Controllers;
use Silex\Application;
class Token
{
public static function addRoutes($routing)
{
$routing->post('/token', array(new self(), 'token'))->bind('grant');
}
public function token(Application $app)
{
$server = $app['oauth_server'];
$response = $app['oauth_response'];
return $server->handleTokenRequest($app['request'], $response);
}
}
Resource.php
namespace OAuth2Demo\Server\Controllers;
use Silex\Application;
use Symfony\Component\HttpFoundation\Response;
class Resource
{
public static function addRoutes($routing)
{
$routing->get('/resource', array(new self(), 'resource'))->bind('access');
}
public function resource(Application $app)
{
$server = $app['oauth_server'];
$response = $app['oauth_response'];
if (!$server->verifyResourceRequest($app['request'], $response)) {
return $server->getResponse();
} else {
$api_response = array(
"email"=> "xxx@gmail.com",
"name"=> "Pattatharasu Nataraj",
"family_name"=>"Nataraj",
"given_name"=>"Pattatharasu",
"middle_name"=>"",
"nickname"=>"",
"picture"=>"",
"updated_at"=>""
);
return new Response(json_encode($api_response));
}
}
}
Server.php
class Server implements ControllerProviderInterface
{
public function setup(Application $app)
{
if (!file_exists($sqliteFile = __DIR__.'/../../../data/oauth.sqlite')) {
$this->generateSqliteDb();
}
$storage = new Pdo(array('dsn' => 'mysql:host=localhost;dbname=demoapp'));
$grantTypes = array(
'authorization_code' => new AuthorizationCode($storage),
'user_credentials' => new UserCredentials($storage),
'refresh_token' => new RefreshToken($storage, array(
'always_issue_new_refresh_token' => true,
)),
);
$server = new OAuth2Server($storage, array(
'enforce_state' => true,
'allow_implicit' => true,
'use_openid_connect' => true,
'issuer' => $_SERVER['HTTP_HOST'],
),$grantTypes);
$server->addStorage($this->getKeyStorage(), 'public_key');
$app['oauth_server'] = $server;
$app['oauth_response'] = new BridgeResponse();
}
public function connect(Application $app)
{
$this->setup($app);
$routing = $app['controllers_factory'];
Controllers\Authorize::addRoutes($routing);
Controllers\Token::addRoutes($routing);
Controllers\Resource::addRoutes($routing);
return $routing;
}
private function generateSqliteDb()
{
include_once($this->getProjectRoot().'/data/rebuild_db.php');
}
private function getKeyStorage()
{
$publicKey = file_get_contents($this->getProjectRoot().'/data/pubkey.pem');
$privateKey = file_get_contents($this->getProjectRoot().'/data/privkey.pem');
$keyStorage = new Memory(array('keys' => array(
'public_key' => $publicKey,
'private_key' => $privateKey,
)));
return $keyStorage;
}
private function getProjectRoot()
{
return dirname(dirname(dirname(__DIR__)));
}
}