{"action":"created","comment":{"url":"https://api.github.com/repos/wso2/carbon-identity/pulls/comments/46017402","id":46017402,"diff_hunk":"@@ -0,0 +1,103 @@\n+/*\n+ * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.\n+ *\n+ * WSO2 Inc. licenses this file to you under the Apache License,\n+ * Version 2.0 (the \"License\"); you may not use this file except\n+ * in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing,\n+ * software distributed under the License is distributed on an\n+ * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n+ * KIND, either express or implied.  See the License for the\n+ * specific language governing permissions and limitations\n+ * under the License.\n+ */\n+\n+package org.wso2.carbon.identity.oauth2.token.handlers.grant.iwa.ntlm;\n+\n+import org.apache.commons.logging.Log;\n+import org.apache.commons.logging.LogFactory;\n+import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;\n+import org.wso2.carbon.identity.oauth2.ResponseHeader;\n+import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;\n+import org.wso2.carbon.identity.oauth2.token.handlers.grant.AbstractAuthorizationGrantHandler;\n+import org.wso2.carbon.identity.oauth2.util.OAuth2Util;\n+import waffle.util.Base64;\n+import waffle.windows.auth.IWindowsSecurityContext;\n+import waffle.windows.auth.impl.WindowsAuthProviderImpl;\n+\n+public class NTLMAuthenticationGrantHandlerWithHandshake extends AbstractAuthorizationGrantHandler  {\n+    private static Log log = LogFactory.getLog(NTLMAuthenticationGrantHandlerWithHandshake.class);\n+    private static WindowsAuthProviderImpl provider = new WindowsAuthProviderImpl();\n+    private static final String securityPackage = \"Negotiate\";\n+\n+    public int getNLTMMessageType(byte[] decodedNLTMMessage) throws IdentityOAuth2Exception {\n+        int messageType;\n+        if (decodedNLTMMessage.length > 8) {\n+            messageType = decodedNLTMMessage[8];\n+        } else {\n+            throw new IdentityOAuth2Exception(\n+                    \"Cannot extract message type from NLTM Token. Decoded token length is less than 8.\");\n+        }\n+\n+        //NLTM token type must be one of 1,2 or 3\n+        if (messageType < 1 || messageType > 3) {\n+            throw new IdentityOAuth2Exception(\n+                    \"Invalid NLTM message type:\" + messageType + \". Should be one of 1,2 or 3.\");\n+        }\n+\n+        return messageType;\n+    }\n+\n+    @Override\n+    public boolean validateGrant(OAuthTokenReqMessageContext tokReqMsgCtx) throws IdentityOAuth2Exception {\n+        boolean validGrant = super.validateGrant(tokReqMsgCtx);\n+\n+        if (!validGrant) {\n+            return false;\n+        }\n+\n+        String token = tokReqMsgCtx.getOauth2AccessTokenReqDTO().getWindowsToken();\n+\n+        IWindowsSecurityContext serverContext = null;\n+        if (token != null) {\n+\n+            byte[] bytesToken = Base64.decode(token);\n+            int tokenType = getNLTMMessageType(bytesToken);\n+\n+            if (log.isDebugEnabled()) {\n+                log.debug(\"Received NTLM token Type \" + tokenType + \":\" + token);\n+            }\n+\n+            if (tokenType == 1) {\n+                serverContext = provider.acceptSecurityToken(\"server-connection\", bytesToken, securityPackage);\n+                String type2Token = Base64.encode(serverContext.getToken());\n+                if (log.isDebugEnabled()) {\n+                    log.debug(\"Sent NTLM token Type 2:\" + type2Token);\n+                }\n+                ResponseHeader[] responseHeaders = new ResponseHeader[1];\n+                responseHeaders[0] = new ResponseHeader();\n+                responseHeaders[0].setKey(\"WWW-Authenticate\");\n+                responseHeaders[0].setValue(\"NTLM \" + type2Token);\n+                tokReqMsgCtx.addProperty(\"RESPONSE_HEADERS_PROPERTY\", responseHeaders);\n+                return false;\n+            } else if (tokenType == 3) {","path":"components/oauth/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/token/handlers/grant/iwa/ntlm/NTLMAuthenticationGrantHandlerWithHandshake.java","position":87,"original_position":87,"commit_id":"262d216ebc85650ecb69222cc8964a7d06f2f7c4","original_commit_id":"262d216ebc85650ecb69222cc8964a7d06f2f7c4","user":{"login":"godwinamila","id":8766778,"avatar_url":"https://avatars.githubusercontent.com/u/8766778?v=3","gravatar_id":"","url":"https://api.github.com/users/godwinamila","html_url":"https://github.com/godwinamila","followers_url":"https://api.github.com/users/godwinamila/followers","following_url":"https://api.github.com/users/godwinamila/following{/other_user}","gists_url":"https://api.github.com/users/godwinamila/gists{/gist_id}","starred_url":"https://api.github.com/users/godwinamila/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/godwinamila/subscriptions","organizations_url":"https://api.github.com/users/godwinamila/orgs","repos_url":"https://api.github.com/users/godwinamila/repos","events_url":"https://api.github.com/users/godwinamila/events{/privacy}","received_events_url":"https://api.github.com/users/godwinamila/received_events","type":"User","site_admin":false},"body":"Use constant for token type ","created_at":"2015-11-27T04:56:41Z","updated_at":"2015-11-27T04:56:41Z","html_url":"https://github.com/wso2/carbon-identity/pull/1485#discussion_r46017402","pull_request_url":"https://api.github.com/repos/wso2/carbon-identity/pulls/1485","_links":{"self":{"href":"https://api.github.com/repos/wso2/carbon-identity/pulls/comments/46017402"},"html":{"href":"https://github.com/wso2/carbon-identity/pull/1485#discussion_r46017402"},"pull_request":{"href":"https://api.github.com/repos/wso2/carbon-identity/pulls/1485"}}},"pull_request":{"url":"https://api.github.com/repos/wso2/carbon-identity/pulls/1485","id":51882858,"html_url":"https://github.com/wso2/carbon-identity/pull/1485","diff_url":"https://github.com/wso2/carbon-identity/pull/1485.diff","patch_url":"https://github.com/wso2/carbon-identity/pull/1485.patch","issue_url":"https://api.github.com/repos/wso2/carbon-identity/issues/1485","number":1485,"state":"open","locked":false,"title":"Adding new grant handler to support NTLM handshake","user":{"login":"malinthaprasan","id":1591316,"avatar_url":"https://avatars.githubusercontent.com/u/1591316?v=3","gravatar_id":"","url":"https://api.github.com/users/malinthaprasan","html_url":"https://github.com/malinthaprasan","followers_url":"https://api.github.com/users/malinthaprasan/followers","following_url":"https://api.github.com/users/malinthaprasan/following{/other_user}","gists_url":"https://api.github.com/users/malinthaprasan/gists{/gist_id}","starred_url":"https://api.github.com/users/malinthaprasan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/malinthaprasan/subscriptions","organizations_url":"https://api.github.com/users/malinthaprasan/orgs","repos_url":"https://api.github.com/users/malinthaprasan/repos","events_url":"https://api.github.com/users/malinthaprasan/events{/privacy}","received_events_url":"https://api.github.com/users/malinthaprasan/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-26T08:14:46Z","updated_at":"2015-11-27T04:56:41Z","closed_at":null,"merged_at":null,"merge_commit_sha":"cfd9e0c04ca7ef9d60b5faf474084885f4d721ff","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/wso2/carbon-identity/pulls/1485/commits","review_comments_url":"https://api.github.com/repos/wso2/carbon-identity/pulls/1485/comments","review_comment_url":"https://api.github.com/repos/wso2/carbon-identity/pulls/comments{/number}","comments_url":"https://api.github.com/repos/wso2/carbon-identity/issues/1485/comments","statuses_url":"https://api.github.com/repos/wso2/carbon-identity/statuses/262d216ebc85650ecb69222cc8964a7d06f2f7c4","head":{"label":"malinthaprasan:master","ref":"master","sha":"262d216ebc85650ecb69222cc8964a7d06f2f7c4","user":{"login":"malinthaprasan","id":1591316,"avatar_url":"https://avatars.githubusercontent.com/u/1591316?v=3","gravatar_id":"","url":"https://api.github.com/users/malinthaprasan","html_url":"https://github.com/malinthaprasan","followers_url":"https://api.github.com/users/malinthaprasan/followers","following_url":"https://api.github.com/users/malinthaprasan/following{/other_user}","gists_url":"https://api.github.com/users/malinthaprasan/gists{/gist_id}","starred_url":"https://api.github.com/users/malinthaprasan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/malinthaprasan/subscriptions","organizations_url":"https://api.github.com/users/malinthaprasan/orgs","repos_url":"https://api.github.com/users/malinthaprasan/repos","events_url":"https://api.github.com/users/malinthaprasan/events{/privacy}","received_events_url":"https://api.github.com/users/malinthaprasan/received_events","type":"User","site_admin":false},"repo":{"id":46907263,"name":"carbon-identity","full_name":"malinthaprasan/carbon-identity","owner":{"login":"malinthaprasan","id":1591316,"avatar_url":"https://avatars.githubusercontent.com/u/1591316?v=3","gravatar_id":"","url":"https://api.github.com/users/malinthaprasan","html_url":"https://github.com/malinthaprasan","followers_url":"https://api.github.com/users/malinthaprasan/followers","following_url":"https://api.github.com/users/malinthaprasan/following{/other_user}","gists_url":"https://api.github.com/users/malinthaprasan/gists{/gist_id}","starred_url":"https://api.github.com/users/malinthaprasan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/malinthaprasan/subscriptions","organizations_url":"https://api.github.com/users/malinthaprasan/orgs","repos_url":"https://api.github.com/users/malinthaprasan/repos","events_url":"https://api.github.com/users/malinthaprasan/events{/privacy}","received_events_url":"https://api.github.com/users/malinthaprasan/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/malinthaprasan/carbon-identity","description":"","fork":true,"url":"https://api.github.com/repos/malinthaprasan/carbon-identity","forks_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/forks","keys_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/keys{/key_id}","collaborators_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/teams","hooks_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/hooks","issue_events_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/issues/events{/number}","events_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/events","assignees_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/assignees{/user}","branches_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/branches{/branch}","tags_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/tags","blobs_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/git/refs{/sha}","trees_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/git/trees{/sha}","statuses_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/statuses/{sha}","languages_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/languages","stargazers_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/stargazers","contributors_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/contributors","subscribers_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/subscribers","subscription_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/subscription","commits_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/commits{/sha}","git_commits_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/git/commits{/sha}","comments_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/comments{/number}","issue_comment_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/issues/comments{/number}","contents_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/contents/{+path}","compare_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/compare/{base}...{head}","merges_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/merges","archive_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/downloads","issues_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/issues{/number}","pulls_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/pulls{/number}","milestones_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/milestones{/number}","notifications_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/labels{/name}","releases_url":"https://api.github.com/repos/malinthaprasan/carbon-identity/releases{/id}","created_at":"2015-11-26T06:03:39Z","updated_at":"2015-11-26T06:03:52Z","pushed_at":"2015-11-26T08:13:45Z","git_url":"git://github.com/malinthaprasan/carbon-identity.git","ssh_url":"git@github.com:malinthaprasan/carbon-identity.git","clone_url":"https://github.com/malinthaprasan/carbon-identity.git","svn_url":"https://github.com/malinthaprasan/carbon-identity","homepage":null,"size":53984,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"wso2:master","ref":"master","sha":"6d74a98ff00560a01d9c602dc370a6258ef4e1ac","user":{"login":"wso2","id":533043,"avatar_url":"https://avatars.githubusercontent.com/u/533043?v=3","gravatar_id":"","url":"https://api.github.com/users/wso2","html_url":"https://github.com/wso2","followers_url":"https://api.github.com/users/wso2/followers","following_url":"https://api.github.com/users/wso2/following{/other_user}","gists_url":"https://api.github.com/users/wso2/gists{/gist_id}","starred_url":"https://api.github.com/users/wso2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wso2/subscriptions","organizations_url":"https://api.github.com/users/wso2/orgs","repos_url":"https://api.github.com/users/wso2/repos","events_url":"https://api.github.com/users/wso2/events{/privacy}","received_events_url":"https://api.github.com/users/wso2/received_events","type":"Organization","site_admin":false},"repo":{"id":16588204,"name":"carbon-identity","full_name":"wso2/carbon-identity","owner":{"login":"wso2","id":533043,"avatar_url":"https://avatars.githubusercontent.com/u/533043?v=3","gravatar_id":"","url":"https://api.github.com/users/wso2","html_url":"https://github.com/wso2","followers_url":"https://api.github.com/users/wso2/followers","following_url":"https://api.github.com/users/wso2/following{/other_user}","gists_url":"https://api.github.com/users/wso2/gists{/gist_id}","starred_url":"https://api.github.com/users/wso2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wso2/subscriptions","organizations_url":"https://api.github.com/users/wso2/orgs","repos_url":"https://api.github.com/users/wso2/repos","events_url":"https://api.github.com/users/wso2/events{/privacy}","received_events_url":"https://api.github.com/users/wso2/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/wso2/carbon-identity","description":"","fork":false,"url":"https://api.github.com/repos/wso2/carbon-identity","forks_url":"https://api.github.com/repos/wso2/carbon-identity/forks","keys_url":"https://api.github.com/repos/wso2/carbon-identity/keys{/key_id}","collaborators_url":"https://api.github.com/repos/wso2/carbon-identity/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/wso2/carbon-identity/teams","hooks_url":"https://api.github.com/repos/wso2/carbon-identity/hooks","issue_events_url":"https://api.github.com/repos/wso2/carbon-identity/issues/events{/number}","events_url":"https://api.github.com/repos/wso2/carbon-identity/events","assignees_url":"https://api.github.com/repos/wso2/carbon-identity/assignees{/user}","branches_url":"https://api.github.com/repos/wso2/carbon-identity/branches{/branch}","tags_url":"https://api.github.com/repos/wso2/carbon-identity/tags","blobs_url":"https://api.github.com/repos/wso2/carbon-identity/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/wso2/carbon-identity/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/wso2/carbon-identity/git/refs{/sha}","trees_url":"https://api.github.com/repos/wso2/carbon-identity/git/trees{/sha}","statuses_url":"https://api.github.com/repos/wso2/carbon-identity/statuses/{sha}","languages_url":"https://api.github.com/repos/wso2/carbon-identity/languages","stargazers_url":"https://api.github.com/repos/wso2/carbon-identity/stargazers","contributors_url":"https://api.github.com/repos/wso2/carbon-identity/contributors","subscribers_url":"https://api.github.com/repos/wso2/carbon-identity/subscribers","subscription_url":"https://api.github.com/repos/wso2/carbon-identity/subscription","commits_url":"https://api.github.com/repos/wso2/carbon-identity/commits{/sha}","git_commits_url":"https://api.github.com/repos/wso2/carbon-identity/git/commits{/sha}","comments_url":"https://api.github.com/repos/wso2/carbon-identity/comments{/number}","issue_comment_url":"https://api.github.com/repos/wso2/carbon-identity/issues/comments{/number}","contents_url":"https://api.github.com/repos/wso2/carbon-identity/contents/{+path}","compare_url":"https://api.github.com/repos/wso2/carbon-identity/compare/{base}...{head}","merges_url":"https://api.github.com/repos/wso2/carbon-identity/merges","archive_url":"https://api.github.com/repos/wso2/carbon-identity/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/wso2/carbon-identity/downloads","issues_url":"https://api.github.com/repos/wso2/carbon-identity/issues{/number}","pulls_url":"https://api.github.com/repos/wso2/carbon-identity/pulls{/number}","milestones_url":"https://api.github.com/repos/wso2/carbon-identity/milestones{/number}","notifications_url":"https://api.github.com/repos/wso2/carbon-identity/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/wso2/carbon-identity/labels{/name}","releases_url":"https://api.github.com/repos/wso2/carbon-identity/releases{/id}","created_at":"2014-02-06T17:58:23Z","updated_at":"2015-11-04T13:30:12Z","pushed_at":"2015-11-27T04:29:29Z","git_url":"git://github.com/wso2/carbon-identity.git","ssh_url":"git@github.com:wso2/carbon-identity.git","clone_url":"https://github.com/wso2/carbon-identity.git","svn_url":"https://github.com/wso2/carbon-identity","homepage":null,"size":75327,"stargazers_count":12,"watchers_count":12,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":152,"mirror_url":null,"open_issues_count":58,"forks":152,"open_issues":58,"watchers":12,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/wso2/carbon-identity/pulls/1485"},"html":{"href":"https://github.com/wso2/carbon-identity/pull/1485"},"issue":{"href":"https://api.github.com/repos/wso2/carbon-identity/issues/1485"},"comments":{"href":"https://api.github.com/repos/wso2/carbon-identity/issues/1485/comments"},"review_comments":{"href":"https://api.github.com/repos/wso2/carbon-identity/pulls/1485/comments"},"review_comment":{"href":"https://api.github.com/repos/wso2/carbon-identity/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/wso2/carbon-identity/pulls/1485/commits"},"statuses":{"href":"https://api.github.com/repos/wso2/carbon-identity/statuses/262d216ebc85650ecb69222cc8964a7d06f2f7c4"}}}}