PHP code example of zhimei / sso

1. Go to this page and download the library: Download zhimei/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/ */

    

zhimei / sso example snippets


    Zhimei\sso\SsoServerServiceProvider::class

    'SsoServer' => 'Zhimei\sso\Facades\SsoServer'

    'sso_server' => [
                'model'         => env('SSO_MODEL'),  //It's abstract of Zhimei\sso\SsoServerModeAbstract
                'driver'        => env('SSO_SERVER_DRIVER', 'file'),  //file or memcached (recommend memcached)
                'clients'       => [
                    //'app_id'              => ['app_id'=>'app_id', 'app_secret'=>'app_secret', 'return_url'=>'return_url'],
                    'app_id_client_www'     => ['app_id'=>'app_id_client_www', 'app_secret'=>'app_secret_24A234FDG34S54GS', 'return_url'=>'http://www.zhimei360.com/'],
                    //...
                ],
    
            ],

    $app->register(Zhimei\sso\SsoServerServiceProvider::class);

     SSO_MODEL=App\Models\SsoUser  ####It's abstract of Zhimei\sso\SsoServerModeAbstract
     SSO_SERVER_DRIVER=file
     SSO_CLIENT_APP_ID_1=app_id_1
     SSO_CLIENT_APP_SECRET_1=asdfsdfdf34rfdfE
     SSO_CLIENT_APP_RETURN_URL_1=http://sso/
    # SSO_CLIENT_APP_ID_2=
    # SSO_CLIENT_APP_SECRET_2=
    # SSO_CLIENT_APP_RETURN_URL_2=
    # SSO_CLIENT_APP_ID_3=
    # SSO_CLIENT_APP_SECRET_3=
    # SSO_CLIENT_APP_RETURN_URL_3=

 namespace App\Models;

use Zhimei\sso\SsoServerModeAbstract;

class SsoUser extends SsoServerModeAbstract  {

    /**
         * @param $username
         * @param $password
         * @return bool
         */
        public function authenticate($username, $password){
            $user = User::where('username', $username)->first();
            if(empty($user)){
                return false;
            }
            if(!password_verify($password, $user->password)){
                return false;
            }
            return true;
        }
    
        /**
         * @param $username
         * @return null
         */
        public function getUserByUsername($username){
            $user = User::where('username', $username)->first();
            if(empty($user)){
                return null;
            }
            return $user->toArray();
        }
    
        /**
         * @param $user_id
         */
        public function getUserById($user_id){
    
        }

}

    #for Lumen
    $app->get('/', function(){
        $method = app('request')->input('command');
        try{
            $return = app('SsoServer')->{$method}();
        }catch (\Exception $e){
            if($method=='attach'){
                throw new \Exception($e->getMessage());
            }else {
                return ['fail' => true, 'msg' => $e->getMessage()];
            }
        }
        return $return;
    });
    $app->post('login', ['as'=>'sso.login', function()use($app){
        return $app['SsoServer']->login();
    }]);
    
    #for Laravel
    Route::get('/', function(){
                      $method = \Request::input('command');
                      try{
                          $return = app('SsoServer')->{$method}();
                      }catch (\Exception $e){
                          if($method=='attach'){
                              throw new \Exception($e->getMessage());
                          }else {
                              return ['fail' => true, 'msg' => $e->getMessage()];
                          }
                      }
                      return $return;
                  });
    Route::post('login', ['as'=>'sso.login', function()use(){
                       return \SsoServer::login();
                   }]);

    Zhimei\sso\SsoClientServiceProvider::class

    'SsoClient' => 'Zhimei\sso\Facades\SsoClient'

    app('SsoClient')->getAttachUrl();

    /**
     * Attach our session to the user's session on the SSO server.
     * @param null $state
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    return app('SsoClient')->attach();

    app('SsoClient')->getUserInfo();

    app('SsoClient')->logout();

#for Lumen
$app->get('login', function()use($app){
    if($app['SsoClient']::isAttached()){
        $userInfo = app('SsoClient')->getUserInfo();
        if(empty($userInfo)){
            return app('SsoClient')->reAttach();
        }
        return $userInfo;
    }else{
        return $app['SsoClient']::attach();
    }
});
$app->get('logout', function()use($app){
    $app['SsoClient']->logout();
});

#for Laravel
Route::get('login', function(){
    if(\SsoClient::isAttached()){
        $userInfo = app('SsoClient')->getUserInfo();
        if(empty($userInfo)){
            return app('SsoClient')->reAttach();
        }
        return $userInfo;
    }else{
        return \SsoClient::attach();
    }
});
Route::get('logout', function(){
    \SsoClient::logout();
});

    $ php artisan config:publish Zhimei/sso

    $ php artisan config:publish Zhimei/sso