-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathModelixJWTUtil.kt
390 lines (342 loc) · 14.6 KB
/
ModelixJWTUtil.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package org.modelix.authorization
import com.auth0.jwt.interfaces.DecodedJWT
import com.auth0.jwt.interfaces.Payload
import com.nimbusds.jose.JWSAlgorithm
import com.nimbusds.jose.JWSHeader
import com.nimbusds.jose.JWSObject
import com.nimbusds.jose.JWSSigner
import com.nimbusds.jose.JWSVerifier
import com.nimbusds.jose.KeySourceException
import com.nimbusds.jose.crypto.MACSigner
import com.nimbusds.jose.crypto.RSASSASigner
import com.nimbusds.jose.crypto.factories.DefaultJWSVerifierFactory
import com.nimbusds.jose.jwk.JWK
import com.nimbusds.jose.jwk.JWKMatcher
import com.nimbusds.jose.jwk.JWKSelector
import com.nimbusds.jose.jwk.JWKSet
import com.nimbusds.jose.jwk.KeyType
import com.nimbusds.jose.jwk.KeyUse
import com.nimbusds.jose.jwk.RSAKey
import com.nimbusds.jose.jwk.gen.RSAKeyGenerator
import com.nimbusds.jose.jwk.source.ImmutableJWKSet
import com.nimbusds.jose.jwk.source.JWKSource
import com.nimbusds.jose.jwk.source.RemoteJWKSet
import com.nimbusds.jose.proc.BadJOSEException
import com.nimbusds.jose.proc.JWSAlgorithmFamilyJWSKeySelector
import com.nimbusds.jose.proc.JWSKeySelector
import com.nimbusds.jose.proc.SecurityContext
import com.nimbusds.jose.proc.SingleKeyJWSKeySelector
import com.nimbusds.jose.util.AbstractRestrictedResourceRetriever
import com.nimbusds.jose.util.Resource
import com.nimbusds.jwt.JWTClaimsSet
import com.nimbusds.jwt.JWTParser
import com.nimbusds.jwt.proc.DefaultJWTProcessor
import com.nimbusds.jwt.proc.JWTProcessor
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import io.ktor.http.contentType
import kotlinx.coroutines.runBlocking
import org.modelix.authorization.permissions.PermissionEvaluator
import org.modelix.authorization.permissions.Schema
import org.modelix.authorization.permissions.SchemaInstance
import java.io.File
import java.net.URI
import java.net.URL
import java.security.Key
import java.security.MessageDigest
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.Base64
import java.util.Date
import java.util.UUID
import javax.crypto.spec.SecretKeySpec
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
class ModelixJWTUtil {
private val hmacKeys = LinkedHashMap<JWSAlgorithm, ByteArray>()
private val jwkSources = ArrayList<JWKSource<SecurityContext>>()
private var expectedKeyId: String? = null
private var ktorClient: HttpClient? = null
var accessControlDataProvider: IAccessControlDataProvider = EmptyAccessControlDataProvider()
private var jwtProcessor: JWTProcessor<SecurityContext>? = null
var fileRefreshTime: Duration = 5.seconds
@Synchronized
private fun getOrCreateJwtProcessor(): JWTProcessor<SecurityContext> {
return jwtProcessor ?: DefaultJWTProcessor<SecurityContext>().also { processor ->
val keySelectors: List<JWSKeySelector<SecurityContext>> = hmacKeys.map { it.toPair() }.map {
SingleKeyJWSKeySelector<SecurityContext>(it.first, SecretKeySpec(it.second, it.first.name))
} + jwkSources.map {
try {
JWSAlgorithmFamilyJWSKeySelector.fromJWKSource<SecurityContext>(it)
} catch (ex: KeySourceException) {
throw KeySourceException("Couldn't retrieve JWKs from $it", ex)
}
}
processor.jwsKeySelector = if (keySelectors.size == 1) keySelectors.single() else CompositeJWSKeySelector(keySelectors)
val expectedKeyId = this.expectedKeyId
if (expectedKeyId != null) {
processor.jwsVerifierFactory = object : DefaultJWSVerifierFactory() {
override fun createJWSVerifier(header: JWSHeader, key: Key): JWSVerifier {
if (header.keyID != expectedKeyId) {
throw BadJOSEException("Invalid key ID. [expected=$expectedKeyId, actual=${header.keyID}]")
}
return super.createJWSVerifier(header, key)
}
}
}
}.also { jwtProcessor = it }
}
fun getPrivateKey(): JWK? {
return jwkSources.flatMap {
it.get(JWKSelector(JWKMatcher.Builder().privateOnly(true).algorithms(JWSAlgorithm.Family.RSA.toSet()).build()), null)
}.firstOrNull()
}
private fun resetJwtProcessor() {
jwtProcessor = null
}
@Synchronized
fun canVerifyTokens(): Boolean {
return hmacKeys.isNotEmpty() || jwkSources.isNotEmpty()
}
/**
* Tokens are only valid if they are signed with this key.
*/
@Synchronized
fun requireKeyId(id: String) {
expectedKeyId = id
}
@Synchronized
fun useKtorClient(client: HttpClient) {
resetJwtProcessor()
this.ktorClient = client.config {
expectSuccess = true
}
}
@Synchronized
fun addJwksUrl(url: String) {
addJwksUrl(URI(url).toURL())
}
@Synchronized
fun addJwksUrl(url: URL) {
resetJwtProcessor()
jwkSources.add(RemoteJWKSet(url, ktorClient?.let { KtorResourceRetriever(it) }))
}
fun setHmac512Key(key: String) {
addHmacKey(key, JWSAlgorithm.HS512)
}
fun addHmacKey(key: String, algorithm: JWSAlgorithm) {
// nimbusds checks for weak keys that are shorter than 256 bytes
addHmacKey(key.toByteArray().ensureMinSecretLength(algorithm), algorithm)
}
@Synchronized
fun addPublicKey(key: JWK) {
requireNotNull(key.keyID) { "Key doesn't specify a key ID: $key" }
requireNotNull(key.algorithm) { "Key doesn't specify an algorithm: $key" }
resetJwtProcessor()
jwkSources.add(ImmutableJWKSet(JWKSet(key.toPublicJWK())))
}
@Synchronized
fun setRSAPrivateKey(key: JWK) {
requireNotNull(key.keyID) { "Key doesn't specify a key ID: $key" }
requireNotNull(key.algorithm) { "Key doesn't specify an algorithm: $key" }
resetJwtProcessor()
jwkSources.add(ImmutableJWKSet(JWKSet(listOf(key, key.toPublicJWK()))))
}
@Synchronized
fun addJWK(key: JWK) {
requireNotNull(key.keyID) { "Key doesn't specify a key ID: $key" }
requireNotNull(key.algorithm) { "Key doesn't specify an algorithm: $key" }
resetJwtProcessor()
if (key.isPrivate) {
jwkSources.add(ImmutableJWKSet(JWKSet(listOf(key, key.toPublicJWK()))))
} else {
jwkSources.add(ImmutableJWKSet(JWKSet(key)))
}
}
@Synchronized
private fun addHmacKey(key: ByteArray, algorithm: JWSAlgorithm) {
resetJwtProcessor()
hmacKeys[algorithm] = key
}
@Synchronized
fun loadKeysFromEnvironment() {
resetJwtProcessor()
System.getenv().filter { it.key.startsWith("MODELIX_JWK_FILE") }.values.forEach {
loadKeysFromFiles(File(it))
}
// allows multiple URLs (MODELIX_JWK_URI1, MODELIX_JWK_URI2, MODELIX_JWK_URI_MODEL_SERVER, ...)
System.getenv().filter { it.key.startsWith("MODELIX_JWK_URI") }.values
.forEach { addJwksUrl(URI(it).toURL()) }
}
fun loadKeysFromFiles(fileOrFolder: File) {
fileOrFolder.walk().forEach { file ->
when (file.extension) {
"pem" -> jwkSources.add(PemFileJWKSet(file))
"json" -> jwkSources.add(FileJWKSet(file))
}
}
}
@Synchronized
fun createAccessToken(user: String, grantedPermissions: List<String>, additionalTokenContent: (TokenBuilder) -> Unit = {}): String {
val signer: JWSSigner
val algorithm: JWSAlgorithm
val signingKeyId: String?
val jwk = getPrivateKey()
if (jwk != null) {
signer = RSASSASigner(jwk.toRSAKey().toRSAPrivateKey())
algorithm = checkNotNull(jwk.algorithm) { "RSA key doesn't specify an algorithm" }
.let { it as? JWSAlgorithm ?: JWSAlgorithm.parse(it.name) }
signingKeyId = checkNotNull(jwk.keyID) { "RSA key doesn't specify a key ID" }
} else {
val entry = checkNotNull(hmacKeys.entries.firstOrNull()) { "No keys for signing provided" }
signer = MACSigner(entry.value)
algorithm = entry.key
signingKeyId = null
}
val payload = JWTClaimsSet.Builder()
.claim(KeycloakTokenConstants.PREFERRED_USERNAME, user)
.claim(ModelixTokenConstants.PERMISSIONS, grantedPermissions)
.expirationTime(Date(Instant.now().plus(12, ChronoUnit.HOURS).toEpochMilli()))
.also { additionalTokenContent(TokenBuilder(it)) }
.build()
.toPayload()
val header = JWSHeader.Builder(algorithm).keyID(signingKeyId).build()
return JWSObject(header, payload).also { it.sign(signer) }.serialize()
}
fun isAccessToken(token: DecodedJWT): Boolean {
return extractPermissions(token) != null
}
fun isIdentityToken(token: DecodedJWT): Boolean {
return !isAccessToken(token)
}
fun createPermissionEvaluator(token: DecodedJWT, schema: Schema): PermissionEvaluator {
return createPermissionEvaluator(token, SchemaInstance(schema))
}
fun createPermissionEvaluator(token: DecodedJWT, schema: SchemaInstance): PermissionEvaluator {
return PermissionEvaluator(schema).also { loadGrantedPermissions(token, it) }
}
fun extractPermissions(token: Payload): List<String>? {
return token.claims[ModelixTokenConstants.PERMISSIONS]?.asList(String::class.java)
}
@Synchronized
fun loadGrantedPermissions(token: Payload, evaluator: PermissionEvaluator) {
val permissions = extractPermissions(token)
// There is a difference between access tokens and identity tokens.
// An identity token just contains the user ID and the service has to know the granted permissions.
// An access token has more limited permissions and is issued for a specific task. It contains the list of
// granted permissions. Since tokens are signed and created by a trusted authority we don't have to check the
// list of permissions against our own access control data.
if (permissions != null) {
permissions.forEach { evaluator.grantPermission(it) }
} else {
val directGrants = extractUserId(token)?.let { userId ->
accessControlDataProvider.getGrantedPermissionsForUser(userId)
}.orEmpty() + extractUserRoles(token).flatMap { role ->
accessControlDataProvider.getGrantedPermissionsForRole(role)
}.toSet()
directGrants.forEach { permission ->
evaluator.grantPermission(permission)
}
}
}
fun extractUserId(jwt: Payload): String? {
return Companion.extractUserId(jwt)
}
fun extractUserRoles(jwt: Payload): List<String> {
val keycloakRoles = jwt
.getClaim(KeycloakTokenConstants.REALM_ACCESS)?.asMap()
?.get(KeycloakTokenConstants.REALM_ACCESS_ROLES)
?.let { it as? List<*> }
?.mapNotNull { it as? String }
?: emptyList()
return keycloakRoles
}
fun generateRSAPrivateKey(): JWK {
return RSAKeyGenerator(2048)
.keyUse(KeyUse.SIGNATURE)
.keyID(UUID.randomUUID().toString())
.issueTime(Date())
.algorithm(JWSAlgorithm.RS256)
.generate()
.also { addJWK(it) }
}
private fun ensureValidKey(key: JWK): JWK {
return ensureKeyId(ensureAlgorithmSet(key))
}
private fun ensureAlgorithmSet(key: JWK): JWK {
if (key.algorithm != null) return key
require(key.keyType == KeyType.RSA) { "Unsupported key type: ${key.keyType}" }
return RSAKey.Builder(key.toRSAKey()).algorithm(JWSAlgorithm.RS256).build()
}
private fun ensureKeyId(key: JWK): JWK {
if (key.keyID != null) return key
val rsaKey = key.toRSAKey()
val digest = MessageDigest.getInstance("SHA-256")
digest.update(rsaKey.modulus.decode())
digest.update(0)
digest.update(rsaKey.publicExponent.decode())
val keyId = Base64.getUrlEncoder().withoutPadding().encodeToString(digest.digest())
return RSAKey.Builder(rsaKey).keyID(keyId).build()
}
@Synchronized
fun verifyToken(token: String) {
getOrCreateJwtProcessor().process(JWTParser.parse(token), null)
}
class TokenBuilder(private val builder: JWTClaimsSet.Builder) {
val claimSetBuilder: JWTClaimsSet.Builder get() = builder
fun claim(name: String, value: String) {
builder.claim(name, value)
}
}
private inner class PemFileJWKSet<C : SecurityContext>(pemFile: File) : FileJWKSet<C>(pemFile) {
override fun readFile(): JWKSet {
return JWKSet(ensureValidKey(JWK.parseFromPEMEncodedObjects(file.readText())))
}
override fun toString(): String {
return "PemFileJWKSet[$file]"
}
}
private open inner class FileJWKSet<C : SecurityContext>(val file: File) : JWKSource<C> {
private var loadedAt: Long = 0
private var cached: JWKSet? = null
open fun readFile(): JWKSet {
return JWKSet(JWK.parse(file.readText()))
}
override fun get(jwkSelector: JWKSelector, context: C?): List<JWK?>? {
val jwks = cached.takeIf { System.currentTimeMillis() - loadedAt < fileRefreshTime.inWholeMilliseconds }
?: readFile().let { jwks ->
JWKSet(
jwks.keys.flatMap { key ->
if (key.isPrivate) {
listOf(key, key.toPublicJWK())
} else {
listOf(key)
}
},
)
}.also {
cached = it
loadedAt = System.currentTimeMillis()
}
return jwkSelector.select(jwks)
}
override fun toString(): String {
return "FileJWKSet[$file]"
}
}
companion object {
fun extractUserId(jwt: Payload): String? {
return jwt.getClaim(KeycloakTokenConstants.EMAIL)?.asString()
?: jwt.getClaim(KeycloakTokenConstants.PREFERRED_USERNAME)?.asString()
}
}
}
class KtorResourceRetriever(val client: HttpClient) : AbstractRestrictedResourceRetriever(1000, 1000, 0) {
override fun retrieveResource(url: URL): Resource? {
return runBlocking {
val response = client.get(url.toString())
Resource(response.bodyAsText(), response.contentType()?.toString())
}
}
}