Skip to content

Commit

Permalink
Merge pull request #24 from vsoch/add/view-specific-permissions
Browse files Browse the repository at this point in the history
adding scopes to customize permissions needed for each view
  • Loading branch information
vsoch committed Oct 29, 2020
2 parents bf846de + c4f06e2 commit 403c032
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Critical items to know are:
- changed behaviour

## [master](https://github.com/vsoch/django-oci/tree/master)
- View specific permission (pull,push) required (0.0.13)
- Adding Django ratelimit to protect views (0.0.12)
- Added authentication (0.0.11)
- Django OCI core release without authentication (0.0.1)
Expand Down
2 changes: 1 addition & 1 deletion django_oci/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.0.12"
__version__ = "0.0.13"
default_app_config = "django_oci.apps.DjangoOciConfig"
7 changes: 5 additions & 2 deletions django_oci/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@


def is_authenticated(
request, repository=None, must_be_owner=True, repository_exists=True
request, repository=None, must_be_owner=True, repository_exists=True, scopes=None
):
"""
Function to check if a request is authenticated, a repository and the request is required.
Expand All @@ -51,6 +51,9 @@ def is_authenticated(
must_be_owner (bool) : if must be owner is true, requires push
reposity_exists (bool) : flag to indicate that the repository exists.
"""
# Scopes default to push and pull, more conservative
scopes = scopes or ["push", "pull"]

# Derive the view name from the request PATH_INFO
func, two, three = resolve(request.META["PATH_INFO"])
view_name = "%s.%s" % (func.__module__, func.__name__)
Expand All @@ -76,7 +79,7 @@ def is_authenticated(
# Case 3: False and response will return request for auth
user = get_user(request)
if not user:
headers = {"Www-Authenticate": get_challenge(request, name)}
headers = {"Www-Authenticate": get_challenge(request, name, scopes=scopes)}
return False, Response(status=401, headers=headers), user

# Denied for any other reason
Expand Down
2 changes: 1 addition & 1 deletion django_oci/views/blobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get(self, request, *args, **kwargs):
digest = kwargs.get("digest")

# If allow_continue False, return response
allow_continue, response, _ = is_authenticated(request, name)
allow_continue, response, _ = is_authenticated(request, name, scopes=["pull"])
if not allow_continue:
return response

Expand Down
6 changes: 4 additions & 2 deletions django_oci/views/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ def get(self, request, *args, **kwargs):
raise Http404

# If allow_continue False, return response
allow_continue, response, _ = is_authenticated(request, repository)
allow_continue, response, _ = is_authenticated(
request, repository, scopes=["pull"]
)
if not allow_continue:
return response

Expand Down Expand Up @@ -213,7 +215,7 @@ def get(self, request, *args, **kwargs):
tag = kwargs.get("tag")

# If allow_continue False, return response
allow_continue, response, _ = is_authenticated(request, name)
allow_continue, response, _ = is_authenticated(request, name, scopes=["pull"])
if not allow_continue:
return response

Expand Down

0 comments on commit 403c032

Please sign in to comment.