{"action":"created","comment":{"url":"https://api.github.com/repos/dotCMS/core/pulls/comments/192425188","pull_request_review_id":125204807,"id":192425188,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDE5MjQyNTE4OA==","diff_hunk":"@@ -0,0 +1,173 @@\n+package com.dotcms.auth.providers.jwt.factories.impl;\n+\n+import com.dotcms.auth.providers.jwt.factories.KeyFactoryUtils;\n+import com.dotcms.auth.providers.jwt.factories.SigningKeyFactory;\n+import com.dotmarketing.util.Config;\n+import com.dotmarketing.util.Logger;\n+import io.jsonwebtoken.SignatureAlgorithm;\n+import java.security.Key;\n+import java.security.MessageDigest;\n+import java.security.NoSuchAlgorithmException;\n+import java.security.SecureRandom;\n+import java.util.Locale;\n+import java.util.Random;\n+import javax.crypto.spec.SecretKeySpec;\n+import javax.xml.bind.DatatypeConverter;\n+\n+/**\n+ * @author Jonathan Gamba 5/29/18\n+ */\n+public class SecretKeySpecFactoryImpl implements SigningKeyFactory {\n+\n+    public static final String DEFAULT_SECRET = \"26b30b77909b6a91ec10c0692c6b82f851360b6e3dbffc685137dbf554da6d1c2540a3d4196ecdd9e9585320e6554261c99522e1e7ac824e589f8007882281bc\";\n+\n+    @Override\n+    public Key getKey() {\n+\n+        //The JWT signature algorithm we will be using to sign the token\n+        final SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;\n+\n+        //Read the secret to use\n+        final String secret = getAndProcessSecret();\n+\n+        //We will sign our JWT with our app secret key\n+        byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(secret);\n+        return new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());\n+    }\n+\n+    /**\n+     * Returns a secret, the secret will be read from a property <strong>json.web.token.hash.signing.key</strong>\n+     * or from an file inside the assets folder, if not secret is provided we will generate one for\n+     * the client.\n+     *\n+     * @return The secret to use for signing the token\n+     */\n+    private String getAndProcessSecret() {\n+\n+        final KeyFactoryUtils factoryUtils = KeyFactoryUtils.getInstance();\n+\n+        //Read the secret hash\n+        String secret = Config\n+                .getStringProperty(\n+                        \"json.web.token.hash.signing.key\",\n+                        DEFAULT_SECRET);\n+\n+        //The secret was not changed using the config properties file\n+        if (DEFAULT_SECRET.equals(secret)) {\n+\n+            //Verify if the have a secret file in the assets folder\n+            if (factoryUtils.existSecretFile()) {\n+\n+                //Read the secret file from the assets folder\n+                final String secretInSharedStorage = factoryUtils.readSecretFromDisk();\n+                //And verify is not our default secret\n+                if (DEFAULT_SECRET.equals(secretInSharedStorage)) {\n+                    //Generate a new secret\n+                    secret = generateSecret();\n+                    factoryUtils.writeSecretToDisk(secret);\n+                } else {\n+                    //Use the secret found in the assets folder\n+                    secret = secretInSharedStorage;\n+                }\n+\n+            } else {//There is not secret file in the assets folder\n+                //Generate a new secret\n+                secret = generateSecret();\n+                factoryUtils.writeSecretToDisk(secret);\n+            }\n+        } else {\n+            // Use the override secret and write it to the assets folder\n+            factoryUtils.writeSecretToDisk(secret);\n+        }\n+\n+        return secret;\n+    }\n+\n+    /**\n+     * Generates a random secret\n+     */\n+    private String generateSecret() {\n+\n+        try {\n+            final String randomString = new RandomString().nextString();\n+            return getSHA512(randomString, getSalt());\n+        } catch (NoSuchAlgorithmException e) {\n+            Logger.error(this.getClass(),\n+                    String.format(\"Unable to generate JWT secret [%s]\", e.getMessage()), e);\n+        }\n+\n+        return DEFAULT_SECRET;\n+    }\n+\n+    /**\n+     * Applies a <strong>SHA-512</strong> algorithm to a given secret\n+     */\n+    private String getSHA512(String toHash, byte[] salt) throws NoSuchAlgorithmException {\n+\n+        MessageDigest md = MessageDigest.getInstance(\"SHA-512\");\n+        md.update(salt);\n+        byte[] bytes = md.digest(toHash.getBytes());\n+        StringBuilder sb = new StringBuilder();\n+        for (byte aByte : bytes) {\n+            sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1));\n+        }\n+        return sb.toString();\n+    }\n+\n+    /**\n+     * Returns a SecureRandom byte array that implements a <strong>SHA1PRNG</strong> Random Number\n+     * Generator (RNG) algorithm.\n+     */\n+    private byte[] getSalt() throws NoSuchAlgorithmException {\n+\n+        SecureRandom sr = SecureRandom.getInstance(\"SHA1PRNG\");\n+        byte[] salt = new byte[16];\n+        sr.nextBytes(salt);\n+        return salt;\n+    }\n+\n+    class RandomString {\n+\n+        final static String upper = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n+        final static String digits = \"0123456789\";\n+\n+        final Random random;\n+        final char[] symbols;\n+        final char[] buf;\n+\n+        /**\n+         * Creates an alphanumeric strings from a secure generator\n+         */\n+        RandomString(final int length) {\n+\n+            if (length < 1) {\n+                throw new IllegalArgumentException();\n+            }\n+\n+            String symbolsToUse = upper + upper.toLowerCase(Locale.ROOT) + digits;","path":"dotCMS/src/main/java/com/dotcms/auth/providers/jwt/factories/impl/SecretKeySpecFactoryImpl.java","position":147,"original_position":147,"commit_id":"492a208431df0fa966a27f9b636367bca301b1ab","original_commit_id":"492a208431df0fa966a27f9b636367bca301b1ab","user":{"login":"codacy-bot","id":19940114,"node_id":"MDQ6VXNlcjE5OTQwMTE0","avatar_url":"https://avatars1.githubusercontent.com/u/19940114?v=4","gravatar_id":"","url":"https://api.github.com/users/codacy-bot","html_url":"https://github.com/codacy-bot","followers_url":"https://api.github.com/users/codacy-bot/followers","following_url":"https://api.github.com/users/codacy-bot/following{/other_user}","gists_url":"https://api.github.com/users/codacy-bot/gists{/gist_id}","starred_url":"https://api.github.com/users/codacy-bot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/codacy-bot/subscriptions","organizations_url":"https://api.github.com/users/codacy-bot/orgs","repos_url":"https://api.github.com/users/codacy-bot/repos","events_url":"https://api.github.com/users/codacy-bot/events{/privacy}","received_events_url":"https://api.github.com/users/codacy-bot/received_events","type":"User","site_admin":false},"body":"![Codacy](https://app.codacy.com/assets/images/favicon.png) Issue found: [Local variable 'symbolsToUse' could be declared final](https://app.codacy.com/app/dotCMS/core/pullRequest?prid=1721338)","created_at":"2018-06-01T15:05:32Z","updated_at":"2018-06-01T15:05:33Z","html_url":"https://github.com/dotCMS/core/pull/14535#discussion_r192425188","pull_request_url":"https://api.github.com/repos/dotCMS/core/pulls/14535","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/dotCMS/core/pulls/comments/192425188"},"html":{"href":"https://github.com/dotCMS/core/pull/14535#discussion_r192425188"},"pull_request":{"href":"https://api.github.com/repos/dotCMS/core/pulls/14535"}}},"pull_request":{"url":"https://api.github.com/repos/dotCMS/core/pulls/14535","id":191881535,"node_id":"MDExOlB1bGxSZXF1ZXN0MTkxODgxNTM1","html_url":"https://github.com/dotCMS/core/pull/14535","diff_url":"https://github.com/dotCMS/core/pull/14535.diff","patch_url":"https://github.com/dotCMS/core/pull/14535.patch","issue_url":"https://api.github.com/repos/dotCMS/core/issues/14535","number":14535,"state":"open","locked":false,"title":"Issue 13451 13453 jwt","user":{"login":"jgambarios","id":923947,"node_id":"MDQ6VXNlcjkyMzk0Nw==","avatar_url":"https://avatars1.githubusercontent.com/u/923947?v=4","gravatar_id":"","url":"https://api.github.com/users/jgambarios","html_url":"https://github.com/jgambarios","followers_url":"https://api.github.com/users/jgambarios/followers","following_url":"https://api.github.com/users/jgambarios/following{/other_user}","gists_url":"https://api.github.com/users/jgambarios/gists{/gist_id}","starred_url":"https://api.github.com/users/jgambarios/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jgambarios/subscriptions","organizations_url":"https://api.github.com/users/jgambarios/orgs","repos_url":"https://api.github.com/users/jgambarios/repos","events_url":"https://api.github.com/users/jgambarios/events{/privacy}","received_events_url":"https://api.github.com/users/jgambarios/received_events","type":"User","site_admin":false},"body":"","created_at":"2018-05-31T21:41:15Z","updated_at":"2018-06-01T15:05:33Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ccdf6434e596899737aff69934dddf08931ca29c","assignee":null,"assignees":[],"requested_reviewers":[{"login":"wezell","id":934364,"node_id":"MDQ6VXNlcjkzNDM2NA==","avatar_url":"https://avatars2.githubusercontent.com/u/934364?v=4","gravatar_id":"","url":"https://api.github.com/users/wezell","html_url":"https://github.com/wezell","followers_url":"https://api.github.com/users/wezell/followers","following_url":"https://api.github.com/users/wezell/following{/other_user}","gists_url":"https://api.github.com/users/wezell/gists{/gist_id}","starred_url":"https://api.github.com/users/wezell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wezell/subscriptions","organizations_url":"https://api.github.com/users/wezell/orgs","repos_url":"https://api.github.com/users/wezell/repos","events_url":"https://api.github.com/users/wezell/events{/privacy}","received_events_url":"https://api.github.com/users/wezell/received_events","type":"User","site_admin":false}],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/dotCMS/core/pulls/14535/commits","review_comments_url":"https://api.github.com/repos/dotCMS/core/pulls/14535/comments","review_comment_url":"https://api.github.com/repos/dotCMS/core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotCMS/core/issues/14535/comments","statuses_url":"https://api.github.com/repos/dotCMS/core/statuses/492a208431df0fa966a27f9b636367bca301b1ab","head":{"label":"dotCMS:issue-13451-13453-JWT","ref":"issue-13451-13453-JWT","sha":"492a208431df0fa966a27f9b636367bca301b1ab","user":{"login":"dotCMS","id":1005263,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEwMDUyNjM=","avatar_url":"https://avatars3.githubusercontent.com/u/1005263?v=4","gravatar_id":"","url":"https://api.github.com/users/dotCMS","html_url":"https://github.com/dotCMS","followers_url":"https://api.github.com/users/dotCMS/followers","following_url":"https://api.github.com/users/dotCMS/following{/other_user}","gists_url":"https://api.github.com/users/dotCMS/gists{/gist_id}","starred_url":"https://api.github.com/users/dotCMS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotCMS/subscriptions","organizations_url":"https://api.github.com/users/dotCMS/orgs","repos_url":"https://api.github.com/users/dotCMS/repos","events_url":"https://api.github.com/users/dotCMS/events{/privacy}","received_events_url":"https://api.github.com/users/dotCMS/received_events","type":"Organization","site_admin":false},"repo":{"id":3729629,"node_id":"MDEwOlJlcG9zaXRvcnkzNzI5NjI5","name":"core","full_name":"dotCMS/core","owner":{"login":"dotCMS","id":1005263,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEwMDUyNjM=","avatar_url":"https://avatars3.githubusercontent.com/u/1005263?v=4","gravatar_id":"","url":"https://api.github.com/users/dotCMS","html_url":"https://github.com/dotCMS","followers_url":"https://api.github.com/users/dotCMS/followers","following_url":"https://api.github.com/users/dotCMS/following{/other_user}","gists_url":"https://api.github.com/users/dotCMS/gists{/gist_id}","starred_url":"https://api.github.com/users/dotCMS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotCMS/subscriptions","organizations_url":"https://api.github.com/users/dotCMS/orgs","repos_url":"https://api.github.com/users/dotCMS/repos","events_url":"https://api.github.com/users/dotCMS/events{/privacy}","received_events_url":"https://api.github.com/users/dotCMS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotCMS/core","description":"Source Code for dotCMS Java Enterprise Content Management System","fork":false,"url":"https://api.github.com/repos/dotCMS/core","forks_url":"https://api.github.com/repos/dotCMS/core/forks","keys_url":"https://api.github.com/repos/dotCMS/core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotCMS/core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotCMS/core/teams","hooks_url":"https://api.github.com/repos/dotCMS/core/hooks","issue_events_url":"https://api.github.com/repos/dotCMS/core/issues/events{/number}","events_url":"https://api.github.com/repos/dotCMS/core/events","assignees_url":"https://api.github.com/repos/dotCMS/core/assignees{/user}","branches_url":"https://api.github.com/repos/dotCMS/core/branches{/branch}","tags_url":"https://api.github.com/repos/dotCMS/core/tags","blobs_url":"https://api.github.com/repos/dotCMS/core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotCMS/core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotCMS/core/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotCMS/core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotCMS/core/statuses/{sha}","languages_url":"https://api.github.com/repos/dotCMS/core/languages","stargazers_url":"https://api.github.com/repos/dotCMS/core/stargazers","contributors_url":"https://api.github.com/repos/dotCMS/core/contributors","subscribers_url":"https://api.github.com/repos/dotCMS/core/subscribers","subscription_url":"https://api.github.com/repos/dotCMS/core/subscription","commits_url":"https://api.github.com/repos/dotCMS/core/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotCMS/core/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotCMS/core/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotCMS/core/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotCMS/core/contents/{+path}","compare_url":"https://api.github.com/repos/dotCMS/core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotCMS/core/merges","archive_url":"https://api.github.com/repos/dotCMS/core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotCMS/core/downloads","issues_url":"https://api.github.com/repos/dotCMS/core/issues{/number}","pulls_url":"https://api.github.com/repos/dotCMS/core/pulls{/number}","milestones_url":"https://api.github.com/repos/dotCMS/core/milestones{/number}","notifications_url":"https://api.github.com/repos/dotCMS/core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotCMS/core/labels{/name}","releases_url":"https://api.github.com/repos/dotCMS/core/releases{/id}","deployments_url":"https://api.github.com/repos/dotCMS/core/deployments","created_at":"2012-03-15T15:24:14Z","updated_at":"2018-06-01T15:03:22Z","pushed_at":"2018-06-01T14:54:25Z","git_url":"git://github.com/dotCMS/core.git","ssh_url":"git@github.com:dotCMS/core.git","clone_url":"https://github.com/dotCMS/core.git","svn_url":"https://github.com/dotCMS/core","homepage":"http://dotcms.com","size":330473,"stargazers_count":374,"watchers_count":374,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":313,"mirror_url":null,"archived":false,"open_issues_count":1890,"license":{"key":"gpl-3.0","name":"GNU General Public License v3.0","spdx_id":"GPL-3.0","url":"https://api.github.com/licenses/gpl-3.0","node_id":"MDc6TGljZW5zZTk="},"forks":313,"open_issues":1890,"watchers":374,"default_branch":"master"}},"base":{"label":"dotCMS:master","ref":"master","sha":"613716713431f36819bc0738b3c41c5bbf04882c","user":{"login":"dotCMS","id":1005263,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEwMDUyNjM=","avatar_url":"https://avatars3.githubusercontent.com/u/1005263?v=4","gravatar_id":"","url":"https://api.github.com/users/dotCMS","html_url":"https://github.com/dotCMS","followers_url":"https://api.github.com/users/dotCMS/followers","following_url":"https://api.github.com/users/dotCMS/following{/other_user}","gists_url":"https://api.github.com/users/dotCMS/gists{/gist_id}","starred_url":"https://api.github.com/users/dotCMS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotCMS/subscriptions","organizations_url":"https://api.github.com/users/dotCMS/orgs","repos_url":"https://api.github.com/users/dotCMS/repos","events_url":"https://api.github.com/users/dotCMS/events{/privacy}","received_events_url":"https://api.github.com/users/dotCMS/received_events","type":"Organization","site_admin":false},"repo":{"id":3729629,"node_id":"MDEwOlJlcG9zaXRvcnkzNzI5NjI5","name":"core","full_name":"dotCMS/core","owner":{"login":"dotCMS","id":1005263,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEwMDUyNjM=","avatar_url":"https://avatars3.githubusercontent.com/u/1005263?v=4","gravatar_id":"","url":"https://api.github.com/users/dotCMS","html_url":"https://github.com/dotCMS","followers_url":"https://api.github.com/users/dotCMS/followers","following_url":"https://api.github.com/users/dotCMS/following{/other_user}","gists_url":"https://api.github.com/users/dotCMS/gists{/gist_id}","starred_url":"https://api.github.com/users/dotCMS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotCMS/subscriptions","organizations_url":"https://api.github.com/users/dotCMS/orgs","repos_url":"https://api.github.com/users/dotCMS/repos","events_url":"https://api.github.com/users/dotCMS/events{/privacy}","received_events_url":"https://api.github.com/users/dotCMS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotCMS/core","description":"Source Code for dotCMS Java Enterprise Content Management System","fork":false,"url":"https://api.github.com/repos/dotCMS/core","forks_url":"https://api.github.com/repos/dotCMS/core/forks","keys_url":"https://api.github.com/repos/dotCMS/core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotCMS/core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotCMS/core/teams","hooks_url":"https://api.github.com/repos/dotCMS/core/hooks","issue_events_url":"https://api.github.com/repos/dotCMS/core/issues/events{/number}","events_url":"https://api.github.com/repos/dotCMS/core/events","assignees_url":"https://api.github.com/repos/dotCMS/core/assignees{/user}","branches_url":"https://api.github.com/repos/dotCMS/core/branches{/branch}","tags_url":"https://api.github.com/repos/dotCMS/core/tags","blobs_url":"https://api.github.com/repos/dotCMS/core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotCMS/core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotCMS/core/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotCMS/core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotCMS/core/statuses/{sha}","languages_url":"https://api.github.com/repos/dotCMS/core/languages","stargazers_url":"https://api.github.com/repos/dotCMS/core/stargazers","contributors_url":"https://api.github.com/repos/dotCMS/core/contributors","subscribers_url":"https://api.github.com/repos/dotCMS/core/subscribers","subscription_url":"https://api.github.com/repos/dotCMS/core/subscription","commits_url":"https://api.github.com/repos/dotCMS/core/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotCMS/core/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotCMS/core/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotCMS/core/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotCMS/core/contents/{+path}","compare_url":"https://api.github.com/repos/dotCMS/core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotCMS/core/merges","archive_url":"https://api.github.com/repos/dotCMS/core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotCMS/core/downloads","issues_url":"https://api.github.com/repos/dotCMS/core/issues{/number}","pulls_url":"https://api.github.com/repos/dotCMS/core/pulls{/number}","milestones_url":"https://api.github.com/repos/dotCMS/core/milestones{/number}","notifications_url":"https://api.github.com/repos/dotCMS/core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotCMS/core/labels{/name}","releases_url":"https://api.github.com/repos/dotCMS/core/releases{/id}","deployments_url":"https://api.github.com/repos/dotCMS/core/deployments","created_at":"2012-03-15T15:24:14Z","updated_at":"2018-06-01T15:03:22Z","pushed_at":"2018-06-01T14:54:25Z","git_url":"git://github.com/dotCMS/core.git","ssh_url":"git@github.com:dotCMS/core.git","clone_url":"https://github.com/dotCMS/core.git","svn_url":"https://github.com/dotCMS/core","homepage":"http://dotcms.com","size":330473,"stargazers_count":374,"watchers_count":374,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":313,"mirror_url":null,"archived":false,"open_issues_count":1890,"license":{"key":"gpl-3.0","name":"GNU General Public License v3.0","spdx_id":"GPL-3.0","url":"https://api.github.com/licenses/gpl-3.0","node_id":"MDc6TGljZW5zZTk="},"forks":313,"open_issues":1890,"watchers":374,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotCMS/core/pulls/14535"},"html":{"href":"https://github.com/dotCMS/core/pull/14535"},"issue":{"href":"https://api.github.com/repos/dotCMS/core/issues/14535"},"comments":{"href":"https://api.github.com/repos/dotCMS/core/issues/14535/comments"},"review_comments":{"href":"https://api.github.com/repos/dotCMS/core/pulls/14535/comments"},"review_comment":{"href":"https://api.github.com/repos/dotCMS/core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotCMS/core/pulls/14535/commits"},"statuses":{"href":"https://api.github.com/repos/dotCMS/core/statuses/492a208431df0fa966a27f9b636367bca301b1ab"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"codacy-bot"}}