Last active
March 30, 2019 11:24
-
-
Save slinkydeveloper/b1444aa59828c4f3fe657ff67d5d3bad to your computer and use it in GitHub Desktop.
Example solution for SO question 55408452
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.vertx.core.Handler | |
import io.vertx.core.http.HttpMethod | |
import io.vertx.ext.web.Router | |
import io.vertx.ext.web.RoutingContext | |
import io.vertx.ext.web.handler.* | |
import io.vertx.ext.web.sstore.LocalSessionStore | |
import io.vertx.core.Vertx | |
import io.vertx.kotlin.core.deployVerticleAwait | |
import io.vertx.kotlin.core.http.listenAwait | |
import io.vertx.kotlin.core.json.json | |
import io.vertx.kotlin.core.json.obj | |
import io.vertx.kotlin.coroutines.CoroutineVerticle | |
import io.vertx.kotlin.ext.auth.isAuthorizedAwait | |
import kotlinx.coroutines.launch | |
class ConcreteVerticle : CoroutineVerticle() { | |
override suspend fun start() { | |
val adminAuth = MockAuthProvider() | |
val router = Router.router(vertx) | |
router.route() | |
.handler(BodyHandler.create().setBodyLimit(1024 * 1024)) | |
.handler(CookieHandler.create()) | |
.handler(SessionHandler.create(LocalSessionStore.create(vertx))) | |
.handler(UserSessionHandler.create(adminAuth)) | |
.handler(CorsHandler.create("https://localhost:3000") | |
.allowCredentials(true) | |
.allowedHeader("Content-Type") | |
.allowedMethod(HttpMethod.GET) | |
.allowedMethod(HttpMethod.POST) | |
.allowedMethod(HttpMethod.OPTIONS)) | |
router | |
.get("/admin/login") | |
.handler { rc -> | |
adminAuth.authenticate(json { obj("username" to "francesco") }) { res -> | |
launch { | |
var retJson = json { obj() } | |
if (res.succeeded()) { | |
var user = res.result() | |
rc.session()?.regenerateId() | |
rc.setUser(user) | |
retJson = retJson.put("succeed", true) | |
if (user.isAuthorizedAwait("createadmin")) | |
retJson = retJson.put("createadmin", true) | |
} else { | |
retJson = retJson | |
.put("succeed", false) | |
.put("message", res.cause().message) | |
} | |
rc.response() | |
.putHeader("content-type", "application/json") | |
.end(retJson.encode()) | |
} | |
}} | |
router | |
.get("/admin/stuff") | |
.handler { rc -> | |
if (rc.user() == null) | |
rc.response().setStatusCode(403).end() | |
else { | |
val user = rc.user() | |
val session = rc.session() | |
println(session) | |
val principal = user.principal() | |
rc.response().setStatusCode(200).end(principal.toBuffer()) | |
} | |
} | |
vertx.createHttpServer().requestHandler(router).listenAwait(3000) | |
} | |
} | |
suspend fun main() { | |
val vertx = Vertx.vertx() | |
vertx.deployVerticleAwait(ConcreteVerticle()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io.vertx.core.AsyncResult | |
import io.vertx.core.Future | |
import io.vertx.core.Handler | |
import io.vertx.core.json.JsonObject | |
import io.vertx.ext.auth.AbstractUser | |
import io.vertx.ext.auth.AuthProvider | |
import io.vertx.ext.auth.User | |
import io.vertx.kotlin.core.json.json | |
import io.vertx.kotlin.core.json.obj | |
class MockAuthProvider : AuthProvider { | |
override fun authenticate(authInfo: JsonObject?, resultHandler: Handler<AsyncResult<User>>?) { | |
resultHandler!!.handle(Future.succeededFuture( | |
object: AbstractUser() { | |
override fun doIsPermitted(permission: String?, resultHandler: Handler<AsyncResult<Boolean>>?) { | |
resultHandler!!.handle(Future.succeededFuture(true)) | |
} | |
override fun setAuthProvider(authProvider: AuthProvider?) {} | |
override fun principal(): JsonObject { | |
return json { obj( "username" to authInfo!!.getString("username")) } | |
} | |
} | |
)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment