@@ -36,10 +36,14 @@ import io.vertx.ext.web.RoutingContext
3636import io.vertx.ext.web.handler.AuthenticationHandler
3737import io.vertx.ext.web.handler.BodyHandler
3838import io.vertx.ext.web.handler.SessionHandler
39+ import io.vertx.ext.web.impl.UserContextInternal
3940import io.vertx.ext.web.sstore.LocalSessionStore
4041import mu.KotlinLogging
42+ import org.pac4j.cas.profile.CasProfile
4143import org.pac4j.core.config.Config
42- import org.pac4j.vertx.auth.Pac4jUser
44+ import org.pac4j.core.profile.ProfileManager
45+ import org.pac4j.vertx.VertxProfileManager
46+ import org.pac4j.vertx.VertxWebContext
4347import org.pac4j.vertx.context.session.VertxSessionStore
4448import org.pac4j.vertx.handler.impl.CallbackHandler
4549import org.pac4j.vertx.handler.impl.CallbackHandlerOptions
@@ -95,10 +99,10 @@ abstract class CASAuthProvider(vertx: Vertx) : SSOTockAuthProvider(vertx) {
9599 abstract fun getConfig (): Config
96100
97101 /* * Read Tock Login from CAS user info */
98- abstract fun readCasLogin (user : Pac4jUser ): String
102+ abstract fun readCasLogin (userCASProfile : CasProfile ): String
99103
100104 /* * Read roles grouped by namespace from CAS user infos */
101- abstract fun readRolesByNamespace (user : Pac4jUser ): Map <String , Set <String >>
105+ abstract fun readRolesByNamespace (userCASProfile : CasProfile ): Map <String , Set <String >>
102106
103107 override fun createAuthHandler (verticle : WebVerticle ): AuthenticationHandler {
104108 val options: SecurityHandlerOptions = SecurityHandlerOptions ().setClients(" CasClient" )
@@ -121,10 +125,19 @@ abstract class CASAuthProvider(vertx: Vertx) : SSOTockAuthProvider(vertx) {
121125 return user!! // !! is because user is already guaranteed to be not null
122126 }
123127
124- protected open fun upgradeToTockUser (user : Pac4jUser , resultHandler : Handler <HttpResult <TockUser >>) {
128+ /* *
129+ * https://github.com/pac4j/vertx-pac4j/wiki/Get-the-authenticated-user-profiles
130+ */
131+ fun getUserCasProfile (rc : RoutingContext ): CasProfile {
132+ val profileManager: ProfileManager =
133+ VertxProfileManager (VertxWebContext (rc), sessionStore)
134+ return profileManager.getProfile(CasProfile ::class .java).get()
135+ }
136+
137+ protected open fun upgradeToTockUser (userCASProfile : CasProfile , resultHandler : Handler <HttpResult <TockUser >>) {
125138 try {
126- val username = readCasLogin(user )
127- val rolesByNamespace = readRolesByNamespace(user )
139+ val username = readCasLogin(userCASProfile )
140+ val rolesByNamespace = readRolesByNamespace(userCASProfile )
128141 logger.debug { " authenticate $username /$rolesByNamespace " }
129142
130143 if (rolesByNamespace.keys.isEmpty()) {
@@ -169,15 +182,19 @@ abstract class CASAuthProvider(vertx: Vertx) : SSOTockAuthProvider(vertx) {
169182
170183 verticle.router.route(" /*" ).handler(WithExcludedPathHandler (excluded) { rc ->
171184 val user = rc.user()
172- if (user != null && user !is TockUser ) {
185+ if (user != null && user !is TockUser ) { // user is Pac4jUser
173186 executor.executeBlocking {
174- upgradeToTockUser(user as Pac4jUser ) { hr ->
187+ upgradeToTockUser(getUserCasProfile(rc) ) { hr ->
175188 if (hr.succeeded()) {
176189 vertx.runOnContext {
177- sessionHandler
178- .setUser(rc, hr.result)
179- .onSuccess { rc.next() }
180- .onFailure { err -> rc.fail(err) }
190+ // TODO : setUser problem (*): This assignment (adding the user to the context) is not maintained for the next vertx handler (rc.next()).
191+ (rc.userContext() as UserContextInternal ).setUser(hr.result)
192+ // TODO : we are therefore temporarily using the session to store tockUser.
193+ rc.session().put(
194+ " tockUser" ,
195+ hr.result
196+ )
197+ rc.next()
181198 }
182199 } else {
183200 rc.userContext().clear()
0 commit comments