PHP code example of romichoirudin33 / sso

1. Go to this page and download the library: Download romichoirudin33/sso library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

romichoirudin33 / sso example snippets


use Romichoirudin33\Sso\Facades\Sso;

Route::get('/auth/redirect', function () {
    return Sso::driver('ntbprov')->redirect();
});

Route::get('/auth/callback', function () {
    $user = Sso::driver('ntbprov')->user();

    // $user->token
});


defined('BASEPATH') OR exit('No direct script access allowed');

class HTTPClient {
  protected $CI;

  public function __construct() {
    $this->CI =& get_instance();
  }

  public function get($url, $params = array(), $headers = array()) {
    $url = $url . '?' . http_build_query($params);
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
  }

  public function post($url, $data = array()) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
  }

  public function generateRandomString($length = 10) {
    $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
      $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
  }
}


defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends CI_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->library('HTTPClient');
  }

  var $clientId = 'xxxxxxx-xxxx-xxxx-xxxxx-xxxxxxxx';
  var $clientSecret = 'xxxxxxxxxxxxxxxx';
  var $redirectUrl = 'https://example.ntbprov.go.id/index.php/auth/callback';

  public function redirect() {
    $state = $this->httpclient->generateRandomString(40);
    $this->session->set_userdata('state', $state);
 
    $query = http_build_query([
      'client_id' => $this->clientId,
      'redirect_uri' => $this->redirectUrl,
      'response_type' => 'code',
      'scope' => '',
      'state' => $state,
    ]);
 
    return redirect('https://sso.ntbprov.go.id/oauth/authorize?'.$query);
  }

  public function callback(){
    $code = $this->input->get('code');
    $data = [
      'grant_type' => 'authorization_code',
      'client_id' => $this->clientId,
      'client_secret' => $this->clientSecret,
      'code' => $code,
      'redirect_uri' => $this->redirectUrl,
    ];

    $url = 'https://sso.ntbprov.go.id/oauth/token';
    $response = $this->httpclient->post($url, $data);
    $response = json_decode($response);

    $token = $response->access_token;
    $header = array('Authorization: Bearer '.$token, 'Content-Type: application/json', 'Accept: application/json');
    $user = $this->httpclient->get('https://sso.ntbprov.go.id/api/user', [], $header);

    /*
    * This from get data user
    * If you want to logic authentication you still need to adapt the code to your needs
    */
  }
}