{"action":"created","comment":{"url":"https://api.github.com/repos/hmrc/api-gateway/pulls/comments/94119421","pull_request_review_id":14649743,"id":94119421,"diff_hunk":"@@ -0,0 +1,59 @@\n+/*\n+ * Copyright 2016 HM Revenue & Customs\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except 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, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package uk.gov.hmrc.apigateway.play.filter\n+\n+import javax.inject.{Inject, Singleton}\n+\n+import akka.stream.Materializer\n+import play.api.mvc._\n+import uk.gov.hmrc.apigateway.exception.GatewayError.{MissingCredentials, NotFound => _}\n+import uk.gov.hmrc.apigateway.model.AuthType.APPLICATION\n+import uk.gov.hmrc.apigateway.model.ProxyRequest\n+import uk.gov.hmrc.apigateway.util.HttpHeaders._\n+\n+import scala.concurrent.ExecutionContext.Implicits.global\n+import scala.concurrent.Future.{failed, successful}\n+import scala.concurrent.{ExecutionContext, Future}\n+import scala.util.{Success, Try}\n+\n+/**\n+  * Filter for inspecting requests for application restricted endpoints and\n+  * evaluating their eligibility to be proxied to a downstream services.\n+  */\n+@Singleton\n+class ApplicationRestrictedEndpointFilter @Inject()\n+(delegatedAuthorityFilter: DelegatedAuthorityFilter)\n+(implicit override val mat: Materializer, executionContext: ExecutionContext) extends ApiGatewayFilter {\n+\n+  override def filter(requestHeader: RequestHeader, proxyRequest: ProxyRequest): Future[RequestHeader] =\n+    requestHeader.tags.get(X_API_GATEWAY_AUTH_TYPE) match {\n+      case Some(APPLICATION.toString) =>\n+        Try(delegatedAuthorityFilter.filter(proxyRequest)) match {\n+          case Success(eventualAuthority) => eventualAuthority map { authority =>\n+            requestHeader.withTag(X_APPLICATION_CLIENT_ID, authority.delegatedAuthority.token.accessToken)\n+          }\n+          case _ => requestHeader.tags.get(AUTHORIZATION) match {\n+            case Some(bearerToken) =>\n+              val serverToken = bearerToken.stripPrefix(\"Bearer \")\n+              successful(requestHeader.withTag(X_APPLICATION_CLIENT_ID, serverToken))","path":"app/uk/gov/hmrc/apigateway/play/filter/ApplicationRestrictedEndpointFilter.scala","position":52,"original_position":52,"commit_id":"f51d94a323e80a91efe51448cd656ad6fdfbe3c7","original_commit_id":"f51d94a323e80a91efe51448cd656ad6fdfbe3c7","user":{"login":"edinhodzic","id":4981314,"avatar_url":"https://avatars.githubusercontent.com/u/4981314?v=3","gravatar_id":"","url":"https://api.github.com/users/edinhodzic","html_url":"https://github.com/edinhodzic","followers_url":"https://api.github.com/users/edinhodzic/followers","following_url":"https://api.github.com/users/edinhodzic/following{/other_user}","gists_url":"https://api.github.com/users/edinhodzic/gists{/gist_id}","starred_url":"https://api.github.com/users/edinhodzic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edinhodzic/subscriptions","organizations_url":"https://api.github.com/users/edinhodzic/orgs","repos_url":"https://api.github.com/users/edinhodzic/repos","events_url":"https://api.github.com/users/edinhodzic/events{/privacy}","received_events_url":"https://api.github.com/users/edinhodzic/received_events","type":"User","site_admin":false},"body":"So application restricted endpoints can be invoked using an access/user token or a server token. Hence at this point `X_APPLICATION_CLIENT_ID` may contain an access or a server token. What am I missing?","created_at":"2016-12-29T10:00:03Z","updated_at":"2016-12-29T10:00:03Z","html_url":"https://github.com/hmrc/api-gateway/pull/11#discussion_r94119421","pull_request_url":"https://api.github.com/repos/hmrc/api-gateway/pulls/11","_links":{"self":{"href":"https://api.github.com/repos/hmrc/api-gateway/pulls/comments/94119421"},"html":{"href":"https://github.com/hmrc/api-gateway/pull/11#discussion_r94119421"},"pull_request":{"href":"https://api.github.com/repos/hmrc/api-gateway/pulls/11"}}},"pull_request":{"url":"https://api.github.com/repos/hmrc/api-gateway/pulls/11","id":99544621,"html_url":"https://github.com/hmrc/api-gateway/pull/11","diff_url":"https://github.com/hmrc/api-gateway/pull/11.diff","patch_url":"https://github.com/hmrc/api-gateway/pull/11.patch","issue_url":"https://api.github.com/repos/hmrc/api-gateway/issues/11","number":11,"state":"open","locked":false,"title":"API 2216 separating open and restricted endpoint filtering","user":{"login":"edinhodzic","id":4981314,"avatar_url":"https://avatars.githubusercontent.com/u/4981314?v=3","gravatar_id":"","url":"https://api.github.com/users/edinhodzic","html_url":"https://github.com/edinhodzic","followers_url":"https://api.github.com/users/edinhodzic/followers","following_url":"https://api.github.com/users/edinhodzic/following{/other_user}","gists_url":"https://api.github.com/users/edinhodzic/gists{/gist_id}","starred_url":"https://api.github.com/users/edinhodzic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edinhodzic/subscriptions","organizations_url":"https://api.github.com/users/edinhodzic/orgs","repos_url":"https://api.github.com/users/edinhodzic/repos","events_url":"https://api.github.com/users/edinhodzic/events{/privacy}","received_events_url":"https://api.github.com/users/edinhodzic/received_events","type":"User","site_admin":false},"body":"Here the `ProxyFilter` has been split into three filters; `GenericEndpointFilter`, `UserRestrictedEndpointFilter` and `ApplicationRestrictedEndpointFilter`. This made the implementation to distinguish between open and restricted endpoints easier and better.\r\n\r\nA picture is worth a thousand words so here's the before:\r\n\r\n![before](https://cloud.githubusercontent.com/assets/4981314/21522097/a683ca24-ccfa-11e6-8b8a-b615b696a774.png)\r\n\r\nAnd this is the after:\r\n\r\n![after](https://cloud.githubusercontent.com/assets/4981314/21523794/1139db84-cd09-11e6-8aaf-ce56fa252f29.png)\r\n","created_at":"2016-12-28T12:39:40Z","updated_at":"2016-12-29T10:00:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9cf41b9e0e706c018574d3a308e6f9f7eac2b892","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/hmrc/api-gateway/pulls/11/commits","review_comments_url":"https://api.github.com/repos/hmrc/api-gateway/pulls/11/comments","review_comment_url":"https://api.github.com/repos/hmrc/api-gateway/pulls/comments{/number}","comments_url":"https://api.github.com/repos/hmrc/api-gateway/issues/11/comments","statuses_url":"https://api.github.com/repos/hmrc/api-gateway/statuses/f51d94a323e80a91efe51448cd656ad6fdfbe3c7","head":{"label":"hmrc:API-2216-separating-open-and-restricted-endpoint-filtering","ref":"API-2216-separating-open-and-restricted-endpoint-filtering","sha":"f51d94a323e80a91efe51448cd656ad6fdfbe3c7","user":{"login":"hmrc","id":5755339,"avatar_url":"https://avatars.githubusercontent.com/u/5755339?v=3","gravatar_id":"","url":"https://api.github.com/users/hmrc","html_url":"https://github.com/hmrc","followers_url":"https://api.github.com/users/hmrc/followers","following_url":"https://api.github.com/users/hmrc/following{/other_user}","gists_url":"https://api.github.com/users/hmrc/gists{/gist_id}","starred_url":"https://api.github.com/users/hmrc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmrc/subscriptions","organizations_url":"https://api.github.com/users/hmrc/orgs","repos_url":"https://api.github.com/users/hmrc/repos","events_url":"https://api.github.com/users/hmrc/events{/privacy}","received_events_url":"https://api.github.com/users/hmrc/received_events","type":"Organization","site_admin":false},"repo":{"id":76022347,"name":"api-gateway","full_name":"hmrc/api-gateway","owner":{"login":"hmrc","id":5755339,"avatar_url":"https://avatars.githubusercontent.com/u/5755339?v=3","gravatar_id":"","url":"https://api.github.com/users/hmrc","html_url":"https://github.com/hmrc","followers_url":"https://api.github.com/users/hmrc/followers","following_url":"https://api.github.com/users/hmrc/following{/other_user}","gists_url":"https://api.github.com/users/hmrc/gists{/gist_id}","starred_url":"https://api.github.com/users/hmrc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmrc/subscriptions","organizations_url":"https://api.github.com/users/hmrc/orgs","repos_url":"https://api.github.com/users/hmrc/repos","events_url":"https://api.github.com/users/hmrc/events{/privacy}","received_events_url":"https://api.github.com/users/hmrc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/hmrc/api-gateway","description":null,"fork":false,"url":"https://api.github.com/repos/hmrc/api-gateway","forks_url":"https://api.github.com/repos/hmrc/api-gateway/forks","keys_url":"https://api.github.com/repos/hmrc/api-gateway/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hmrc/api-gateway/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hmrc/api-gateway/teams","hooks_url":"https://api.github.com/repos/hmrc/api-gateway/hooks","issue_events_url":"https://api.github.com/repos/hmrc/api-gateway/issues/events{/number}","events_url":"https://api.github.com/repos/hmrc/api-gateway/events","assignees_url":"https://api.github.com/repos/hmrc/api-gateway/assignees{/user}","branches_url":"https://api.github.com/repos/hmrc/api-gateway/branches{/branch}","tags_url":"https://api.github.com/repos/hmrc/api-gateway/tags","blobs_url":"https://api.github.com/repos/hmrc/api-gateway/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hmrc/api-gateway/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hmrc/api-gateway/git/refs{/sha}","trees_url":"https://api.github.com/repos/hmrc/api-gateway/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hmrc/api-gateway/statuses/{sha}","languages_url":"https://api.github.com/repos/hmrc/api-gateway/languages","stargazers_url":"https://api.github.com/repos/hmrc/api-gateway/stargazers","contributors_url":"https://api.github.com/repos/hmrc/api-gateway/contributors","subscribers_url":"https://api.github.com/repos/hmrc/api-gateway/subscribers","subscription_url":"https://api.github.com/repos/hmrc/api-gateway/subscription","commits_url":"https://api.github.com/repos/hmrc/api-gateway/commits{/sha}","git_commits_url":"https://api.github.com/repos/hmrc/api-gateway/git/commits{/sha}","comments_url":"https://api.github.com/repos/hmrc/api-gateway/comments{/number}","issue_comment_url":"https://api.github.com/repos/hmrc/api-gateway/issues/comments{/number}","contents_url":"https://api.github.com/repos/hmrc/api-gateway/contents/{+path}","compare_url":"https://api.github.com/repos/hmrc/api-gateway/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hmrc/api-gateway/merges","archive_url":"https://api.github.com/repos/hmrc/api-gateway/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hmrc/api-gateway/downloads","issues_url":"https://api.github.com/repos/hmrc/api-gateway/issues{/number}","pulls_url":"https://api.github.com/repos/hmrc/api-gateway/pulls{/number}","milestones_url":"https://api.github.com/repos/hmrc/api-gateway/milestones{/number}","notifications_url":"https://api.github.com/repos/hmrc/api-gateway/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hmrc/api-gateway/labels{/name}","releases_url":"https://api.github.com/repos/hmrc/api-gateway/releases{/id}","deployments_url":"https://api.github.com/repos/hmrc/api-gateway/deployments","created_at":"2016-12-09T09:42:04Z","updated_at":"2016-12-12T16:10:12Z","pushed_at":"2016-12-28T17:19:11Z","git_url":"git://github.com/hmrc/api-gateway.git","ssh_url":"git@github.com:hmrc/api-gateway.git","clone_url":"https://github.com/hmrc/api-gateway.git","svn_url":"https://github.com/hmrc/api-gateway","homepage":"","size":138,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"hmrc:master","ref":"master","sha":"b57cc265855734df02db13331696bcbf68da3868","user":{"login":"hmrc","id":5755339,"avatar_url":"https://avatars.githubusercontent.com/u/5755339?v=3","gravatar_id":"","url":"https://api.github.com/users/hmrc","html_url":"https://github.com/hmrc","followers_url":"https://api.github.com/users/hmrc/followers","following_url":"https://api.github.com/users/hmrc/following{/other_user}","gists_url":"https://api.github.com/users/hmrc/gists{/gist_id}","starred_url":"https://api.github.com/users/hmrc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmrc/subscriptions","organizations_url":"https://api.github.com/users/hmrc/orgs","repos_url":"https://api.github.com/users/hmrc/repos","events_url":"https://api.github.com/users/hmrc/events{/privacy}","received_events_url":"https://api.github.com/users/hmrc/received_events","type":"Organization","site_admin":false},"repo":{"id":76022347,"name":"api-gateway","full_name":"hmrc/api-gateway","owner":{"login":"hmrc","id":5755339,"avatar_url":"https://avatars.githubusercontent.com/u/5755339?v=3","gravatar_id":"","url":"https://api.github.com/users/hmrc","html_url":"https://github.com/hmrc","followers_url":"https://api.github.com/users/hmrc/followers","following_url":"https://api.github.com/users/hmrc/following{/other_user}","gists_url":"https://api.github.com/users/hmrc/gists{/gist_id}","starred_url":"https://api.github.com/users/hmrc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hmrc/subscriptions","organizations_url":"https://api.github.com/users/hmrc/orgs","repos_url":"https://api.github.com/users/hmrc/repos","events_url":"https://api.github.com/users/hmrc/events{/privacy}","received_events_url":"https://api.github.com/users/hmrc/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/hmrc/api-gateway","description":null,"fork":false,"url":"https://api.github.com/repos/hmrc/api-gateway","forks_url":"https://api.github.com/repos/hmrc/api-gateway/forks","keys_url":"https://api.github.com/repos/hmrc/api-gateway/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hmrc/api-gateway/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hmrc/api-gateway/teams","hooks_url":"https://api.github.com/repos/hmrc/api-gateway/hooks","issue_events_url":"https://api.github.com/repos/hmrc/api-gateway/issues/events{/number}","events_url":"https://api.github.com/repos/hmrc/api-gateway/events","assignees_url":"https://api.github.com/repos/hmrc/api-gateway/assignees{/user}","branches_url":"https://api.github.com/repos/hmrc/api-gateway/branches{/branch}","tags_url":"https://api.github.com/repos/hmrc/api-gateway/tags","blobs_url":"https://api.github.com/repos/hmrc/api-gateway/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hmrc/api-gateway/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hmrc/api-gateway/git/refs{/sha}","trees_url":"https://api.github.com/repos/hmrc/api-gateway/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hmrc/api-gateway/statuses/{sha}","languages_url":"https://api.github.com/repos/hmrc/api-gateway/languages","stargazers_url":"https://api.github.com/repos/hmrc/api-gateway/stargazers","contributors_url":"https://api.github.com/repos/hmrc/api-gateway/contributors","subscribers_url":"https://api.github.com/repos/hmrc/api-gateway/subscribers","subscription_url":"https://api.github.com/repos/hmrc/api-gateway/subscription","commits_url":"https://api.github.com/repos/hmrc/api-gateway/commits{/sha}","git_commits_url":"https://api.github.com/repos/hmrc/api-gateway/git/commits{/sha}","comments_url":"https://api.github.com/repos/hmrc/api-gateway/comments{/number}","issue_comment_url":"https://api.github.com/repos/hmrc/api-gateway/issues/comments{/number}","contents_url":"https://api.github.com/repos/hmrc/api-gateway/contents/{+path}","compare_url":"https://api.github.com/repos/hmrc/api-gateway/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hmrc/api-gateway/merges","archive_url":"https://api.github.com/repos/hmrc/api-gateway/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hmrc/api-gateway/downloads","issues_url":"https://api.github.com/repos/hmrc/api-gateway/issues{/number}","pulls_url":"https://api.github.com/repos/hmrc/api-gateway/pulls{/number}","milestones_url":"https://api.github.com/repos/hmrc/api-gateway/milestones{/number}","notifications_url":"https://api.github.com/repos/hmrc/api-gateway/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hmrc/api-gateway/labels{/name}","releases_url":"https://api.github.com/repos/hmrc/api-gateway/releases{/id}","deployments_url":"https://api.github.com/repos/hmrc/api-gateway/deployments","created_at":"2016-12-09T09:42:04Z","updated_at":"2016-12-12T16:10:12Z","pushed_at":"2016-12-28T17:19:11Z","git_url":"git://github.com/hmrc/api-gateway.git","ssh_url":"git@github.com:hmrc/api-gateway.git","clone_url":"https://github.com/hmrc/api-gateway.git","svn_url":"https://github.com/hmrc/api-gateway","homepage":"","size":138,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/hmrc/api-gateway/pulls/11"},"html":{"href":"https://github.com/hmrc/api-gateway/pull/11"},"issue":{"href":"https://api.github.com/repos/hmrc/api-gateway/issues/11"},"comments":{"href":"https://api.github.com/repos/hmrc/api-gateway/issues/11/comments"},"review_comments":{"href":"https://api.github.com/repos/hmrc/api-gateway/pulls/11/comments"},"review_comment":{"href":"https://api.github.com/repos/hmrc/api-gateway/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/hmrc/api-gateway/pulls/11/commits"},"statuses":{"href":"https://api.github.com/repos/hmrc/api-gateway/statuses/f51d94a323e80a91efe51448cd656ad6fdfbe3c7"}}}}
{"actor":{"display_login":"edinhodzic"}}