https://kotlinlang.org logo
#ktor
Title
# ktor
j

Joost Klitsie

11/11/2021, 3:52 PM
Hi all! Is there somewhere some in depth information about routes and phases? I made once a feature that could "override" a security rule/role for a route. It can override it, by adding phases to a pipeline in reverse order. In eap 2.0-256 the behavior seems a bit different than older version, more in the thread.
I was making something, where you can override the access rules for the route you are creating (simplified to roles only here):
Copy code
routeWithRules("path", Role.Admin) { // <-- only accessible for admins
	
	routeWithRules("sub-path", Role.User) { // <-- accessible for all users
		get("/") {
			respondText("All users can see this, even if it is within another route with higher restrictions")
		}
	}
	
	get("something") {
		respondText("Only admins can see this")
	}

}
Any nested
routeWithRules
added a phase to the pipeline, making sure the most inner phase would be first in the list of phases. Then I set a flag to the context of the call, that the access rules have been overriden, so the outer
routeWithRules
would skip applying their rules. So looks like this: At the outer
routeWithRules("path", Role.Admin) {..}
We add phase "route_rules_0" to the pipeline and intercept it. We only apply the rules if the flag
hasInterceptedAccessRules
is still false. At the inner
routeWithRules("sub-path", Role.User) {..}
We add phases "route_rules_1, route_rules_0" in this order. We intercept here "route_rules_1" (if
hasInterceptedAccessRules
is false) and then set the flag
hasInterceptedAccessRules
to true. Any nested
routeWithRules
will add a new phase in reverse order, as to override the parent rules. In earlier ktor, if I add a phase within the
routeWithRules
to the pipeline, any parent
routeWithRules
would inherit the phases of the children within. In ktor 2.0 eap 256, if I add a phase, the phase isn't added to the parents in the pipeline. I can of course find the parent routes and add the phase manually, but did something change here? Or am I holding it wrong? 🙂
r

Rustam Siniukov

11/11/2021, 4:00 PM
the only change that I can remember was related to ordering of phases when using
insertPhaseAfter
. can it be related? https://github.com/ktorio/ktor/pull/2618
👍 1
j

Joost Klitsie

11/16/2021, 3:24 PM
I am unsure, but I guess perhaps children phases will not be added to the parents, makes sense to me