lsfiege/cas

Centralized app logins made simple, use ONLY in small/internal projects for demo purposes or testing

This package's canonical repository appears to be gone and the package has been frozen as a result.

1.0.9 2019-03-07 14:09 UTC

This package is auto-updated.

Last update: 2019-11-30 23:16:15 UTC


README

Centralized app logins made simple, use ONLY in small/internal projects or for demo and learning purposes / testing

68747470733a2f2f6c61726176656c2e636f6d2f6173736574732f696d672f636f6d706f6e656e74732f6c6f676f2d6c61726176656c2e737667

Requirements

  • Laravel 5.3|5.4|5.5|5.6|5.7
  • PHP 7.1+

Installation

Server

Install this package using composer.

composer require lsfiege/cas

For Laravel 5.3 and 5.4 add in config/app.php, Laravel 5.5+ have package autodiscovery

'providers' => [
    ...
    \Lsfiege\CAS\CASServiceProvider::class,
]

'aliases' => [
    ...
    'CASServer'=> \Lsfiege\CAS\Facades\CASServer::class,
    'CASClient'=> \Lsfiege\CAS\Facades\CASClient::class
]

Publish config/cas.php file.

php artisan vendor:publish --provider="Lsfiege\CAS\CASServiceProvider"

Run migrations

php artisan migrate

Now create cas key for server You can create it using following Artisan CLI command:

php artisan cas:generate-key

Now this key its stored in your .env file, copy it to clients .env in the key CAS_KEY=your-cas-key-here

Finally, add your allowed client origins without trailing comma, example: http://foo.bar:

'allowed' => [
    'https://mysite.bar',
],

Client

Install this package using composer.

composer require lsfiege/cas

Publish config/cas.php file.

php artisan vendor:publish --provider="Lsfiege\CAS\CASServiceProvider"

Change type value in config/cas.php file from server to client.

Set these options in your .env file:

CAS_KEY=your-server-cas-key-here
CAS_HOST=https://cas.host

CAS_HOST is your server's http url without trailing slash.

Edit your app/User.php by adding Lsfiege\CAS\Traits\HasCASAccess trait

...
use Lsfiege\CAS\Traits\HasCASAccess;

class User extends Authenticatable
{
    use Notifiable, HasCASAccess;
    
    ...    
}

Now add to LoginController these two methods like this

namespace App\Http\Controllers\Auth;
...
use Illuminate\Http\Request;
use Lsfiege\CAS\Facades\CASClient;
class LoginController extends Controller
{
    ...
    
    public function showLoginForm()
    {
        return CASClient::showLoginForm();
    }
    
    public function login(Request $request)
    {
        return CASClient::login($request);
    }
}

Edit your ForgotPasswordController:

namespace App\Http\Controllers\Auth;
...
use Lsfiege\CAS\Facades\CASClient;

class ForgotPasswordController extends Controller
{
    ...
    
    public function showLinkRequestForm()
    {
        return CASClient::resetPasswordRedirect();
    }
}

Finally edit RegisterController like this:

namespace App\Http\Controllers\Auth;
...
use Lsfiege\CAS\Facades\CASClient;

class RegisterController extends Controller
{
    ...
    protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);

        if (CASClient::register($data)) {
            return $user;
        };
    }

    public function showRegistrationForm()
    {
        return CASClient::showRegistrationForm();
    }
}

To update password for given user in client and server in App\User use the resetPassword($user, $password) method.

Example

class UserController extends controller{
    ...
    public function changePassword(User $user, $password){
        $user->resetPassword($user, $password);
        
        ...
    }
}

If you need to override array $data to create users into your app, in User.php class add this method:

use HasCASAccess {
    createAuth as protected createUserAuth;
}

...

/** @override */
public static function createAuth($data)
{
    $role = Role::whereName('user')->first();

    $data['role_id'] = $role->id; 
    
    // Other custom params here

    return self::createUserAuth($data);
}

And into your RegisterController:

protected function create(array $data)
{
    $role = Role::whereName('user')->first();

    $user = User::create([
        'name' => $data['name'],
        'role_id' => $role->id,
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    if (CASClient::register($data)) {
        return $user;
    };
}

Both

You can specify your User model in config file in:

'usersModel' => \App\User::class,