subfission / cas

Simple CAS Authentication for Laravel 5 - 10.
MIT License
151 stars 70 forks source link

Wiki update with a new guide to install and configure this package in Laravel 11 #133

Open jmcortesmartinez opened 1 week ago

jmcortesmartinez commented 1 week ago

I am new to Laravel 11 and I see that quite a few things have changed compared to version 10. Can you update the installation document to include how to install and configure it in Laravel 11?

Instructions for Laravel 10 cannot be applied.

Thank you for your work!

dstepe commented 1 week ago

@jmcortesmartinez I've added a section for Laravel 11 to the Installation page and updated the Configuration to include details on route middleware. I have tested these instructions with a vanilla Laravel 11 installation. Please let me know how this work for you or if more changes are needed.

jmcortesmartinez commented 1 week ago

Thank you very much @dstepe, but I still have some issues. Forgive my inexperience:

In the section Configuration of the repository, it’s not clear where to set the environment variables. I believe that in previous versions, you needed to modify the config/cas.php file, but I can’t find it now. Can you help me?

Best regards, and thank you very much for your work. Without this package, we’d be lost.

dstepe commented 1 week ago

@jmcortesmartinez I suggest going through the Laravel Configuration docs. Packages frequently use a configuration file, as you mentioned, but they are not always published into the project config directory for editing. Instead, configuration is done through environment variables.

This package does have a configuration file, but as noted on the installation guide, publishing it is optional.

You will notice lines like this in config files:

'cas_hostname' => env('CAS_HOSTNAME', 'cas.myuniv.edu'),

The env() helper returns the environment variable value, or the default. The package's config file results are merged with any config returned by the project config file of the same name.

You can find all of the configuration variables for this package on the Configuration wiki page.

There are many options for setting environment variables. In a traditional server based installation, you may use the .env file in the root of the project, as documented here. In a container run time, environment variables may be injected during container launch.

Hope this helps.

jmcortesmartinez commented 1 week ago

It helps a lot! It works!

But I’ve noticed that even though I’ve declared the variables in .env, it always uses the ones declared in the file /vendor/subfission/cas/src/config/config.php

Does it have any kind of priority? Is it defined somewhere? Can I change it to use the variables declared in another file?

dstepe commented 1 week ago

I'll need to know more about your specific circumstances. It is possible to define values in the config/*.php files in your project, but since those are typically committed to source code control, you need to be careful. I'm not clear what you mean by "declared in the file" because of that.

Note that in your code you could only ever use the config() helper function to get configuration values. Do not rely on the env() helper itself due to potential configuration caching.

You are setting the variables for this package in your .env file (and you have confirmed the file is named exactly that and readable by the web server process)? What do you get when you run:

php artisan config:show cas

for your project? If you are not familiar with artisan, you will find console commands useful to know.

jmcortesmartinez commented 6 days ago

I'll try to explain myself better:

-In the .env file I put: _CASHOSTNAME=www.a.com -In the /vendor/subfission/cas/src/config/config.php file I put: _'cas_hostname' => env('CASHOSTNAME', 'www.b.com'),

and when I run: php artisan config:show cas I get:

$ php artisan config:show cas

cas ................................................................................................................................................ cas_hostname .............................................................................................................................www.b.com etc..

That's why I understand that the CAS configuration is obtained, with priority, from the file that is in /vendor/subfission/cas/src/config/config.php

If I modify that file and put the values ​​of my server it works perfectly but I would like to know where it is referenced to have the option of being able to change it for other.

Thank you very much for your help and your patience with me.

dstepe commented 6 days ago

While the observable result could be interpreted as you have, I suspect the real issue is that the variables in your .env file are not actually be used. The env() helper function reads the value from the environment. The result of that won't change depending on which file the function is used in. I suggest approaching this as a problem getting the environment variables correctly set rather than how the Laravel configuration system uses them.

What operating system are you running this on? Have you confirmed the exact name of the .env file? (Windows and macOS hide . files and extensions by default for example.) Are there any errors related to environment setup in the Laravel log file?

jmcortesmartinez commented 6 days ago

I'm pretty sure my system is using the .env file because I have the database connection set up and I can run the migrations.

Here is the data from my system:

OS: Debian GNU/Linux 12 (bookworm) Laravel: 11.14 PHP: 8.3.8 Composer: 2.7.7

