@panomc/plugin-kit 1.1.0-dev.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,6 +17,7 @@ val pf4jVersion = property("pf4jVersion") as String
17
17
  val vertxVersion = property("vertxVersion") as String
18
18
  val gsonVersion = property("gsonVersion") as String
19
19
  val springContextVersion = property("springContextVersion") as String
20
+ val javaJwtVersion = property("javaJwtVersion") as String
20
21
  val bootstrap = (project.findProperty("bootstrap") as String?)?.toBoolean() ?: false
21
22
  val noui = project.hasProperty("noui")
22
23
  // Where copyJar puts the jar: the pluginsDir of the pano-web-platform tree when this plugin is built inside
@@ -109,6 +110,10 @@ dependencies {
109
110
 
110
111
  // https://mvnrepository.com/artifact/org.springframework/spring-context
111
112
  compileOnly("org.springframework:spring-context:${springContextVersion}")
113
+
114
+ // The license check verifies its token with its OWN java-jwt: bundled and relocated by shadowJar below, so
115
+ // a patched host cannot stand in for it (the host's own java-jwt is never used by the check).
116
+ implementation("com.auth0:java-jwt:${javaJwtVersion}")
112
117
  }
113
118
 
114
119
  val panoLicensePublicKey = findProperty("panoLicensePublicKey") as String?
