def test_normalized_request_string_with_custom_port(self): req = "GET / HTTP/1.1\r\nHost: example.com:88\r\n\r\n" req = Request.from_bytes(req) req.authorization = ("MAC", {"ts": "1", "nonce": "2"}) sigstr = "1\n2\nGET\n/\nexample.com\n88\n\n" mysigstr = get_normalized_request_string(req) self.assertEquals(sigstr, mysigstr)
def test_normalized_request_string_with_https_scheme(self): req = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n" req = Request.from_bytes(req) req.authorization = ("MAC", {"ts": "1", "nonce": "2"}) req.scheme = "https" sigstr = "1\n2\nGET\n/\nexample.com\n443\n\n" mysigstr = get_normalized_request_string(req) self.assertEquals(sigstr, mysigstr)
def get_signature(request, key, hashmod=None, params=None): """Get the MAC signature for the given request. This function calculates the MAC signature for the given request and returns it as a string. If the "params" parameter is not None, it is assumed to be a pre-parsed dict of MAC parameters as one might find in the Authorization header. If it is missing or None then the Authorization header from the request will be parsed to determine the necessary parameters. """ if params is None: params = utils.parse_authz_header(request, {}) if hashmod is None: hashmod = sha1 sigstr = utils.get_normalized_request_string(request, params) return b64encode(hmac.new(key, sigstr, hashmod).digest())
def get_signature(request, key, hashmod=None, params=None): """Get the MAC signature for the given request. This function calculates the MAC signature for the given request and returns it as a string. If the "params" parameter is not None, it is assumed to be a pre-parsed dict of MAC parameters as one might find in the Authorization header. If it is missing or None then the Authorization header from the request will be parsed to determine the necessary parameters. """ if params is None: params = utils.parse_authz_header(request, {}) if hashmod is None: hashmod = sha1 sigstr = utils.get_normalized_request_string(request, params) # The spec mandates that ids and keys must be ascii. # It's therefore safe to encode like this before doing the signature. sigstr = sigstr.encode("ascii") key = key.encode("ascii") return utils.b64encode(hmac.new(key, sigstr, hashmod).digest())
def test_normalized_request_string_against_example_from_spec(self): # This is the example used in Section 3.2.1 of RFC-TODO req = "POST /request?b5=%3D%253D&a3=a&c%40=&a2=r%20b&c2&a3=2+q"\ " HTTP/1.1\r\n"\ "Host: example.com\r\n"\ "\r\n"\ "Hello World!" params = { "ts": "264095", "nonce": "7d8f3e4a", "ext": "a,b,c", } sigstr = "264095\n"\ "7d8f3e4a\n"\ "POST\n"\ "/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b&c2&a3=2+q\n"\ "example.com\n"\ "80\n"\ "a,b,c\n" # IanB, *thank you* for Request.from_bytes! req = Request.from_bytes(req) mysigstr = get_normalized_request_string(req, params) self.assertEquals(sigstr, mysigstr)