Skip to content

question: Why am I not forwarded to the resource after authenticating (OIDC with keycloak)?Β #500

Description

@elistia

Hi,

I have keycloak set up as an OIDC provider and am successfully using it for other OIDC tasks. I am using caddy to reverse proxy a number of systems, but also now to serve some static files (using file_server browse).

I would like to use OIDC to provide a layer of security over the shared files.

I am running caddy as a docker container, and have replaced the standard image with ghcr.io/authcrunch/authcrunch:latest

I seem to have got caddy-security working in that when hitting a protected resource I am forwarded to my keycloak login page. However, on authentication, I am not returned to the resource that I had initially tried to access, but to a caddy-security portal page, which simply gives an option to Sign Out.

My Caddyfile is as follows:

{
        auto_https disable_certs
        order authenticate before respond
        order authorize before basicauth

        debug

        security {
                oauth identity provider keycloak {
                        driver generic
                        realm keycloak
                        client_id {env.KEYCLOAK_CLIENT_ID}
                        client_secret {env.KEYCLOAK_CLIENT_SECRET}
                        scopes openid email
                        base_auth_url https://keycloak.mydomain.com/realms/MyRealm
                        metadata_url https://keycloak.mydomain.com/realms/MyRealm/.well-known/openid-configuration
                }

                authentication portal keycloak_portal {
                        crypto default token lifetime 3600
                        crypto key sign-verify {env.JWT_SHARED_KEY}
                        enable identity provider keycloak
                        cookie domain mydomain.com

                        # unsure if this is required
                        transform user {
                                match realm keycloak
                                action add role authp/user
                        }
                }

                authorization policy mypolicy {
                        set auth url /caddy-security/oauth2/keycloak
                        allow roles *
                        #allow roles authp/user
                        crypto key verify {env.JWT_SHARED_KEY}
                        validate bearer header
                        inject headers with claims
                }
        }
}

*.mydomain.com:443 {
        tls /etc/ssl/certs/my-chain.pem /etc/ssl/certs/key.pem
}

www.mydomain.com:443 {

        route /caddy-security/* {
                authenticate with keycloak_portal
        }

        redir /pub /pub/
        handle_path /pub/* {
                authorize with mypolicy
                root * /srv/www/pub
                file_server browse
        }
}

The ClientID, ClientSecret and JWT key are being set as env variables in docker compose.

I am not really worried about authorization using groups at this stage. As long as a user has successfully authenticated with keycloak then happy to let them in (hence using allow roles *)

When I browse to https://www.mydomain.com/pub I am forwarded to my keycloak login page as expected.

After logging in I am redirected to https://www.mydomain.com/caddy-security/portal

If I then manually navigate back to https://www.mydomain.com/pub I am able to access the contents so things are working. However, users will expect to be redirected to the page they initially tried to access (when sent a link to a file for example).

At what stage is the "redirect to original resource" supposed to happen? I cannot see anything in the debug logs that suggests it is being attempted and failing.
When I am forwarded to keycloak, redirect_url=www.mydomain.com/caddy-security/oauth2/keycloak/authorization-code-callback which I assume is correct?

Should something be kicking in at this point and redirecting the user to /pub?

Appreciate any help - I feel that I am close!

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions