Skip to content

Commit bc1069b

Browse files
committed
store creator and creation timestamp for a resource
1 parent 61d5353 commit bc1069b

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

authorization/src/main/kotlin/org/modelix/authorization/KeycloakUtils.kt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import org.keycloak.representations.idm.authorization.PermissionRequest
2727
import org.keycloak.representations.idm.authorization.ResourceRepresentation
2828
import org.keycloak.representations.idm.authorization.ScopeRepresentation
2929
import java.net.URL
30+
import java.time.Instant
3031
import java.util.concurrent.TimeUnit
3132

3233
object KeycloakUtils {
@@ -142,18 +143,21 @@ object KeycloakUtils {
142143
return existingResources.get(resourceSpec.name) {
143144
var resource = authzClient.protection().resource().findByNameAnyOwner(resourceSpec.name)
144145
if (resource != null) return@get resource
145-
val protection = (
146-
if (resourceSpec.type.ownerManaged) {
147-
owner?.let { authzClient.protection(owner.token) }
148-
} else {
149-
null
150-
}
151-
) ?: authzClient.protection()
146+
// val protection = owner?.let { authzClient.protection(owner.token) }
147+
// ?.takeIf { resourceSpec.type.createByUser }
148+
// ?: authzClient.protection()
149+
val protection = authzClient.protection()
152150
resource = ResourceRepresentation().apply {
153151
name = resourceSpec.name
154152
scopes = resourceSpec.type.scopes.map { ScopeRepresentation(it.name) }.toSet()
155153
type = resourceSpec.type.name
156-
if (resourceSpec.type.ownerManaged) ownerManagedAccess = true
154+
// if (resourceSpec.type.createByUser) ownerManagedAccess = true
155+
if (resourceSpec.type.createByUser) {
156+
attributes = mapOf(
157+
"created-by" to listOfNotNull(owner?.subject, owner?.getClaim("email")?.asString()),
158+
"creation-timestamp" to listOf(Instant.now().epochSecond.toString())
159+
)
160+
}
157161
}
158162
resource = protection.resource().create(resource)
159163
permissionCache.invalidateAll()
@@ -191,7 +195,7 @@ data class KeycloakResource(val name: String, val type: KeycloakResourceType) {
191195

192196
}
193197

194-
data class KeycloakResourceType(val name: String, val scopes: Set<KeycloakScope>, val ownerManaged: Boolean = false) {
198+
data class KeycloakResourceType(val name: String, val scopes: Set<KeycloakScope>, val createByUser: Boolean = false) {
195199
fun createInstance(resourceName: String) = KeycloakResource(this.name + "/" + resourceName, this)
196200

197201
companion object {

0 commit comments

Comments
 (0)