-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
587246c
commit 88d5848
Showing
6 changed files
with
175 additions
and
46 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
22 changes: 15 additions & 7 deletions
22
...lstefani/paddy/security/JwksController.kt → ...ication/DeviceAuthenticationController.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
60 changes: 60 additions & 0 deletions
60
src/main/kotlin/online/danielstefani/paddy/authorization/AuthorizationController.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,60 @@ | ||
package online.danielstefani.paddy.authorization | ||
|
||
import io.quarkus.logging.Log | ||
import io.vertx.core.json.JsonObject | ||
import io.vertx.ext.auth.impl.jose.JWT | ||
import jakarta.ws.rs.Consumes | ||
import jakarta.ws.rs.POST | ||
import jakarta.ws.rs.Path | ||
import jakarta.ws.rs.Produces | ||
import jakarta.ws.rs.core.MediaType | ||
import online.danielstefani.paddy.authorization.dto.AuthorizationRequestDto | ||
import online.danielstefani.paddy.authorization.dto.AuthorizationResultDto | ||
import org.jboss.resteasy.reactive.RestResponse | ||
|
||
import online.danielstefani.paddy.authorization.dto.AuthorizationResultDto.Companion.forbid | ||
import online.danielstefani.paddy.authorization.dto.AuthorizationResultDto.Companion.allow | ||
|
||
@Path("/") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
class AuthorizationController { | ||
|
||
@Path("/verify") | ||
@POST | ||
fun getJwt(authDto: AuthorizationRequestDto): RestResponse<AuthorizationResultDto> { | ||
|
||
// Parse JWT | ||
val jwt: JsonObject? = | ||
try { | ||
JWT.parse(authDto.username) | ||
} catch (ex: Exception) { | ||
Log.debug("Received invalid JWT: <${authDto.username}>.") | ||
null | ||
} | ||
if (jwt == null) return forbid() | ||
|
||
val sub = jwt.getJsonObject("payload").getString("sub") | ||
|
||
// Special case: Check if the token is for the backend | ||
if (sub.equals("paddy-backend")) { | ||
Log.debug("Received Paddy Backend JWT: <${authDto.username}>.") | ||
return allow() | ||
} | ||
|
||
// Check if the topic matches the sub -> if no match 403 | ||
return if (topicMatchSub(authDto.topic, sub)) allow() else forbid() | ||
} | ||
|
||
/* | ||
Topics are expected to be in some-topic-here/test/abc/SUB-XXXX. | ||
This function extracts the SUB-XXXX part and checks it against the sub in the claim. | ||
*/ | ||
private fun topicMatchSub(topic: String, sub: String): Boolean { | ||
val topicSerial: String? = with(topic.split("/")) { | ||
if (this.isEmpty()) null | ||
else this.last() | ||
} | ||
return topicSerial.equals(sub) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/online/danielstefani/paddy/authorization/dto/AuthorizationRequestDto.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,13 @@ | ||
package online.danielstefani.paddy.authorization.dto | ||
|
||
/* | ||
This payload will be sent by EMQX to verify | ||
whether an actor has access to a topic. | ||
This step happens after the actor's authenticity has been verified. | ||
https://www.emqx.io/docs/en/latest/access-control/authz/http.html | ||
*/ | ||
data class AuthorizationRequestDto( | ||
val username: String, // Expected to be a JWT | ||
val topic: String // Topic that client wants to access | ||
) |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/online/danielstefani/paddy/authorization/dto/AuthorizationResultDto.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,39 @@ | ||
package online.danielstefani.paddy.authorization.dto | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue | ||
import org.jboss.resteasy.reactive.RestResponse | ||
import org.jboss.resteasy.reactive.RestResponse.* | ||
import java.util.* | ||
|
||
class AuthorizationResultDto private constructor( | ||
val result: DeviceAuthorizationResult | ||
) { | ||
companion object { | ||
fun allow(): RestResponse<AuthorizationResultDto> { | ||
return ResponseBuilder.ok(AuthorizationResultDto(DeviceAuthorizationResult.ALLOW)) | ||
.status(Status.OK) | ||
.build() | ||
} | ||
|
||
fun forbid(): RestResponse<AuthorizationResultDto> { | ||
return ResponseBuilder.ok(AuthorizationResultDto(DeviceAuthorizationResult.DENY)) | ||
.status(Status.OK) | ||
.build() | ||
} | ||
|
||
fun ignore(): RestResponse<AuthorizationResultDto> { | ||
return ResponseBuilder.ok(AuthorizationResultDto(DeviceAuthorizationResult.IGNORE)) | ||
.status(Status.OK) | ||
.build() | ||
} | ||
} | ||
|
||
enum class DeviceAuthorizationResult { | ||
ALLOW, DENY, IGNORE; | ||
|
||
@JsonValue | ||
fun toLowerCase(): String { | ||
return toString().lowercase(Locale.getDefault()) | ||
} | ||
} | ||
} |
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