Git Product home page Git Product logo

keycloak's Introduction

Keycloak OAuth2 Provider for Laravel Socialite

Installation

    clone : git clone https://github.com/avdevs/keycloak.git
    installing package : composer require avdevs/keycloak

Requirements

    Download latest keycloak server repository from https://www.keycloak.org/ and setup keycloak server at admin console.

Environment Setup

Add below key array in '/config/services.php' file.

    'keycloak' => [
            'authServerUrl'         => env('KEYCLOAK_AUTHSERVERURL'),
            'realm'                 => env('KEYCLOAK_REALM'),
            'clientId'              => env('KEYCLOAK_CLIENTID'),
            'clientSecret'          => env('KEYCLOAK_CLIENTSECRET'),
            'redirectUri'           => env('KEYCLOAK_REDIRECTURI'),
            'encryptionAlgorithm'   => env('KEYCLOAK_ENCRYPTIONALGORITHM'),
            'encryptionKeyPath'     => env('KEYCLOAK_ENCRYPTIONKEYPATH'),
            'encryptionKey'         => env('KEYCLOAK_ENCRYPTIONKEY'),
    ]

Laravel AppServiceProvider

Add below code in /app/Providers/AppServiceProvider.php file which helps to execute keycloak as a socialite provider.

    use Avdevs\Keycloak\KeycloakProvider;

In boot() method, add below code

    $this->bootKeycloakSocialite();

Add Function

    private function bootKeycloakSocialite()
    {
        $socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
        $socialite->extend(
            'keycloak',
            function ($app) use ($socialite) {
                $config = $app['config']['services.keycloak'];
                return new KeycloakProvider($config);
            }
        );
    }

Laravel Functions and Routes

    use Socialite;
    Route::get('/redirect/{provider}', 'ProviderAuthController@redirectToProvider');

    /**
     * Redirect to keycloak server.
     * @provider
     * @return
     */
    public function redirectToProvider($provider)
    {
        /* where $provider = 'keycloak' */
        return Socialite::driver($provider)
                    ->stateless()
                    ->scopes([]) // Array ex : name
                    ->redirect();
    }
    Route::get('/callback/{provider}', 'ProviderAuthController@CallbackFunction');

    /**
     * retrieve user information which is located at keycloak serve.
     * @provider
     * @return
     */
    public function CallbackFunction($provider)
    {
        /* where $provider = 'keycloak' */
        $userData = Socialite::driver($provider)
                        ->stateless()
                        ->user();
        /* Note : */
        /* 1) Callback url is same for login and logout request. so this function executed twice. */
        /* 2) Must add below code, Because user data not retrieved while logout calls is requested. */
        if(!isset($userData->email)){
            return redirect()->back();
        }

        /* your logic for add or get user detail */

    }
    Route::get('/logout/{provider}', 'ProviderAuthController@ProviderLogout');

     /**
     * Log the user out of the application.
     * @provider
     * @return void
     */
    public function ProviderLogout($provider)
    {
        /* where $provider = 'keycloak' */
        /* logout from laravel auth */
        Auth::logout();
        /* redirect to keycloak logout url */
        return redirect(
            Socialite::driver($provider)
                ->getLogoutUrl()
        );
    }

keycloak's People

Contributors

axent avatar dhavalatavdevs avatar leakhand avatar newavdevs avatar papalardo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

keycloak's Issues

Can provide more detail step for installation?

Hi, I'm feel interested in this project however however Im confuse with your installation step. It stated we shall perform git clone https://github.com/avdevs/keycloak.git, but it didnt state where it shall clone too, and which package in socialiteproviders shall we use.

Can you provide more detail guidance?

Error on KeycloakResourceOwner

Every line you have use Elvis operator.

return $this->response['name'] ?: null; // get E_NOTICE, if $response['name'] eq false

 /**
 * Get resource owner id
 *
 * @return string|null
 */
public function getId()
{
    return $this->response['sub'] ?: null;
}

/**
 * Get resource owner email
 *
 * @return string|null
 */
public function getEmail()
{
    return $this->response['email'] ?: null;
}

/**
 * Get resource owner name
 *
 * @return string|null
 */
public function getName()
{
    return $this->response['name'] ?: null;
}

I can create pull request for you to update

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.