{"action":"created","comment":{"url":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/comments/45450186","id":45450186,"diff_hunk":"@@ -13,118 +14,177 @@ import {AuthException} from './Exception/AuthException';\n export class Token {\n   /**\n    * @param {String} identityPoolId\n-   * @param {String} providerName\n-   * @param {String} providerUserToken\n-   * @param {String} providerUserId\n    */\n-  constructor(identityPoolId, providerName = null, providerUserToken = null, providerUserId = null) {\n+  constructor(identityPoolId) {\n     this._identityPoolId = identityPoolId;\n-    this._providerName = providerName;\n-    this._providerUserToken = providerUserToken;\n-    this._providerUserId = providerUserId;\n \n+    this._identityProvider = null;\n+    this._lambdaContext = null;\n     this._user = null;\n     this._userProvider = null;\n-    this._identityId = null;\n     this._credentials = null;\n \n-    this._isAnonymous = true;\n+    this._credsManager = new CredentialsManager(identityPoolId);\n+\n+    this._setupAwsCognitoConfig();\n   }\n \n   /**\n-   * @param {Function} callback\n+   * Setup region for CognitoIdentity and CognitoSync services\n+   *\n+   * @private\n    */\n-  getCredentials(callback = () => null) {\n+  _setupAwsCognitoConfig() {\n     // @todo: set retries in a smarter way...\n     AWS.config.maxRetries = 3;\n \n-    let defaultRegion = AWS.config.region;\n+    let cognitoRegion = Token.getRegionFromIdentityPoolId(this._identityPoolId);\n \n     AWS.config.update({\n-      region: Token._getRegionFromIdentityPoolId(this._identityPoolId),\n+      cognitoidentity: {region: cognitoRegion},\n+      cognitosync: {region: cognitoRegion},\n     });\n+  }\n+\n+  /**\n+   * @returns {IdentityProvider}\n+   */\n+  get identityProvider() {\n+    return this._identityProvider;\n+  }\n+\n+  /**\n+   * @param {IdentityProvider} provider\n+   */\n+  set identityProvider(provider) {\n+    this._identityProvider = provider;\n+  }\n \n-    let cognitoParams = {\n-      IdentityPoolId: this._identityPoolId,\n-    };\n+  /**\n+   * @returns {Object}\n+   */\n+  get lambdaContext() {\n+    return this._lambdaContext;\n+  }\n+\n+  /**\n+   * @param {Object} lambdaContext\n+   */\n+  set lambdaContext(lambdaContext) {\n+    this._lambdaContext = lambdaContext;\n+  }\n \n-    if (this._providerName && this._providerUserToken) {\n-      this._isAnonymous = false;\n-      cognitoParams.Logins = {};\n-      cognitoParams.Logins[this._providerName] = this._providerUserToken;\n+  /**\n+   * @param {Function} callback\n+   */\n+  loadCredentials(callback = () => {}) {\n+    // avoid refreshing or loading credentials for each request\n+    if (this._validCredentials()) {\n+      callback(null, this.credentials);\n+      return;\n     }\n \n-    this._credentials = new AWS.CognitoIdentityCredentials(cognitoParams);\n+    if (this.lambdaContext) {\n+      this._credsManager.loadCredentials(this.identityId, (error, credentials) => {\n+        if (error) {\n+          callback(error, null);\n+          return;\n+        }\n+\n+        this._credentials = credentials;\n \n-    // update AWS credentials\n-    AWS.config.credentials = this._credentials.refresh(function(error) {\n-      if (error) {\n-        callback(new AuthException(error));\n-        return;\n+        callback(null, this._credentials);\n+      });\n+    } else {\n+      let cognitoParams = {\n+        IdentityPoolId: this._identityPoolId,\n+      };\n+\n+      if (this.identityProvider) {\n+        cognitoParams.Logins = {};\n+        cognitoParams.Logins[this.identityProvider.name] = this.identityProvider.userToken;\n       }\n \n-      this._identityId = this._credentials.identityId;\n+      this._credentials = new AWS.CognitoIdentityCredentials(cognitoParams);\n \n-      AWS.config.update({\n-        accessKeyId: this._credentials.accessKeyId,\n-        secretAccessKey: this._credentials.secretAccessKey,\n-        sessionToken: this._credentials.sessionToken,\n-        region: defaultRegion, // restore to default region\n-      });\n+      this._credentials.refresh((error) => {\n+        if (error) {\n+          callback(new AuthException(error), null);\n+          return;\n+        }\n+\n+        AWS.config.credentials = this._credentials;\n \n-      callback(null, this);\n-    }.bind(this));\n+        this._credsManager.saveCredentials(this._credentials, (error, record) => {","path":"src/deep-security/lib/Token.js","position":148,"original_position":148,"commit_id":"789a090c910d29e4530f8aeb572f43130146efdf","original_commit_id":"789a090c910d29e4530f8aeb572f43130146efdf","user":{"login":"devs-deep","id":14040097,"avatar_url":"https://avatars.githubusercontent.com/u/14040097?v=3","gravatar_id":"","url":"https://api.github.com/users/devs-deep","html_url":"https://github.com/devs-deep","followers_url":"https://api.github.com/users/devs-deep/followers","following_url":"https://api.github.com/users/devs-deep/following{/other_user}","gists_url":"https://api.github.com/users/devs-deep/gists{/gist_id}","starred_url":"https://api.github.com/users/devs-deep/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/devs-deep/subscriptions","organizations_url":"https://api.github.com/users/devs-deep/orgs","repos_url":"https://api.github.com/users/devs-deep/repos","events_url":"https://api.github.com/users/devs-deep/events{/privacy}","received_events_url":"https://api.github.com/users/devs-deep/received_events","type":"User","site_admin":false},"body":"![Codacy](https://www.codacy.com/versioned/images/favicon.png) Issue found: ['record' is defined but never used.](https://www.codacy.com/app/MitocGroup/deep-framework/file/1371453482/issues/source?bid=2895475&fileBranchId=2973909#l118)","created_at":"2015-11-20T09:34:52Z","updated_at":"2015-11-20T09:34:52Z","html_url":"https://github.com/MitocGroup/deep-framework/pull/106#discussion_r45450186","pull_request_url":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/106","_links":{"self":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/comments/45450186"},"html":{"href":"https://github.com/MitocGroup/deep-framework/pull/106#discussion_r45450186"},"pull_request":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/106"}}},"pull_request":{"url":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/106","id":51219819,"html_url":"https://github.com/MitocGroup/deep-framework/pull/106","diff_url":"https://github.com/MitocGroup/deep-framework/pull/106.diff","patch_url":"https://github.com/MitocGroup/deep-framework/pull/106.patch","issue_url":"https://api.github.com/repos/MitocGroup/deep-framework/issues/106","number":106,"state":"open","locked":false,"title":"#63 - Implement backend to backend calls through API Gateway","user":{"login":"mgoria","id":2959952,"avatar_url":"https://avatars.githubusercontent.com/u/2959952?v=3","gravatar_id":"","url":"https://api.github.com/users/mgoria","html_url":"https://github.com/mgoria","followers_url":"https://api.github.com/users/mgoria/followers","following_url":"https://api.github.com/users/mgoria/following{/other_user}","gists_url":"https://api.github.com/users/mgoria/gists{/gist_id}","starred_url":"https://api.github.com/users/mgoria/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mgoria/subscriptions","organizations_url":"https://api.github.com/users/mgoria/orgs","repos_url":"https://api.github.com/users/mgoria/repos","events_url":"https://api.github.com/users/mgoria/events{/privacy}","received_events_url":"https://api.github.com/users/mgoria/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-11-19T13:08:39Z","updated_at":"2015-11-20T09:34:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"f5d20702aaed7f8ccd36132fa83790e804d1e900","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/106/commits","review_comments_url":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/106/comments","review_comment_url":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/comments{/number}","comments_url":"https://api.github.com/repos/MitocGroup/deep-framework/issues/106/comments","statuses_url":"https://api.github.com/repos/MitocGroup/deep-framework/statuses/789a090c910d29e4530f8aeb572f43130146efdf","head":{"label":"MitocGroup:add_cognito_sync","ref":"add_cognito_sync","sha":"789a090c910d29e4530f8aeb572f43130146efdf","user":{"login":"MitocGroup","id":8398780,"avatar_url":"https://avatars.githubusercontent.com/u/8398780?v=3","gravatar_id":"","url":"https://api.github.com/users/MitocGroup","html_url":"https://github.com/MitocGroup","followers_url":"https://api.github.com/users/MitocGroup/followers","following_url":"https://api.github.com/users/MitocGroup/following{/other_user}","gists_url":"https://api.github.com/users/MitocGroup/gists{/gist_id}","starred_url":"https://api.github.com/users/MitocGroup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MitocGroup/subscriptions","organizations_url":"https://api.github.com/users/MitocGroup/orgs","repos_url":"https://api.github.com/users/MitocGroup/repos","events_url":"https://api.github.com/users/MitocGroup/events{/privacy}","received_events_url":"https://api.github.com/users/MitocGroup/received_events","type":"Organization","site_admin":false},"repo":{"id":43718196,"name":"deep-framework","full_name":"MitocGroup/deep-framework","owner":{"login":"MitocGroup","id":8398780,"avatar_url":"https://avatars.githubusercontent.com/u/8398780?v=3","gravatar_id":"","url":"https://api.github.com/users/MitocGroup","html_url":"https://github.com/MitocGroup","followers_url":"https://api.github.com/users/MitocGroup/followers","following_url":"https://api.github.com/users/MitocGroup/following{/other_user}","gists_url":"https://api.github.com/users/MitocGroup/gists{/gist_id}","starred_url":"https://api.github.com/users/MitocGroup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MitocGroup/subscriptions","organizations_url":"https://api.github.com/users/MitocGroup/orgs","repos_url":"https://api.github.com/users/MitocGroup/repos","events_url":"https://api.github.com/users/MitocGroup/events{/privacy}","received_events_url":"https://api.github.com/users/MitocGroup/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/MitocGroup/deep-framework","description":"Serverless Web Framework for Cloud-Native Applications and Platforms using Microservices Architecture","fork":false,"url":"https://api.github.com/repos/MitocGroup/deep-framework","forks_url":"https://api.github.com/repos/MitocGroup/deep-framework/forks","keys_url":"https://api.github.com/repos/MitocGroup/deep-framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/MitocGroup/deep-framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/MitocGroup/deep-framework/teams","hooks_url":"https://api.github.com/repos/MitocGroup/deep-framework/hooks","issue_events_url":"https://api.github.com/repos/MitocGroup/deep-framework/issues/events{/number}","events_url":"https://api.github.com/repos/MitocGroup/deep-framework/events","assignees_url":"https://api.github.com/repos/MitocGroup/deep-framework/assignees{/user}","branches_url":"https://api.github.com/repos/MitocGroup/deep-framework/branches{/branch}","tags_url":"https://api.github.com/repos/MitocGroup/deep-framework/tags","blobs_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/MitocGroup/deep-framework/statuses/{sha}","languages_url":"https://api.github.com/repos/MitocGroup/deep-framework/languages","stargazers_url":"https://api.github.com/repos/MitocGroup/deep-framework/stargazers","contributors_url":"https://api.github.com/repos/MitocGroup/deep-framework/contributors","subscribers_url":"https://api.github.com/repos/MitocGroup/deep-framework/subscribers","subscription_url":"https://api.github.com/repos/MitocGroup/deep-framework/subscription","commits_url":"https://api.github.com/repos/MitocGroup/deep-framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/MitocGroup/deep-framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/MitocGroup/deep-framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/MitocGroup/deep-framework/contents/{+path}","compare_url":"https://api.github.com/repos/MitocGroup/deep-framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/MitocGroup/deep-framework/merges","archive_url":"https://api.github.com/repos/MitocGroup/deep-framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/MitocGroup/deep-framework/downloads","issues_url":"https://api.github.com/repos/MitocGroup/deep-framework/issues{/number}","pulls_url":"https://api.github.com/repos/MitocGroup/deep-framework/pulls{/number}","milestones_url":"https://api.github.com/repos/MitocGroup/deep-framework/milestones{/number}","notifications_url":"https://api.github.com/repos/MitocGroup/deep-framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/MitocGroup/deep-framework/labels{/name}","releases_url":"https://api.github.com/repos/MitocGroup/deep-framework/releases{/id}","created_at":"2015-10-05T22:48:47Z","updated_at":"2015-11-19T14:53:41Z","pushed_at":"2015-11-20T09:30:33Z","git_url":"git://github.com/MitocGroup/deep-framework.git","ssh_url":"git@github.com:MitocGroup/deep-framework.git","clone_url":"https://github.com/MitocGroup/deep-framework.git","svn_url":"https://github.com/MitocGroup/deep-framework","homepage":"http://docs.deep.mg","size":5104,"stargazers_count":86,"watchers_count":86,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":17,"mirror_url":null,"open_issues_count":15,"forks":17,"open_issues":15,"watchers":86,"default_branch":"master"}},"base":{"label":"MitocGroup:dev","ref":"dev","sha":"21b0d8d4fb873d25b12b66dcdc69eec16ed653d3","user":{"login":"MitocGroup","id":8398780,"avatar_url":"https://avatars.githubusercontent.com/u/8398780?v=3","gravatar_id":"","url":"https://api.github.com/users/MitocGroup","html_url":"https://github.com/MitocGroup","followers_url":"https://api.github.com/users/MitocGroup/followers","following_url":"https://api.github.com/users/MitocGroup/following{/other_user}","gists_url":"https://api.github.com/users/MitocGroup/gists{/gist_id}","starred_url":"https://api.github.com/users/MitocGroup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MitocGroup/subscriptions","organizations_url":"https://api.github.com/users/MitocGroup/orgs","repos_url":"https://api.github.com/users/MitocGroup/repos","events_url":"https://api.github.com/users/MitocGroup/events{/privacy}","received_events_url":"https://api.github.com/users/MitocGroup/received_events","type":"Organization","site_admin":false},"repo":{"id":43718196,"name":"deep-framework","full_name":"MitocGroup/deep-framework","owner":{"login":"MitocGroup","id":8398780,"avatar_url":"https://avatars.githubusercontent.com/u/8398780?v=3","gravatar_id":"","url":"https://api.github.com/users/MitocGroup","html_url":"https://github.com/MitocGroup","followers_url":"https://api.github.com/users/MitocGroup/followers","following_url":"https://api.github.com/users/MitocGroup/following{/other_user}","gists_url":"https://api.github.com/users/MitocGroup/gists{/gist_id}","starred_url":"https://api.github.com/users/MitocGroup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/MitocGroup/subscriptions","organizations_url":"https://api.github.com/users/MitocGroup/orgs","repos_url":"https://api.github.com/users/MitocGroup/repos","events_url":"https://api.github.com/users/MitocGroup/events{/privacy}","received_events_url":"https://api.github.com/users/MitocGroup/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/MitocGroup/deep-framework","description":"Serverless Web Framework for Cloud-Native Applications and Platforms using Microservices Architecture","fork":false,"url":"https://api.github.com/repos/MitocGroup/deep-framework","forks_url":"https://api.github.com/repos/MitocGroup/deep-framework/forks","keys_url":"https://api.github.com/repos/MitocGroup/deep-framework/keys{/key_id}","collaborators_url":"https://api.github.com/repos/MitocGroup/deep-framework/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/MitocGroup/deep-framework/teams","hooks_url":"https://api.github.com/repos/MitocGroup/deep-framework/hooks","issue_events_url":"https://api.github.com/repos/MitocGroup/deep-framework/issues/events{/number}","events_url":"https://api.github.com/repos/MitocGroup/deep-framework/events","assignees_url":"https://api.github.com/repos/MitocGroup/deep-framework/assignees{/user}","branches_url":"https://api.github.com/repos/MitocGroup/deep-framework/branches{/branch}","tags_url":"https://api.github.com/repos/MitocGroup/deep-framework/tags","blobs_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/refs{/sha}","trees_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/trees{/sha}","statuses_url":"https://api.github.com/repos/MitocGroup/deep-framework/statuses/{sha}","languages_url":"https://api.github.com/repos/MitocGroup/deep-framework/languages","stargazers_url":"https://api.github.com/repos/MitocGroup/deep-framework/stargazers","contributors_url":"https://api.github.com/repos/MitocGroup/deep-framework/contributors","subscribers_url":"https://api.github.com/repos/MitocGroup/deep-framework/subscribers","subscription_url":"https://api.github.com/repos/MitocGroup/deep-framework/subscription","commits_url":"https://api.github.com/repos/MitocGroup/deep-framework/commits{/sha}","git_commits_url":"https://api.github.com/repos/MitocGroup/deep-framework/git/commits{/sha}","comments_url":"https://api.github.com/repos/MitocGroup/deep-framework/comments{/number}","issue_comment_url":"https://api.github.com/repos/MitocGroup/deep-framework/issues/comments{/number}","contents_url":"https://api.github.com/repos/MitocGroup/deep-framework/contents/{+path}","compare_url":"https://api.github.com/repos/MitocGroup/deep-framework/compare/{base}...{head}","merges_url":"https://api.github.com/repos/MitocGroup/deep-framework/merges","archive_url":"https://api.github.com/repos/MitocGroup/deep-framework/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/MitocGroup/deep-framework/downloads","issues_url":"https://api.github.com/repos/MitocGroup/deep-framework/issues{/number}","pulls_url":"https://api.github.com/repos/MitocGroup/deep-framework/pulls{/number}","milestones_url":"https://api.github.com/repos/MitocGroup/deep-framework/milestones{/number}","notifications_url":"https://api.github.com/repos/MitocGroup/deep-framework/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/MitocGroup/deep-framework/labels{/name}","releases_url":"https://api.github.com/repos/MitocGroup/deep-framework/releases{/id}","created_at":"2015-10-05T22:48:47Z","updated_at":"2015-11-19T14:53:41Z","pushed_at":"2015-11-20T09:30:33Z","git_url":"git://github.com/MitocGroup/deep-framework.git","ssh_url":"git@github.com:MitocGroup/deep-framework.git","clone_url":"https://github.com/MitocGroup/deep-framework.git","svn_url":"https://github.com/MitocGroup/deep-framework","homepage":"http://docs.deep.mg","size":5104,"stargazers_count":86,"watchers_count":86,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":17,"mirror_url":null,"open_issues_count":15,"forks":17,"open_issues":15,"watchers":86,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/106"},"html":{"href":"https://github.com/MitocGroup/deep-framework/pull/106"},"issue":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/issues/106"},"comments":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/issues/106/comments"},"review_comments":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/106/comments"},"review_comment":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/pulls/106/commits"},"statuses":{"href":"https://api.github.com/repos/MitocGroup/deep-framework/statuses/789a090c910d29e4530f8aeb572f43130146efdf"}}}}