def add_auth(self, request): if self.credentials is None: raise NoCredentialsError if 'Date' not in request.headers: request.headers['Date'] = formatdate(usegmt=True) if self.credentials.token: request.headers['X-Amz-Security-Token'] = self.credentials.token new_hmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha256) new_hmac.update(request.headers['Date'].encode('utf-8')) encoded_signature = encodebytes(new_hmac.digest()).strip() signature = ('AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=%s,Signature=%s' % (self.credentials.access_key, 'HmacSHA256', encoded_signature.decode('utf-8'))) request.headers['X-Amzn-Authorization'] = signature
def sign_string(self, string_to_sign): new_hmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha1) new_hmac.update(string_to_sign.encode('utf-8')) return encodebytes(new_hmac.digest()).strip().decode('utf-8')