@@ -2,10 +2,32 @@ package com.sakethh.linkora
22
33import io.ktor.http.*
44import io.ktor.server.application.*
5- import io.ktor.server.request.path
5+ import io.ktor.server.request.*
66import io.ktor.server.response.*
77import io.ktor.server.routing.*
88
9+
10+ /*
11+ in RoutingNode's `buildPipeline()`, the following is implemented:
12+
13+ for (index in 0..handlers.lastIndex) {
14+ pipeline.intercept(Call) {
15+ val call = call as RoutingPipelineCall
16+ val routingCall = RoutingCall(call)
17+ val routingContext = RoutingContext(routingCall)
18+ if (call.isHandled) return@intercept
19+ handlers[index].invoke(routingContext)
20+ }
21+ }
22+
23+ so if a call is handled here in this `Security.kt` file,
24+ the handler of our routes (defined in routing files) is never reached;
25+ which is exactly what we want.
26+
27+ I've gone through the Ktor auth library (just the surface), and it defines an extension function
28+ `Route.authenticate` (same name) in `AuthenticationInterceptors.kt` that does almost the same thing.
29+ It doesn't directly send a 403 like I'm doing here, but for our scenario, this is fine.
30+ */
931val CustomBearerAuth = createRouteScopedPlugin(name = " CustomBearerAuth" ) {
1032 onCall { call ->
1133 val rawHeader = call.request.headers[HttpHeaders .Authorization ]
@@ -57,6 +79,14 @@ fun Route.authenticate(initOnSuccess: Route.() -> Unit) {
5779}
5880
5981/*
82+
83+ i should have just used `authHeader` and it probably would've fixed the special characters in token = 401 issue
84+
85+ but this does pretty much the same thing, and I learnt a couple of new things, so hell yeahh
86+
87+ --------------------------------------------
88+
89+
6090with the auth lib, something like this would have worked:
6191
6292install(Authentication) {
0 commit comments