-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* All POST and DELETE requests should now refresh session token token expiration increased to 5 hours * Small fix for version service Co-authored-by: szymon.owczarzak <[email protected]>
- Loading branch information
1 parent
8e6a215
commit 78c4df6
Showing
26 changed files
with
304 additions
and
181 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
12 changes: 9 additions & 3 deletions
12
cogboard-app/src/main/kotlin/com/cognifide/cogboard/config/handler/HandlerUtil.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
package com.cognifide.cogboard.config.handler | ||
|
||
import io.vertx.core.http.HttpMethod | ||
import io.vertx.reactivex.ext.web.RoutingContext | ||
|
||
class HandlerUtil { | ||
companion object { | ||
fun endResponse(body: String, routingContext: RoutingContext) { | ||
routingContext.response() | ||
.putHeader("Content-Type", "application/json") | ||
.end(body) | ||
if (SESSION_REFRESHERS.contains(routingContext.request().method())) { | ||
routingContext.request().headers().add("body", body) | ||
routingContext.reroute(HttpMethod.POST, "/api/session/refresh") | ||
} else routingContext.response() | ||
.putHeader("Content-Type", "application/json") | ||
.end(body) | ||
} | ||
|
||
val SESSION_REFRESHERS = setOf(HttpMethod.POST, HttpMethod.DELETE) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
49 changes: 49 additions & 0 deletions
49
cogboard-app/src/main/kotlin/com/cognifide/cogboard/security/JwtCommon.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.cognifide.cogboard.security | ||
|
||
import com.cognifide.cogboard.utils.ExtensionFunctions.asJsonObject | ||
import com.cognifide.cogboard.utils.ExtensionFunctions.endEmptyJson | ||
import com.cognifide.cogboard.utils.ExtensionFunctions.toJWT | ||
import io.vertx.core.json.JsonObject | ||
import io.vertx.ext.auth.KeyStoreOptions | ||
import io.vertx.ext.jwt.JWTOptions | ||
import io.vertx.reactivex.core.Vertx | ||
import io.vertx.reactivex.ext.auth.jwt.JWTAuth | ||
import io.vertx.reactivex.ext.web.RoutingContext | ||
|
||
open class JwtCommon { | ||
|
||
protected lateinit var jwtAuth: JWTAuth | ||
|
||
protected fun sendJWT(ctx: RoutingContext, user: String) { | ||
val body = ctx.request().getHeader("body") ?: "" | ||
ctx.response().putHeader("token", generateJWT(user)) | ||
if (body.isNotEmpty()) ctx.response().end(body) | ||
else ctx.response().endEmptyJson() | ||
} | ||
|
||
protected open fun init(vertx: Vertx, config: JsonObject) { | ||
jwtAuth = initJWT(vertx, config) | ||
} | ||
|
||
private fun generateJWT(username: String): String { | ||
val token = jwtAuth.generateToken( | ||
username.asJsonObject("name"), | ||
JWTOptions().setExpiresInSeconds(SESSION_DURATION_IN_SECONDS) | ||
) ?: "no data" | ||
return "Bearer $token" | ||
} | ||
|
||
private fun initJWT(vertx: Vertx, config: JsonObject): JWTAuth { | ||
val options = KeyStoreOptions() | ||
.setType(config.getString("type", "jceks")) | ||
.setPath(config.getString("path", "keystore.jceks")) | ||
.setPassword(config.getString("password", "secret")) | ||
|
||
return JWTAuth.create(vertx, options.toJWT()) | ||
} | ||
|
||
companion object { | ||
const val SESSION_DURATION_IN_SECONDS = 5 * 60 * 60 // hours * min * sec | ||
const val DEFAULT_ERROR = "Unable to authenticate" | ||
} | ||
} |
This file contains 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
50 changes: 50 additions & 0 deletions
50
cogboard-app/src/main/kotlin/com/cognifide/cogboard/security/SessionHandler.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.cognifide.cogboard.security | ||
|
||
import com.cognifide.cogboard.CogboardConstants.Companion.STATUS_CODE_401 | ||
import com.cognifide.cogboard.utils.ExtensionFunctions.asJsonObject | ||
import com.cognifide.cogboard.storage.Storage | ||
import com.cognifide.cogboard.storage.VolumeStorageFactory | ||
import io.knotx.server.api.handler.RoutingHandlerFactory | ||
import io.vertx.core.Handler | ||
import io.vertx.core.VertxException | ||
import io.vertx.core.json.JsonObject | ||
import io.vertx.reactivex.core.Vertx | ||
import io.vertx.reactivex.ext.web.RoutingContext | ||
|
||
class SessionHandler(val storage: Storage = VolumeStorageFactory.admin()) : RoutingHandlerFactory, JwtCommon() { | ||
|
||
private lateinit var sessionRefreshError: String | ||
|
||
override fun getName(): String = "session-handler" | ||
|
||
override fun create(vertx: Vertx?, config: JsonObject?): Handler<RoutingContext> { | ||
if (vertx == null || config == null) { | ||
throw VertxException("Unable to create SessionHandler vertex=$vertx, config=$config") | ||
} | ||
init(vertx, config) | ||
|
||
return Handler { ctx -> | ||
val token = ctx | ||
.request() | ||
.getHeader("Authorization") | ||
?.substringAfter("Bearer ") | ||
?.asJsonObject("jwt") | ||
|
||
jwtAuth.authenticate(token) { | ||
val username = it.result().delegate.principal().getString("name") ?: "" | ||
if (it.succeeded() && username.isNotBlank()) { | ||
sendJWT(ctx, username) | ||
} else sendUnauthorized(ctx, sessionRefreshError) | ||
} | ||
} | ||
} | ||
|
||
override fun init(vertx: Vertx, config: JsonObject) { | ||
super.init(vertx, config) | ||
sessionRefreshError = config.getString("sessionRefreshError", DEFAULT_ERROR) | ||
} | ||
|
||
private fun sendUnauthorized(ctx: RoutingContext, message: String) { | ||
ctx.response().setStatusMessage(message).setStatusCode(STATUS_CODE_401).end() | ||
} | ||
} |
This file contains 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
9 changes: 0 additions & 9 deletions
9
cogboard-app/src/main/kotlin/com/cognifide/cogboard/widget/Util.kt
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
Oops, something went wrong.