Skip to content

Commit

Permalink
Add support for set_url_signature in AuthToken
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary authored Aug 1, 2024
1 parent 2db58e3 commit b037310
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cloudinary/auth_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


def generate(url=None, acl=None, start_time=None, duration=None,
expiration=None, ip=None, key=None, token_name=AUTH_TOKEN_NAME):
expiration=None, ip=None, key=None, token_name=AUTH_TOKEN_NAME, **_):

if expiration is None:
if duration is not None:
Expand Down
2 changes: 1 addition & 1 deletion cloudinary/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,7 @@ def cloudinary_url(source, **options):
transformation = re.sub(r'([^:])/+', r'\1/', transformation)

signature = None
if sign_url and not auth_token:
if sign_url and (not auth_token or auth_token.pop('set_url_signature', False)):
to_sign = "/".join(__compact([transformation, source_to_sign]))
if long_url_signature:
# Long signature forces SHA256
Expand Down
18 changes: 13 additions & 5 deletions test/test_auth_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def test_explicit_authToken_should_override_global_setting(self):
"w_300/sample.jpg?__cld_token__=st=222222222~exp=222222322~hmac"
"=55cfe516530461213fe3b3606014533b1eca8ff60aeab79d1bb84c9322eebc1f")

def test_should_set_url_signature(self):
cloudinary.config(private_cdn=True)
url, _ = cloudinary.utils.cloudinary_url("sample.jpg", sign_url=True,
auth_token={"key": ALT_KEY, "start_time": 222222222, "duration": 100,
"set_url_signature": True},
type="authenticated", transformation={"crop": "scale", "width": 300})
self.assertEqual("http://test123-res.cloudinary.com/image/authenticated/s--Ok4O32K7--/"
"c_scale,w_300/sample.jpg?__cld_token__=st=222222222~exp=222222322~hmac"
"=92a55aaed531b2dab074074bbd1430120119f9cb1b901656925dda2e514a63cc", url)

def test_should_compute_expiration_as_start_time_plus_duration(self):
cloudinary.config(private_cdn=True)
token = {"key": KEY, "start_time": 11111111, "duration": 300}
Expand All @@ -75,8 +85,7 @@ def test_should_compute_expiration_as_start_time_plus_duration(self):

def test_generate_token_string(self):
user = "foobar" # we can't rely on the default "now" value in tests
token_options = {"key": KEY, "duration": 300, "acl": "/*/t_%s" % user}
token_options["start_time"] = 222222222 # we can't rely on the default "now" value in tests
token_options = {"key": KEY, "duration": 300, "acl": "/*/t_%s" % user, "start_time": 222222222}
cookie_token = cloudinary.utils.generate_auth_token(**token_options)
self.assertEqual(
cookie_token,
Expand All @@ -97,8 +106,8 @@ def test_should_ignore_url_if_acl_is_provided(self):
)

def test_should_support_multiple_acls(self):
token_options = {"key": KEY, "duration": 3600,
"acl": ["/i/a/*", "/i/a/*", "/i/a/*"],
token_options = {"key": KEY, "duration": 3600,
"acl": ["/i/a/*", "/i/a/*", "/i/a/*"],
"start_time": 222222222}

cookie_token = cloudinary.utils.generate_auth_token(**token_options)
Expand Down Expand Up @@ -128,6 +137,5 @@ def test_should_support_url_without_acl(self):
)



if __name__ == '__main__':
unittest.main()

0 comments on commit b037310

Please sign in to comment.