mariovalney / laravel-keycloak-web-guard

Simple Keycloak Guard to Laravel Web Routes - https://packagist.org/packages/vizir/laravel-keycloak-web-guard
146 stars 80 forks source link

Auth driver [keycloak-web] for guard [web] is not defined. #78

Closed lknite closed 1 year ago

lknite commented 2 years ago

alpine-nginx 3.14 laravel 7 php7

followed instructions, added to guard and providers section

image

mariovalney commented 2 years ago

Hi!

Did you followed all instructions to install package? Would you please clear cache and options?

lknite commented 2 years ago

Hello. My servers went off due to a power failure. I need to get them back up and running before I can pick back up on this ticket.

I followed all instructions as far as I know. How do I clear the cache and options? Is that something in laravel or do you mean to clear the cache in the browser? What do you mean by 'options'?

mariovalney commented 2 years ago

I mean Laravel cache:

Clear Cache: php artisan cache:clear

Clear Route Cache: php artisan route:cache

Clear View Cache: php artisan view:clear

Clear Config Cache: php artisan config:cache

lknite commented 2 years ago

I am working to clear out the laravel cache according to your instructions. While I do I wanted to note that I am working with the docker image from the linuxserver project 'docker-heimdall', modifying that one to install 'php7-phar', 'composer', and 'vizir/laravel-keycloak-web-guard'. Maybe this will indicate its not needed to clear the laravel cache?

FROM ghcr.io/linuxserver/baseimage-alpine-nginx:3.14

# set version label
ARG BUILD_DATE
ARG VERSION
ARG HEIMDALL_RELEASE
LABEL build_version="Linuxserver.io version:- ${VERSION} Build-date:- ${BUILD_DATE}"
LABEL maintainer="aptalca"

# environment settings
ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2

