{"record":{"id":"8101432dd8faf82e","repo":"jwtk/jjwt","slug":"unable-to-create-type-getsimplename-from-jwk","errorCode":null,"errorMessage":"Unable to create ${type.getSimpleName()} from JWK ${ctx}: ${e.getMessage()}","messagePattern":"Unable to create (.+?) from JWK (.+?): (.+?)","errorType":"validation","errorClass":"InvalidKeyException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/security/AbstractFamilyJwkFactory.java","lineNumber":94,"sourceCode":"\n    protected String getKeyFactoryJcaName(final JwkContext<?> ctx) {\n        String jcaName = KeysBridge.findAlgorithm(ctx.getKey());\n        return Strings.hasText(jcaName) ? jcaName : getId();\n    }\n\n    protected <T extends Key> T generateKey(final JwkContext<?> ctx, final Class<T> type, final CheckedFunction<KeyFactory, T> fn) {\n        String jcaName = getKeyFactoryJcaName(ctx);\n        JcaTemplate template = new JcaTemplate(jcaName, ctx.getProvider(), ctx.getRandom());\n        return template.withKeyFactory(new CheckedFunction<KeyFactory, T>() {\n            @Override\n            public T apply(KeyFactory instance) {\n                try {\n                    return fn.apply(instance);\n                } catch (KeyException keyException) {\n                    throw keyException; // propagate\n                } catch (Exception e) {\n                    String msg = \"Unable to create \" + type.getSimpleName() + \" from JWK \" + ctx + \": \" + e.getMessage();\n                    throw new InvalidKeyException(msg, e);\n                }\n            }\n        });\n    }\n\n    @Override\n    public final J createJwk(JwkContext<K> ctx) {\n        Assert.notNull(ctx, \"JwkContext argument cannot be null.\");\n        if (!supports(ctx)) { //should be asserted by caller, but assert just in case:\n            String msg = \"Unsupported JwkContext.\";\n            throw new IllegalArgumentException(msg);\n        }\n        K key = ctx.getKey();\n        if (key != null) {\n            ctx.setType(this.ktyValue);\n            return createJwkFromKey(ctx);\n        } else {\n            return createJwkFromValues(ctx);","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/security/AbstractFamilyJwkFactory.java#L76-L112","documentation":"AbstractFamilyJwkFactory.apply wraps exceptions thrown while converting a JWK context into a typed Jwk/Key. Any non-KeyException failure becomes an InvalidKeyException prefixed with the target type and JWK context summary. It indicates malformed or inconsistent JWK fields that broke key reconstruction.","triggerScenarios":"Calling Jwks.builder().build() or parsing a JWK where an arithmetic/decoding error occurs inside the type-specific factory (e.g. base64url-decoded coordinates of the wrong length, BigInteger conversion failures).","commonSituations":"JWK JSON with truncated or non-base64url 'n'/'e'/'x'/'y' values; mismatched kty vs fields; copying keys between systems corrupting values.","solutions":["Validate that all JWK fields are valid base64url strings of the expected length for the curve/modulus.","Confirm the 'kty' matches the concrete builder used (e.g. ECPublicJwkBuilder for EC keys).","Catch InvalidKeyException and log the wrapped cause (e.getCause()) for the underlying failure.","Regenerate or re-export the key if the material is corrupt."],"exampleFix":"// before\ntry { Jwk<?> jwk = Jwks.parser().build().parse(json); }\ncatch (Exception e) { log.error(e.getMessage()); }\n// after\ntry { Jwk<?> jwk = Jwks.parser().build().parse(json); }\ncatch (InvalidKeyException e) { log.error(\"JWK invalid\", e.getCause()); }","handlingStrategy":"try-catch","validationCode":"// pre-check base64url decodability of JWK fields\nBase64.getUrlDecoder().decode(jwk.getString(\"n\"));","typeGuard":null,"tryCatchPattern":"try { Jwk<?> jwk = factory.apply(ctx); }\ncatch (InvalidKeyException e) { log.error(\"JWK {} invalid: {}\", e.getMessage(), e.getCause()); }","preventionTips":["Validate JWK fields against the RFC 7518 schema before building.","Ensure kty matches the concrete builder.","Round-trip test keys through parse/build in CI.","Log the cause chain, not just the message."],"tags":["jwk","invalid-key","key-parsing","jjwt"],"backgroundTag":"invalid-key-format","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}