Python pyramid.httpexceptions.HTTPUnauthorized() Examples

The following are 5 code examples of pyramid.httpexceptions.HTTPUnauthorized(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyramid.httpexceptions , or try the search function .
Example #1
Source File: __init__.py    From shavar with Mozilla Public License 2.0 6 votes vote down vote up
def _check_signature(self, request, key):
        """Check the Hawk auth signature on the request.

        This method checks the Hawk signature on the request against the
        supplied signing key.  If missing or invalid then HTTPUnauthorized
        is raised.

        The TokenServerAuthenticationPolicy implementation wraps the default
        HawkAuthenticationPolicy implementation with some logging.
        """
        supercls = super(TokenServerAuthenticationPolicy, self)
        try:
            return supercls._check_signature(request, key)
        except HTTPUnauthorized:
            logger.warn("Authentication Failed: invalid hawk signature")
            raise 
Example #2
Source File: test_user.py    From shavar with Mozilla Public License 2.0 6 votes vote down vote up
def test_that_hawkauth_is_used_by_default(self):
        # Generate signed request.
        req = self.make_request()
        tokenid, key = self.policy.encode_hawk_id(req, 42)
        hawkauthlib.sign_request(req, tokenid, key)
        # That should be enough to authenticate.
        self.assertEqual(req.authenticated_userid, 42)
        self.assertEqual(req.user.get("uid"), 42)
        # Check that it rejects invalid Hawk ids.
        req = self.make_request()
        hawkauthlib.sign_request(req, tokenid, key)
        authz = req.environ["HTTP_AUTHORIZATION"]
        req.environ["HTTP_AUTHORIZATION"] = authz.replace(tokenid, "XXXXXX")
        with self.assertRaises(HTTPUnauthorized):
            req.authenticated_userid
        # And that the rejection gets raised when accessing request.user
        self.assertRaises(HTTPUnauthorized, getattr, req, "user") 
Example #3
Source File: test_user.py    From shavar with Mozilla Public License 2.0 6 votes vote down vote up
def test_checking_of_token_node_assignment(self):
        # Generate a token for one node
        req = self.make_request(environ={
            "HTTP_HOST": "host1.com",
        })
        tokenid, key = self.policy.encode_hawk_id(req, 42)
        # It can authenticate for requests to that node.
        hawkauthlib.sign_request(req, tokenid, key)
        self.assertEqual(req.authenticated_userid, 42)
        self.assertEqual(req.user.get("uid"), 42)
        # But not requests to some other node.
        req = self.make_request(environ={
            "HTTP_HOST": "host2.com",
        })
        hawkauthlib.sign_request(req, tokenid, key)
        with self.assertRaises(HTTPUnauthorized):
            req.authenticated_userid 
Example #4
Source File: views.py    From nova-ideo with GNU Affero General Public License v3.0 6 votes vote down vote up
def graphqlview(context, request):  #pylint: disable=W0613
    token = request.headers.get('X-Api-Key', '')
    is_private = getattr(request.root, 'only_for_members', False)
    if is_private and not auth_user(token, request):
        response = HTTPUnauthorized()
        response.content_type = 'application/json'
        return response

    if request.method == 'OPTIONS':
        response = Response(status=200, body=b'')
        response.headerlist = []  # we have to reset headerlist
        response.headerlist.extend(
            (
                ('Access-Control-Allow-Origin', '*'),
                ('Access-Control-Allow-Headers', 'Content-Type'),
            )
        )
    else:
        solver = graphql_wsgi(schema)
        response = solver(request)
        response.headerlist.append(
            ('Access-Control-Allow-Origin', '*')
        )

    return response 
Example #5
Source File: server.py    From channelstream with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def listen(request):
    """
    Handles long polling connections
    ---
    get:
      tags:
      - "Client API"
      summary: "Handles long polling connections"
      description: ""
      operationId: "listen"
      produces:
      - "application/json"
      responses:
        200:
          description: "Success"
    """
    server_state = get_state()
    config = request.registry.settings
    conn_id = utils.uuid_from_string(request.params.get("conn_id"))
    connection = server_state.connections.get(conn_id)
    if not connection:
        raise HTTPUnauthorized()
    # attach a queue to connection
    connection.queue = Queue()
    connection.deliver_catchup_messages()
    request.response.app_iter = yield_response(request, connection, config)
    return request.response