Skip to content

Commit

Permalink
Token scope resource can have several resource actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ipanova committed Apr 11, 2017
1 parent bea26db commit 0e7f39e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions plugins/pulp_docker/plugins/token_util.py
@@ -1,6 +1,7 @@
from cStringIO import StringIO
import json
import logging
import re
import urllib
import urlparse

Expand Down Expand Up @@ -82,12 +83,13 @@ def parse_401_response_headers(response_headers):
"""
auth_header = response_headers.get('www-authenticate')
if auth_header is None:
raise IOError("401 responses are expected to conatin authentication information")
raise IOError("401 responses are expected to contain authentication information")
auth_header = auth_header[len("Bearer "):]
auth_header = re.split(',(?=[^=,]+=)', auth_header)

# The remaining string consists of comma seperated key=value pairs
auth_dict = {}
for key, value in (item.split('=') for item in auth_header.split(',')):
for key, value in (item.split('=') for item in auth_header):
# The value is a string within a string, ex: '"value"'
auth_dict[key] = json.loads(value)
return auth_dict
16 changes: 16 additions & 0 deletions plugins/test/unit/plugins/test_token_util.py
Expand Up @@ -84,3 +84,19 @@ def test_dict_created(self):
ret = token_util.parse_401_response_headers(headers)
self.assertDictEqual(ret, {"realm": "https://auth.docker.io/token",
"service": "registry.docker.io"})

def test_multiple_resource_actions(self):
"""
Ensure that the www-authenticate header is correctly parsed into a dict
when there are multiple resource actions specified.
"""
headers = {'www-authenticate':
'Bearer realm="https://auth.docker.io/token",service="registry.docker.io",'
'some=1,scope="repository:samalba/my-app:pull,push",foo="bar",answer=42'}
ret = token_util.parse_401_response_headers(headers)
self.assertDictEqual(ret, {"realm": "https://auth.docker.io/token",
"service": "registry.docker.io",
"answer": 42,
"foo": "bar",
"scope": "repository:samalba/my-app:pull,push",
"some": 1})

0 comments on commit 0e7f39e

Please sign in to comment.