@@ -301,6 +306,11 @@ internal object PluginBuildConstants {
301
306
 
302
307
  archiveFileName.set("$pluginId-$version.jar")
303
308
 
309
+ // java-jwt (and the Jackson it needs) under this plugin's own package, see the dependency above
310
+ val shaded = "${project.group}.shaded"
311
+ relocate("com.auth0.jwt", "$shaded.jwt")
312
+ relocate("com.fasterxml.jackson", "$shaded.jackson")
313
+
304
314
  dependencies {
305
315
  exclude(dependency("io.vertx:vertx-core"))
306
316
  exclude {
@@ -51,6 +51,7 @@ vertxVersion=5.1.10
51
51
  gsonVersion=2.14.0
52
52
  handlebarsVersion=4.4.0
53
53
  springContextVersion=5.3.39
54
+ javaJwtVersion=4.6.2
54
55
  # GitHub organization or user that owns the repository (only used by the maven publication).
55
56
  organization=@@ORG@@
56
57
  # pf4j's processor is given to kapt explicitly; do not scan the compile classpath for more
@@ -1,10 +1,15 @@
1
1
  package com.panomc.plugins.license
2
2
 
3
+ import com.auth0.jwt.JWT
4
+ import com.auth0.jwt.algorithms.Algorithm
5
+ import com.auth0.jwt.exceptions.JWTVerificationException
6
+ import com.auth0.jwt.exceptions.TokenExpiredException
3
7
  import com.panomc.platform.api.PanoPlugin
4
8
  import com.panomc.platform.license.LicenseDeniedReason
5
9
  import com.panomc.platform.license.LicenseRequiredException
6
- import com.panomc.platform.license.SignedLicense
10
+ import java.io.File
7
11
  import java.security.KeyFactory
12
+ import java.security.MessageDigest
8
13
  import java.security.interfaces.RSAPublicKey
9
14
  import java.security.spec.X509EncodedKeySpec
10
15
  import java.util.Base64
@@ -29,19 +34,28 @@ import java.util.concurrent.atomic.AtomicReference
29
34
  * their own plugin if they want to customise behaviour (e.g. additional anti-tamper
30
35
  * measures). The boilerplate ships it as the canonical implementation.
31
36
  *
37
+ * The host is open source and not trusted here. The token is verified inside the plugin with the
38
+ * plugin's own java-jwt (bundled into the plugin jar and relocated by build.gradle.kts, so no host
39
+ * class can stand in for it), and every value that is checked (aud, sub, ver, hash, exp) is read from
40
+ * that verified token, never from the claims the host parsed. The jar hash is computed here from the
41
+ * plugin's own CodeSource, not asked from the host.
42
+ *
32
43
  * For free plugins (PANO_PUBLIC_KEY_BASE64 is empty), [requireValidLicense] is a no-op so
33
44
  * a plugin can be migrated to/from premium without code changes — only by updating
34
45
  * build flags or `PANO_LICENSE_*` / `licenseServer` inputs for the Gradle build.
35
46
  */
36
47
  class PluginLicenseClient(private val plugin: PanoPlugin) {
37
- /** Last successfully verified license. Cached so [assertStillLicensed] is cheap. */
38
- private val cachedLicense = AtomicReference<SignedLicense?>(null)
48
+ /** Claims of the last successfully verified license. Cached so [assertStillLicensed] is cheap. */
49
+ private val cachedClaims = AtomicReference<VerifiedClaims?>(null)
39
50
 
40
51
  private val embeddedPublicKey: RSAPublicKey? by lazy { decodePublicKey() }
41
52
 
42
- /** True when this plugin is configured as premium (i.e. embedded a public key). */
53
+ /**
54
+ * True when this plugin is configured as premium (a public key was embedded at build time). A key that
55
+ * cannot be parsed still counts as premium: [requireValidLicense] then fails with KEY_NOT_AVAILABLE.
56
+ */
43
57
  val isPremium: Boolean
44
- get() = embeddedPublicKey != null
58
+ get() = PluginBuildConstants.PANO_PUBLIC_KEY_BASE64.isNotBlank()
45
59
 
46
60
  /**
47
61
  * Fetches a fresh license from panomc.com via the host, verifies it locally with the
@@ -66,49 +80,60 @@ class PluginLicenseClient(private val plugin: PanoPlugin) {
66
80
  version = PluginBuildConstants.VERSION
67
81
  )
68
82
 
69
- // Re-verify with our OWN copy of the public key. This is the plugin's actual
70
- // security boundary: even if the host is patched/forked to inject a forged JWT,
71
- // an attacker can't sign one without panomc.com's private key.
72
- if (!license.verifySignature(pubKey, plugin.getLicenseJwtIssuer())) {
73
- throw LicenseRequiredException(
83
+ // Re-verify with our OWN copy of the public key and our OWN java-jwt. This is the plugin's
84
+ // actual security boundary: even if the host is patched/forked to inject a forged JWT or other
85
+ // claims, an attacker can't sign one without panomc.com's private key.
86
+ val claims = when (val result = verifyToken(license.rawJwt, pubKey, plugin.getLicenseJwtIssuer())) {
87
+ is Verification.Valid -> result.claims
88
+ Verification.Expired -> throw LicenseRequiredException(
89
+ plugin.pluginId,
90
+ LicenseDeniedReason.EXPIRED,
91
+ "license expired"
92
+ )
93
+ Verification.Invalid -> throw LicenseRequiredException(
74
94
  plugin.pluginId,
75
95
  LicenseDeniedReason.SIGNATURE_INVALID,
76
96
  "license JWT signature is invalid (host may be tampered)"
77
97
  )
78
98
  }
79
99
 
80
- if (!license.claims.resourceId.equals(plugin.pluginId, ignoreCase = true)) {
100
+ if (claims.audience.none { it.equals(plugin.pluginId, ignoreCase = true) }) {
81
101
  throw LicenseRequiredException(
82
102
  plugin.pluginId,
83
103
  LicenseDeniedReason.AUDIENCE_MISMATCH,
84
- "expected ${plugin.pluginId}, got ${license.claims.resourceId}"
104
+ "expected ${plugin.pluginId}, got ${claims.audience.joinToString()}"
85
105
  )
86
106
  }
87
- if (license.claims.version != PluginBuildConstants.VERSION) {
107
+ if (claims.version != PluginBuildConstants.VERSION) {
88
108
  throw LicenseRequiredException(
89
109
  plugin.pluginId,
90
110
  LicenseDeniedReason.VERSION_MISMATCH,
91
- "expected ${PluginBuildConstants.VERSION}, got ${license.claims.version}"
111
+ "expected ${PluginBuildConstants.VERSION}, got ${claims.version}"
92
112
  )
93
113
  }
94
114
 
95
- val ownHash = plugin.getOwnJarSha256()
96
- if (ownHash != null && !license.claims.jarSha256.equals(ownHash, ignoreCase = true)) {
115
+ val ownHash = ownJarSha256()
116
+ ?: throw LicenseRequiredException(
117
+ plugin.pluginId,
118
+ LicenseDeniedReason.JAR_HASH_MISMATCH,
119
+ "the plugin is not running from its jar"
120
+ )
121
+ if (!claims.jarSha256.equals(ownHash, ignoreCase = true)) {
97
122
  throw LicenseRequiredException(
98
123
  plugin.pluginId,
99
124
  LicenseDeniedReason.JAR_HASH_MISMATCH,
100
- "expected ${license.claims.jarSha256.take(12)}..., running ${ownHash.take(12)}..."
125
+ "expected ${claims.jarSha256.take(12)}..., running ${ownHash.take(12)}..."
101
126
  )
102
127
  }
103
- if (license.claims.isExpired()) {
128
+ if (claims.isExpired()) {
104
129
  throw LicenseRequiredException(
105
130
  plugin.pluginId,
106
131
  LicenseDeniedReason.EXPIRED,
107
- "license expired (${license.claims.expiresAtMs})"
132
+ "license expired (${claims.expiresAtMs})"
108
133
  )
109
134
  }
110
135
 
111
- cachedLicense.set(license)
136
+ cachedClaims.set(claims)
112
137
  }
113
138
 
114
139
  /**
@@ -119,8 +144,8 @@ class PluginLicenseClient(private val plugin: PanoPlugin) {
119
144
  */
120
145
  suspend fun assertStillLicensed() {
121
146
  if (!isPremium) return
122
- val cached = cachedLicense.get()
123
- if (cached == null || cached.claims.isExpired()) {
147
+ val cached = cachedClaims.get()
148
+ if (cached == null || cached.isExpired()) {
124
149
  requireValidLicense()
125
150
  }
126
151
  }
@@ -141,4 +166,73 @@ class PluginLicenseClient(private val plugin: PanoPlugin) {
141
166
  null
142
167
  }
143
168
  }
169
+
170
+ /** The claims this plugin checks, read from a token whose signature it verified itself. */
171
+ class VerifiedClaims(
172
+ val subject: String,
173
+ val audience: List<String>,
174
+ val version: String,
175
+ val jarSha256: String,
176
+ val expiresAtMs: Long,
177
+ ) {
178
+ fun isExpired(nowMs: Long = System.currentTimeMillis()): Boolean = nowMs >= expiresAtMs
179
+ }
180
+
181
+ sealed class Verification {
182
+ class Valid(val claims: VerifiedClaims) : Verification()
183
+ object Expired : Verification()
184
+ object Invalid : Verification()
185
+ }
186
+
187
+ companion object {
188
+ /**
189
+ * Verifies an RS256 JWT with [publicKey] and [expectedIssuer] and returns its claims. [Verification.Invalid]
190
+ * when the token is malformed, wrongly signed, from another issuer, or misses sub, aud, ver, hash or exp.
191
+ */
192
+ fun verifyToken(rawJwt: String, publicKey: RSAPublicKey, expectedIssuer: String): Verification {
193
+ val decoded = try {
194
+ JWT.require(Algorithm.RSA256(publicKey, null))
195
+ .withIssuer(expectedIssuer)
196
+ .build()
197
+ .verify(rawJwt)
198
+ } catch (_: TokenExpiredException) {
199
+ return Verification.Expired
200
+ } catch (_: JWTVerificationException) {
201
+ return Verification.Invalid
202
+ } catch (_: Exception) {
203
+ return Verification.Invalid
204
+ }
205
+
206
+ val subject = decoded.subject?.takeIf { it.isNotBlank() }
207
+ val audience = decoded.audience.orEmpty().filter { it.isNotBlank() }
208
+ val version = decoded.getClaim("ver").asString()
209
+ val hash = decoded.getClaim("hash").asString()?.takeIf { it.isNotBlank() }
210
+ val expiresAt = decoded.expiresAt
211
+ if (subject == null || audience.isEmpty() || version == null || hash == null || expiresAt == null) {
212
+ return Verification.Invalid
213
+ }
214
+
215
+ return Verification.Valid(VerifiedClaims(subject, audience, version, hash, expiresAt.time))
216
+ }
217
+
218
+ /** SHA-256 (hex) of the jar this class was loaded from, or null when it is not loaded from a jar file. */
219
+ fun ownJarSha256(): String? = try {
220
+ val location = PluginLicenseClient::class.java.protectionDomain?.codeSource?.location
221
+ val file = location?.toURI()?.let(::File)
222
+ if (file == null || !file.isFile) null else {
223
+ val digest = MessageDigest.getInstance("SHA-256")
224
+ file.inputStream().use { input ->
225
+ val buffer = ByteArray(64 * 1024)
226
+ while (true) {
227
+ val read = input.read(buffer)
228
+ if (read < 0) break
229
+ digest.update(buffer, 0, read)
230
+ }
231
+ }
232
+ digest.digest().joinToString("") { "%02x".format(it) }
233
+ }
234
+ } catch (_: Exception) {
235
+ null
236
+ }
237
+ }
144
238
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@panomc/plugin-kit",
3
- "version": "1.1.0-dev.1",
3
+ "version": "1.1.0",
4
4
  "description": "Build tooling for Pano plugins: the panoPlugin() rollup preset, the pano-plugin CLI and the controller core.",
5
5
  "author": "PanoMC",
6
6
  "license": "MIT",
@@ -27,7 +27,7 @@
27
27
  "access": "public"
28
28
  },
29
29
  "dependencies": {
30
- "@panomc/client-gen": "1.1.0-dev.1",
30
+ "@panomc/client-gen": "1.1.0",
31
31
  "@rollup/plugin-node-resolve": "^16.0.3",
32
32
  "@rollup/plugin-terser": "^1.0.0",
33
33
  "magic-string": "^1.4.3",
@@ -37,11 +37,11 @@
37
37
  "rollup-plugin-svelte": "^7.2.3"
38
38
  },
39
39
  "peerDependencies": {
40
- "@panomc/sdk": "^2.0.0-dev.1",
40
+ "@panomc/sdk": "^2.0.0",
41
41
  "svelte": "*"
42
42
  },
43
43
  "devDependencies": {
44
- "@panomc/sdk": "2.0.0-dev.1",
44
+ "@panomc/sdk": "2.0.0",
45
45
  "bootstrap": "5.3.8",
46
46
  "happy-dom": "^21.0.1",
47
47
  "sass": "^1.105.1",