{"action":"created","comment":{"url":"https://api.github.com/repos/mozilla-services/readinglist/pulls/comments/22922975","id":22922975,"diff_hunk":"@@ -0,0 +1,69 @@\n+from zope.interface import implementer\n+from pyramid import authentication as base_auth\n+from pyramid.interfaces import IAuthenticationPolicy, IAuthorizationPolicy\n+from pyramid.httpexceptions import HTTPServiceUnavailable\n+from fxa.oauth import Client as OAuthClient\n+from fxa import errors as fxa_errors\n+\n+\n+def check_credentials(username, password, request):\n+    \"\"\"Basic auth backdoor. Used for testing (unit and loads)\n+    \"\"\"\n+    settings = request.registry.settings\n+    credentials = settings.get('readinglist.basic_auth_backdoor', '')\n+    is_matching = tuple(credentials.split(':', 1)) == (username, password)\n+    return [username] if is_matching else []\n+\n+\n+class BasicAuthAuthenticationPolicy(base_auth.BasicAuthAuthenticationPolicy):\n+    def __init__(self, *args, **kwargs):\n+        super(BasicAuthAuthenticationPolicy, self).__init__(check_credentials,\n+                                                            *args,\n+                                                            **kwargs)\n+\n+\n+@implementer(IAuthenticationPolicy)\n+class Oauth2AuthenticationPolicy(base_auth.CallbackAuthenticationPolicy):\n+    def __init__(self, realm='Realm'):\n+        self.realm = realm\n+\n+    def unauthenticated_userid(self, request):\n+        user_id = self._get_credentials(request)\n+        return user_id\n+\n+    def forget(self, request):\n+        \"\"\"A no-op. Credentials are sent on every request.\n+        Return WWW-Authenticate Realm header for Bearer token.\n+        \"\"\"\n+        return [('WWW-Authenticate', 'Bearer realm=\"%s\"' % self.realm)]\n+\n+    def _get_credentials(self, request):\n+        authorization = request.headers.get('Authorization', '')\n+\n+        try:\n+            authmeth, auth = authorization.split(' ', 1)\n+            assert authmeth.lower() == 'bearer'\n+        except (AssertionError, ValueError):\n+            return None\n+\n+        server_url = request.registry.settings['fxa-oauth.oauth_uri']\n+        scope = request.registry.settings['fxa-oauth.scope']\n+\n+        auth_client = OAuthClient(server_url=server_url)\n+        try:\n+            profile = auth_client.verify_token(token=auth, scope=scope)\n+            user_id = profile['user']\n+        except fxa_errors.OutOfProtocolError:\n+            raise HTTPServiceUnavailable()\n+        except (fxa_errors.InProtocolError, fxa_errors.TrustError):\n+            return None\n+        return user_id\n+\n+\n+@implementer(IAuthorizationPolicy)\n+class AuthorizationPolicy(object):\n+    def permits(self, context, principals, permission):\n+        return permission in principals","path":"readinglist/authentication.py","position":66,"original_position":66,"commit_id":"8ab0a14810c01a96738ec5a8b52dc819467567d6","original_commit_id":"8ab0a14810c01a96738ec5a8b52dc819467567d6","user":{"login":"Natim","id":229453,"avatar_url":"https://avatars.githubusercontent.com/u/229453?v=3","gravatar_id":"","url":"https://api.github.com/users/Natim","html_url":"https://github.com/Natim","followers_url":"https://api.github.com/users/Natim/followers","following_url":"https://api.github.com/users/Natim/following{/other_user}","gists_url":"https://api.github.com/users/Natim/gists{/gist_id}","starred_url":"https://api.github.com/users/Natim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Natim/subscriptions","organizations_url":"https://api.github.com/users/Natim/orgs","repos_url":"https://api.github.com/users/Natim/repos","events_url":"https://api.github.com/users/Natim/events{/privacy}","received_events_url":"https://api.github.com/users/Natim/received_events","type":"User","site_admin":false},"body":"This looks really strange to me. Permission != principals. The principal is the list of roles not the list of permissions.","created_at":"2015-01-14T08:52:58Z","updated_at":"2015-01-14T08:52:58Z","html_url":"https://github.com/mozilla-services/readinglist/pull/22#discussion_r22922975","pull_request_url":"https://api.github.com/repos/mozilla-services/readinglist/pulls/22","_links":{"self":{"href":"https://api.github.com/repos/mozilla-services/readinglist/pulls/comments/22922975"},"html":{"href":"https://github.com/mozilla-services/readinglist/pull/22#discussion_r22922975"},"pull_request":{"href":"https://api.github.com/repos/mozilla-services/readinglist/pulls/22"}}},"pull_request":{"url":"https://api.github.com/repos/mozilla-services/readinglist/pulls/22","id":26499750,"html_url":"https://github.com/mozilla-services/readinglist/pull/22","diff_url":"https://github.com/mozilla-services/readinglist/pull/22.diff","patch_url":"https://github.com/mozilla-services/readinglist/pull/22.patch","issue_url":"https://api.github.com/repos/mozilla-services/readinglist/issues/22","number":22,"state":"open","locked":false,"title":"Back to the origin : Cornice","user":{"login":"leplatrem","id":546692,"avatar_url":"https://avatars.githubusercontent.com/u/546692?v=3","gravatar_id":"","url":"https://api.github.com/users/leplatrem","html_url":"https://github.com/leplatrem","followers_url":"https://api.github.com/users/leplatrem/followers","following_url":"https://api.github.com/users/leplatrem/following{/other_user}","gists_url":"https://api.github.com/users/leplatrem/gists{/gist_id}","starred_url":"https://api.github.com/users/leplatrem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leplatrem/subscriptions","organizations_url":"https://api.github.com/users/leplatrem/orgs","repos_url":"https://api.github.com/users/leplatrem/repos","events_url":"https://api.github.com/users/leplatrem/events{/privacy}","received_events_url":"https://api.github.com/users/leplatrem/received_events","type":"User","site_admin":false},"body":"The branch is on top of #9 \r\n\r\nThe objective of this PR is to reach the feature set we had with Eve. Also, some patterns might emerge on top of cornice, most probably regarding resource oriented CRUD views\r\n\r\n* [x] Cornice boiler plate\r\n* [x] Basic views for device and article resources \r\n* [x] Memory backend\r\n* [x] Basic authentication\r\n* [x] Basic views for device read status\r\n* [x] FxA authentication\r\n* [x] Record validation\r\n* [x] Hello view\r\n* [x] Heartbeat view\r\n* [x] Fix distinction between 401 and 403 behaviour\r\n\r\n**Postponed**\r\n* [ ] Filtering\r\n* [ ] Sorting\r\n* [ ] Pagination (python [paginate](https://github.com/Pylons/paginate) ?)\r\n","created_at":"2014-12-23T08:29:51Z","updated_at":"2015-01-14T08:52:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mozilla-services/readinglist/pulls/22/commits","review_comments_url":"https://api.github.com/repos/mozilla-services/readinglist/pulls/22/comments","review_comment_url":"https://api.github.com/repos/mozilla-services/readinglist/pulls/comments/{number}","comments_url":"https://api.github.com/repos/mozilla-services/readinglist/issues/22/comments","statuses_url":"https://api.github.com/repos/mozilla-services/readinglist/statuses/8ab0a14810c01a96738ec5a8b52dc819467567d6","head":{"label":"mozilla-services:switch_to_cornice","ref":"switch_to_cornice","sha":"8ab0a14810c01a96738ec5a8b52dc819467567d6","user":{"login":"mozilla-services","id":1066228,"avatar_url":"https://avatars.githubusercontent.com/u/1066228?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla-services","html_url":"https://github.com/mozilla-services","followers_url":"https://api.github.com/users/mozilla-services/followers","following_url":"https://api.github.com/users/mozilla-services/following{/other_user}","gists_url":"https://api.github.com/users/mozilla-services/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla-services/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla-services/subscriptions","organizations_url":"https://api.github.com/users/mozilla-services/orgs","repos_url":"https://api.github.com/users/mozilla-services/repos","events_url":"https://api.github.com/users/mozilla-services/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla-services/received_events","type":"Organization","site_admin":false},"repo":{"id":27563721,"name":"readinglist","full_name":"mozilla-services/readinglist","owner":{"login":"mozilla-services","id":1066228,"avatar_url":"https://avatars.githubusercontent.com/u/1066228?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla-services","html_url":"https://github.com/mozilla-services","followers_url":"https://api.github.com/users/mozilla-services/followers","following_url":"https://api.github.com/users/mozilla-services/following{/other_user}","gists_url":"https://api.github.com/users/mozilla-services/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla-services/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla-services/subscriptions","organizations_url":"https://api.github.com/users/mozilla-services/orgs","repos_url":"https://api.github.com/users/mozilla-services/repos","events_url":"https://api.github.com/users/mozilla-services/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla-services/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla-services/readinglist","description":"Reading List Server","fork":false,"url":"https://api.github.com/repos/mozilla-services/readinglist","forks_url":"https://api.github.com/repos/mozilla-services/readinglist/forks","keys_url":"https://api.github.com/repos/mozilla-services/readinglist/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla-services/readinglist/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla-services/readinglist/teams","hooks_url":"https://api.github.com/repos/mozilla-services/readinglist/hooks","issue_events_url":"https://api.github.com/repos/mozilla-services/readinglist/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla-services/readinglist/events","assignees_url":"https://api.github.com/repos/mozilla-services/readinglist/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla-services/readinglist/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla-services/readinglist/tags","blobs_url":"https://api.github.com/repos/mozilla-services/readinglist/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla-services/readinglist/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla-services/readinglist/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla-services/readinglist/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla-services/readinglist/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla-services/readinglist/languages","stargazers_url":"https://api.github.com/repos/mozilla-services/readinglist/stargazers","contributors_url":"https://api.github.com/repos/mozilla-services/readinglist/contributors","subscribers_url":"https://api.github.com/repos/mozilla-services/readinglist/subscribers","subscription_url":"https://api.github.com/repos/mozilla-services/readinglist/subscription","commits_url":"https://api.github.com/repos/mozilla-services/readinglist/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla-services/readinglist/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla-services/readinglist/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla-services/readinglist/issues/comments/{number}","contents_url":"https://api.github.com/repos/mozilla-services/readinglist/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla-services/readinglist/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla-services/readinglist/merges","archive_url":"https://api.github.com/repos/mozilla-services/readinglist/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla-services/readinglist/downloads","issues_url":"https://api.github.com/repos/mozilla-services/readinglist/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla-services/readinglist/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla-services/readinglist/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla-services/readinglist/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla-services/readinglist/labels{/name}","releases_url":"https://api.github.com/repos/mozilla-services/readinglist/releases{/id}","created_at":"2014-12-04T22:48:28Z","updated_at":"2014-12-24T13:56:16Z","pushed_at":"2015-01-14T00:10:34Z","git_url":"git://github.com/mozilla-services/readinglist.git","ssh_url":"git@github.com:mozilla-services/readinglist.git","clone_url":"https://github.com/mozilla-services/readinglist.git","svn_url":"https://github.com/mozilla-services/readinglist","homepage":null,"size":644,"stargazers_count":3,"watchers_count":3,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":21,"forks":0,"open_issues":21,"watchers":3,"default_branch":"master"}},"base":{"label":"mozilla-services:master","ref":"master","sha":"8ba60cb5d8aa4b127ee03eb32cde1d24cbb3f7d5","user":{"login":"mozilla-services","id":1066228,"avatar_url":"https://avatars.githubusercontent.com/u/1066228?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla-services","html_url":"https://github.com/mozilla-services","followers_url":"https://api.github.com/users/mozilla-services/followers","following_url":"https://api.github.com/users/mozilla-services/following{/other_user}","gists_url":"https://api.github.com/users/mozilla-services/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla-services/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla-services/subscriptions","organizations_url":"https://api.github.com/users/mozilla-services/orgs","repos_url":"https://api.github.com/users/mozilla-services/repos","events_url":"https://api.github.com/users/mozilla-services/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla-services/received_events","type":"Organization","site_admin":false},"repo":{"id":27563721,"name":"readinglist","full_name":"mozilla-services/readinglist","owner":{"login":"mozilla-services","id":1066228,"avatar_url":"https://avatars.githubusercontent.com/u/1066228?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla-services","html_url":"https://github.com/mozilla-services","followers_url":"https://api.github.com/users/mozilla-services/followers","following_url":"https://api.github.com/users/mozilla-services/following{/other_user}","gists_url":"https://api.github.com/users/mozilla-services/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla-services/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla-services/subscriptions","organizations_url":"https://api.github.com/users/mozilla-services/orgs","repos_url":"https://api.github.com/users/mozilla-services/repos","events_url":"https://api.github.com/users/mozilla-services/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla-services/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla-services/readinglist","description":"Reading List Server","fork":false,"url":"https://api.github.com/repos/mozilla-services/readinglist","forks_url":"https://api.github.com/repos/mozilla-services/readinglist/forks","keys_url":"https://api.github.com/repos/mozilla-services/readinglist/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla-services/readinglist/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla-services/readinglist/teams","hooks_url":"https://api.github.com/repos/mozilla-services/readinglist/hooks","issue_events_url":"https://api.github.com/repos/mozilla-services/readinglist/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla-services/readinglist/events","assignees_url":"https://api.github.com/repos/mozilla-services/readinglist/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla-services/readinglist/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla-services/readinglist/tags","blobs_url":"https://api.github.com/repos/mozilla-services/readinglist/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla-services/readinglist/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla-services/readinglist/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla-services/readinglist/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla-services/readinglist/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla-services/readinglist/languages","stargazers_url":"https://api.github.com/repos/mozilla-services/readinglist/stargazers","contributors_url":"https://api.github.com/repos/mozilla-services/readinglist/contributors","subscribers_url":"https://api.github.com/repos/mozilla-services/readinglist/subscribers","subscription_url":"https://api.github.com/repos/mozilla-services/readinglist/subscription","commits_url":"https://api.github.com/repos/mozilla-services/readinglist/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla-services/readinglist/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla-services/readinglist/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla-services/readinglist/issues/comments/{number}","contents_url":"https://api.github.com/repos/mozilla-services/readinglist/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla-services/readinglist/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla-services/readinglist/merges","archive_url":"https://api.github.com/repos/mozilla-services/readinglist/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla-services/readinglist/downloads","issues_url":"https://api.github.com/repos/mozilla-services/readinglist/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla-services/readinglist/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla-services/readinglist/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla-services/readinglist/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla-services/readinglist/labels{/name}","releases_url":"https://api.github.com/repos/mozilla-services/readinglist/releases{/id}","created_at":"2014-12-04T22:48:28Z","updated_at":"2014-12-24T13:56:16Z","pushed_at":"2015-01-14T00:10:34Z","git_url":"git://github.com/mozilla-services/readinglist.git","ssh_url":"git@github.com:mozilla-services/readinglist.git","clone_url":"https://github.com/mozilla-services/readinglist.git","svn_url":"https://github.com/mozilla-services/readinglist","homepage":null,"size":644,"stargazers_count":3,"watchers_count":3,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":21,"forks":0,"open_issues":21,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mozilla-services/readinglist/pulls/22"},"html":{"href":"https://github.com/mozilla-services/readinglist/pull/22"},"issue":{"href":"https://api.github.com/repos/mozilla-services/readinglist/issues/22"},"comments":{"href":"https://api.github.com/repos/mozilla-services/readinglist/issues/22/comments"},"review_comments":{"href":"https://api.github.com/repos/mozilla-services/readinglist/pulls/22/comments"},"review_comment":{"href":"https://api.github.com/repos/mozilla-services/readinglist/pulls/comments/{number}"},"commits":{"href":"https://api.github.com/repos/mozilla-services/readinglist/pulls/22/commits"},"statuses":{"href":"https://api.github.com/repos/mozilla-services/readinglist/statuses/8ab0a14810c01a96738ec5a8b52dc819467567d6"}}}}