1
\$\begingroup\$

I'v developed a OAuth2.0 Client. The initial goal was to make a simple client where people won't have to focus on it to much ,and where they can start coding their product immediately by just including a few lines of code(client).

There is a SQLite3 database which records all the access_tokens and refresh_tokens.Also it handles the expiration of access_tokens(if able to obtain new one).

Please criticize as much as posible i want to learn


The steps to include the project:

1.Download via composer or github:

composer require "tech-387/o-auth2":"v0.1-alpha"

2.Setup the client:

//set json context type
header('Content-Type: application/json');

//Errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

//require autoload
require_once __DIR__.'/../vendor/autoload.php';

//Client
use Discus\Client as Client;

//Client Configuration
use Discus\Configuration\ClientConfiguration as ClientConfiguration;

//Setup the user configuration
$clintData = new ClientConfiguration('testclient','testpass','http://localhostt.com:9000/example/','http://oauth2:9000/token.php');

//Setup the client
$client = new Client($clintData,'mysqlitedb.sqlite'); 

//Autorize the client
echo $client->autorizeClient();

3.Call the api that you want(google play developer for example):

<?php

require_once 'index.php';


/**
* Setup Call for google oauth server
*/
$client->callApi()->headerBearerAuthorization()
                  ->get();
$url = '';
//Execute call
$response = $client->callApi()->execute($url);
if($response == 'recal'){
    $response = $client->callApi()->execute($url);
}else {
    echo $response;
}

The framework it self:

Here is the structure:

W
.
├── README.md
├── composer.json
├── composer.lock
├── discus_logo.png
├── example
│   ├── apiCall.php
│   ├── index.php
│   └── mysqlitedb.sqlite
├── src
│   └── Discus
│       ├── CallApi.php
│       ├── Client.php
│       ├── Configuration
│       │   └── ClientConfiguration.php
│       ├── Database
│       │   └── DatabaseHelper.php
│       ├── GrantTypeHandlers
│       │   ├── AccessTokenHandler.php
│       │   ├── AutorizationHandler.php
│       │   └── RefreshTokenHandler.php
│       └── Helpers
│           ├── AutorizationJsonDecoder.php
│           └── CurlHelper
│               └── CurlBuilder.php
└── tests
    └── StackTest.php

The Client.php is(if i can call it this way) bootstrap file, everything goes over it.

//Api call
use Discus\CallApi as CallApi;

class Client 
{

    //Grand Type Helper
    private $autorizationHandler;
    private $refreshTokenHandler;
    private $accessTokenHandler;
    //Client Configuration
    private $configurationClient;
    //Sqlite 
    private $Sqlite3;
    //Authorization code
    private $code = null;
    //Api Call Helper
    private $callApi = null;



    public function __construct(ClientConfiguration $client_config,$tablePath = []){

        //Pass the table path
        $this->Sqlite3 = new DatabaseHelper($tablePath);

        //Get Autorization Code
        $this->code = $this->getCode();

        //Set configuration to private var
        $this->configurationClient = $client_config;

        //Construct refresh token
        $this->refreshTokenHandler = new RefreshTokenHandler($this->Sqlite3,$this->configurationClient);

        //Construct access token
        $this->accessTokenHandler = new AccessTokenHandler($this->Sqlite3);

        //Construct Call Api Helper
        $this->callApi = new CallApi($this->accessTokenHandler,$this->refreshTokenHandler,$this->configurationClient);

    }


    /**
    * Call the autorization process
    */
    public function autorizeClient()
    {
        //Call the Autorization Handler
        if($this->code !== null){
            $this->autorizationHandler = new AutorizationHandler($this->code,$this->configurationClient,$this->Sqlite3);
            return $this->autorizationHandler->addTokens();
        }
    }

    /**
    * Call to Api Resource
    */
    public function callApi()
    {
        $output = $this->callApi;
        return $output;
    }

    /**
    * Get autorization code
    */
    public function getCode()
    {
        $autorizationCode = null;

        if(isset($_GET['code'])){
            $autorizationCode = trim($_GET['code']);
        }

        return $autorizationCode;
    }

Here is the full project on github,also i have commented out the code a lot.And thanks to @Mike Brant i have started to write more clear code.


There is a lot to do with it ,to add unit testing, to add design patterns...and much more...

I'm having issues since I'm new to all of it.. Which design patterns could i use at which part of the project?

But i wanted to see if the start is good?What would you change?

\$\endgroup\$
  • \$\begingroup\$ @MikeBrant me again :D \$\endgroup\$ – DaAmidza Jun 15 '17 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.