Environment: local Debug Mode: ENABLED

Cache: Config: CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

dstepe commented 6 days ago

Being able to run the migrations is a good indication, yes. I see you are running PHP 8.3. I did my test with PHP 8.2. I'll have another run at this later today with 8.3.

To avoid editing the package config file, you can publish it to your project:

php artisan vendor:publish

Then search for cas:

 ┌ Which provider or tag's files would you like to publish? ────┐
 │ cas                                                          │
 ├──────────────────────────────────────────────────────────────┤
 │ › Provider: Subfission\Cas\CasServiceProvider                │
 │   Tag: cas                                                   │
 └──────────────────────────────────────────────────────────────┘

If you choose Tag: cas it will publish the config file to your project config directory.

jmcortesmartinez commented 6 days ago

That was!

Now it works correctly and the changes I make in the config/cas.php file become effective.

Thank you very much for your help. Your patience has been impressive. If you ever come to Barcelona let me know. I'm indebted.

dstepe commented 6 days ago

Glad to hear it's working! I'm happy to help. Paying it forward and all. :)

Can you tell me what exactly seemed to fix it for you? If you had problems others might, and I'd really like to understand what was happening.

jmcortesmartinez commented 6 days ago

This is, step by step, what I have been doing with your help. I hope it can be useful to someone else.

  1. Create the Project: o Execute: composer create-project laravel/laravel your-project-name. o Make sure to replace your-project-name with your desired project name.

  2. Configure the .env File: o Edit the .env file to configure your database and other variables.

  3. Run Migrations: o Run: php artisan migrate

  4. Install the Package: o Install the subfission/cas package with: composer require subfission/cas o If necessary, copy the certificate to a directory. For example: /storage/certs/

  5. Middleware: o You can use the example middleware from the package or you can create your own. o To create your own you have to execute: php artisan make:middleware your-middleware-name o Then edit the file app/Http/Middleware/your-middleware-name.php and give it functionality.

  6. Edit bootstrap/app.php: o Edit the bootstrap/app.php file and add: o If you want to use the example Middleware from the package:

    ->withMiddleware(function (Middleware $middleware) {
     $middleware->alias([
     'cas.auth' => \Subfission\Cas\Middleware\CASAuth::class,
     ]);
     })

    o If you want to use your own:

    ->withMiddleware(function (Middleware $middleware) {
                $middleware->alias([
                'your-middleware-name' => \App\Http\Middleware\your-middleware-name::class,
                ]);
             })
  7. Testing the Middleware: o To test the middleware you can edit routes/web.php and create a test group:

    Route::group(['middleware' => 'cas.auth'], function () {
        Route::get('/', function () {
            return view('welcome');
        });
    }); 

    Remember to replace 'cas.auth' with your actual middleware name if you’ve created your own.

  8. Configure CAS Variables: o Run: php artisan vendor:publish and choose the “tag:cas” option. o Edit the config/cas.php file to specify your server data and certificate location if needed. If you copied the certified to /storage/certs you can especify: 'cas_cert' => storage_path('certs/your-cert.crt'),

  9. Final Step: o Load your project website root and must request CAS validation if your site is authorized to do so.

Versions: OS: Linux Laravel: 11.14 PHP: 8.3.8 Composer: 2.7.7

dstepe commented 6 days ago

Thanks for the detailed write up. The steps you provided all seem typical and what I did when testing a vanilla installation, except for the need to publish the config file. That step was not necessary for me and the environment variables I added to my .env were picked up and used by the package as expected. I've switched to PHP 8.3 and still have no issues.

Would you mind sharing a sanitized version of your .env file? I'm quite curious about the cause of this. It runs counter to the desired experience.

jmcortesmartinez commented 5 days ago

I added this on my .env and CAS is still working with the variables set on config/cas.php:

CAS_HOSTNAME='cas.myuniv.edu' CAS_REAL_HOSTS='cas.myuniv.edu' CAS_LOGOUT_URL='cas.myuniv.edu'

Any idea? I don't have a clue but it's ok for me to have the CAS configuration file out of the .env file.

dstepe commented 3 days ago

Sorry for not responding sooner. I had some other things come up. This is odd. I have been unable to reproduce this issue. I'll poke around at it some more this weekend.