RUN \
 echo "**** install runtime packages ****" && \
 apk add --no-cache --upgrade \
        curl \
        php7-ctype \
        php7-curl \
        php7-pdo_pgsql \
        php7-pdo_sqlite \
        php7-pdo_mysql \
        php7-tokenizer \
        php7-zip \
        php7-phar && \
 echo "**** link /vendor to heimdall vendor folder ****" && \
 mkdir /var/www/localhost/heimdall && \
 mkdir /var/www/localhost/heimdall/vendor && \
 ln -s /var/www/localhost/heimdall/vendor /vendor && \
 echo "**** install composer ****" && \
 curl -s https://getcomposer.org/installer | php && \
 echo "**** install oidc plugin ****" && \
 php /composer.phar require vizir/laravel-keycloak-web-guard --update-with-dependencies --with-all-dependencies && \
 echo "**** install heimdall ****" && \
 mkdir -p \
        /heimdall && \
 if [ -z ${HEIMDALL_RELEASE+x} ]; then \
        HEIMDALL_RELEASE=$(curl -sX GET "https://api.github.com/repos/linuxserver/Heimdall/releases/latest" \
        | awk '/tag_name/{print $4;exit}' FS='[""]'); \
 fi && \
 curl -o \
 /heimdall/heimdall.tar.gz -L \
        "https://github.com/linuxserver/Heimdall/archive/${HEIMDALL_RELEASE}.tar.gz" && \
 echo "**** cleanup ****" && \
 rm -rf \
        /tmp/*

# add local files
COPY root/ /

Testing with the following values in the .env file:

#KEYCLOAK_BASE_URL=https://keycloak.vc-prod.k.home.net/auth/realms/home
KEYCLOAK_BASE_URL=https://keycloak.vc-prod.k.home.net/auth
KEYCLOAK_REALM=home
KEYCLOAK_REALM_PUBLIC_KEY=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi1E6yZ2KwEzglEeQ0jtGkiYvD78DTDzsTSQ96v12OdD/R3FmuuUex9imK1KnwmlAU2IvoIQ1Rn0d9sf2hMer0STk/K3Xd2HKhTgxl7eo8ro6CG2BBnVyee6ReHApYFKZYQIDAQAB
KEYCLOAK_CLIENT_ID=k.vc-prod.heimdall
KEYCLOAK_CLIENT_SECRET=<client_secret>
#KEYCLOAK_CACHE_OPENID=

What I do to test this is start up the docker container, the .env values are still there when I restart the container but the auth.php are not. If I browse to heimdall everything works and I'm logged in as admin. I exec into the container and modify the auth.php file according to instructions: image

Now when I browse to heimdall I get the error seen in the original posting.

lknite commented 2 years ago

Here's what I get when I try to clear (afterwards heimdall is still showing the same error):

root@heimdall-56b46fffd9-4rtd7:/var/www/localhost/heimdall# php artisan cache:clear
Failed to clear cache. Make sure you have the appropriate permissions.
root@heimdall-56b46fffd9-4rtd7:/var/www/localhost/heimdall# php artisan route:cache
Route cache cleared!

In Route.php line 1150:

  Unable to prepare route [single/{appid}] for serialization. Uses Closure.

root@heimdall-56b46fffd9-4rtd7:/var/www/localhost/heimdall# php artisan view:clear
Compiled views cleared!
root@heimdall-56b46fffd9-4rtd7:/var/www/localhost/heimdall# php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!

also, here is the plugin which looks to be successfully installed to me:

# find /vendor/vizir/laravel-keycloak-web-guard/
/vendor/vizir/laravel-keycloak-web-guard/
/vendor/vizir/laravel-keycloak-web-guard/.github
/vendor/vizir/laravel-keycloak-web-guard/.github/stale.yml
/vendor/vizir/laravel-keycloak-web-guard/.gitignore
/vendor/vizir/laravel-keycloak-web-guard/CHANGELOG.md
/vendor/vizir/laravel-keycloak-web-guard/README.md
/vendor/vizir/laravel-keycloak-web-guard/composer.json
/vendor/vizir/laravel-keycloak-web-guard/composer.lock
/vendor/vizir/laravel-keycloak-web-guard/config
/vendor/vizir/laravel-keycloak-web-guard/config/keycloak-web.php
/vendor/vizir/laravel-keycloak-web-guard/src
/vendor/vizir/laravel-keycloak-web-guard/src/Auth
/vendor/vizir/laravel-keycloak-web-guard/src/Auth/Guard
/vendor/vizir/laravel-keycloak-web-guard/src/Auth/Guard/KeycloakWebGuard.php
/vendor/vizir/laravel-keycloak-web-guard/src/Auth/KeycloakAccessToken.php
/vendor/vizir/laravel-keycloak-web-guard/src/Auth/KeycloakWebUserProvider.php
/vendor/vizir/laravel-keycloak-web-guard/src/Controllers
/vendor/vizir/laravel-keycloak-web-guard/src/Controllers/AuthController.php
/vendor/vizir/laravel-keycloak-web-guard/src/Exceptions
/vendor/vizir/laravel-keycloak-web-guard/src/Exceptions/KeycloakCallbackException.php
/vendor/vizir/laravel-keycloak-web-guard/src/Exceptions/KeycloakCanException.php
/vendor/vizir/laravel-keycloak-web-guard/src/Facades
/vendor/vizir/laravel-keycloak-web-guard/src/Facades/KeycloakWeb.php
/vendor/vizir/laravel-keycloak-web-guard/src/KeycloakWebGuardServiceProvider.php
/vendor/vizir/laravel-keycloak-web-guard/src/Middleware
/vendor/vizir/laravel-keycloak-web-guard/src/Middleware/KeycloakAuthenticated.php
/vendor/vizir/laravel-keycloak-web-guard/src/Middleware/KeycloakCan.php
/vendor/vizir/laravel-keycloak-web-guard/src/Models
/vendor/vizir/laravel-keycloak-web-guard/src/Models/KeycloakUser.php
/vendor/vizir/laravel-keycloak-web-guard/src/Services
/vendor/vizir/laravel-keycloak-web-guard/src/Services/KeycloakService.php
lknite commented 2 years ago

After working with this a bit and starting over with a clean environment I see the normal heimdall screen at first, then after editing auth.php to 'keycloak-web' and 'keycloak-users' and 'Vizir\KeycloakWebGuard\Models\KeycloakUser::class' now I'm just getting 500, seems the error isn't being caught now, logs show a similar message:

==> /config/log/nginx/error.log <==
2022/09/25 17:10:28 [error] 189#189: *113 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught InvalidArgumentException: Auth driver [keycloak-web] for guard [web] is not defined. in /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php:97
Stack trace:
#0 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php(68): Illuminate\Auth\AuthManager->resolve()
#1 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php(307): Illuminate\Auth\AuthManager->guard()
#2 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(261): Illuminate\Auth\AuthManager->__call()
#3 /var/www/localhost/heimdall/app/Providers/AppServiceProvider.php(72): Illuminate\Support\Facades\Facade::__callStatic()
#4 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/View/Concerns/ManagesEvents.php(162): App\Providers\AppServiceProvider->App\Providers\{closure}()
#5 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Eve...PHP message: PHP Fatal error:  Uncaught InvalidArgumentException: Auth driver [keycloak-web] for guard [web] is not defined. in /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php:97
Stack trace:
#0 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php(68): Illuminate\Auth\AuthManager->resolve()
#1 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php(307): Illuminate\Auth\AuthManager->guard()
#2 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(261): Illuminate\Auth\AuthManager->__call()
#3 /var/www/localhost/heimdall/app/Providers/AppServiceProvider.php(72): Illuminate\Support\Facades\Facade::__callStatic()
#4 /var/www/localhost/heimdall/vendor/laravel/framework/src/Illuminate/View/Concerns/ManagesEvents.php(162): App\Providers\AppServiceProvider->App\Providers\{closure}()
#5 /var/www/localhost/heimd

==> /config/log/nginx/access.log <==
172.27.172.220 - - [25/Sep/2022:17:10:28 -0600] "GET / HTTP/1.1" 500 5 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"

Notes:

lknite commented 2 years ago

working out of repo https://github.com/lknite/docker-heimdall

is there a way to list what auth drivers are installed maybe? so i can verify things are installed correctly ... other troubleshooting ideas?

lknite commented 1 year ago

I'm available to work on this today and tomorrow.

lknite commented 1 year ago

snippit from pipeline building docker image (this all look correct?):

**** install oidc plugin ****
Info from https://repo.packagist.org/: #StandWithUkraine
Using version ^3.0 for vizir/laravel-keycloak-web-guard
./composer.json has been created
Running composer update vizir/laravel-keycloak-web-guard --with-all-dependencies
Loading composer repositories with package information
Updating dependencies
Lock file operations: 9 installs, 0 updates, 0 removals
  - Locking guzzlehttp/guzzle (7.5.0)
  - Locking guzzlehttp/promises (1.5.2)
  - Locking guzzlehttp/psr7 (2.4.1)
  - Locking psr/http-client (1.0.1)
  - Locking psr/http-factory (1.0.1)
  - Locking psr/http-message (1.0.1)
  - Locking ralouphie/getallheaders (3.0.3)
  - Locking symfony/deprecation-contracts (v2.5.2)
  - Locking vizir/laravel-keycloak-web-guard (v3.0.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 9 installs, 0 updates, 0 removals
  - Downloading guzzlehttp/promises (1.5.2)
  - Downloading ralouphie/getallheaders (3.0.3)
  - Downloading psr/http-message (1.0.1)
  - Downloading psr/http-factory (1.0.1)
  - Downloading guzzlehttp/psr7 (2.4.1)
  - Downloading psr/http-client (1.0.1)
  - Downloading symfony/deprecation-contracts (v2.5.2)
  - Downloading guzzlehttp/guzzle (7.5.0)
  - Downloading vizir/laravel-keycloak-web-guard (v3.0.1)
 0/9 [>---------------------------]   0%
 4/9 [============>---------------]  44%
 8/9 [========================>---]  88%
 9/9 [============================] 100%
  - Installing guzzlehttp/promises (1.5.2): Extracting archive
  - Installing ralouphie/getallheaders (3.0.3): Extracting archive
  - Installing psr/http-message (1.0.1): Extracting archive
  - Installing psr/http-factory (1.0.1): Extracting archive
  - Installing guzzlehttp/psr7 (2.4.1): Extracting archive
  - Installing psr/http-client (1.0.1): Extracting archive
  - Installing symfony/deprecation-contracts (v2.5.2): Extracting archive
  - Installing guzzlehttp/guzzle (7.5.0): Extracting archive
  - Installing vizir/laravel-keycloak-web-guard (v3.0.1): Extracting archive
 0/9 [>---------------------------]   0%
 9/9 [============================] 100%
3 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
4 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
No security vulnerability advisories found
lknite commented 1 year ago

I was running 'composer' in the wrong folder. Needed to change to the folder just below the existing vendor directory in the project I was in. Closing as the project has released with php8 and I'm starting over with my implementation.