No object for type 'org.pac4j.core.client.Clients' in registry exception Pac4j

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

No object for type 'org.pac4j.core.client.Clients' in registry exception Pac4j

Tom
Hi,

I'm developing a REST API with Ratpack. I've successfully secured the endpoints using Pac4j integrating with an external identity provider:

//Auth setup
String publicKey = '...'
String algorithm = 'RSA'
JwtAuthenticator authenticator = new JwtAuthenticator(publicKey, algorithm, null)
HeaderClient client = new HeaderClient(authenticator)
client.setHeaderName('Authorization')
client.setPrefixHeader('Bearer ')
all RatpackPac4j.authenticator(client)

//API
prefix('api') {
    all RatpackPac4j.requireAuth(HeaderClient, { ctx, profile ->
        return profile.getRoles().contains('ROLE_API')
    } as Authorizer)
    // Endpoint handlers etc
}

I've then refactored the code to externalise the config:

class SecurityConfig {
    String publicKey
    String algorithm
    String headerName
    String headerPrefix
}

conf.yaml 'config.yml'
conf.require('/security', SecurityConfig)

//Auth
all { SecurityConfig config ->
    RatpackPac4j.authenticator(createHeaderClient(config))
    next()
}

private static HeaderClient createHeaderClient(String key) {
    JwtAuthenticator authenticator = new JwtAuthenticator(config.publicKey, config.algorithm, null)
    HeaderClient client = new HeaderClient(authenticator)
    client.setHeaderName(config.headerName)
    client.setPrefixHeader(config.headerPrefix)
    return client
}

This throws a NotInRegistryException: No object for type 'org.pac4j.core.client.Clients' in registry...

Have I defined the "all" handler incorrectly? Any help would be much appreciated.

Kind regards,
Tom
Reply | Threaded
Open this post in threaded view
|

Re: No object for type 'org.pac4j.core.client.Clients' in registry exception Pac4j

Tom
To answer my own question:

SecurityConfig securityConfig = registry.get(SecurityConfig)
all RatpackPac4j.authenticator(createHeaderClient(securityConfig))

instead of

all { SecurityConfig config ->
    RatpackPac4j.authenticator(createHeaderClient(config))
    next()
}

solves the problem.