Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .well-known file example #366

Open
whusterj opened this issue Nov 15, 2024 · 1 comment
Open

Add .well-known file example #366

whusterj opened this issue Nov 15, 2024 · 1 comment
Assignees
Labels
good first issue Good for newcomers

Comments

@whusterj
Copy link
Member

whusterj commented Nov 15, 2024

Sometimes we need to serve a static file from the root to verify domain ownership, e.g. mydomain.com/.well-known/verification.

We should add an example of how to do this with the bootstrapper. Here's some sample code from another project:

# server/myproject/urls.py

# ... other code

# Add to urlpatterns in this file
urlpatterns = [
    path(r"staff/", admin.site.urls),
    path(r"", include("myproject.common.favicon_urls")),
    path(r"", include("myproject.common.stripe_domain_verification_urls")),
    path(r"", include("myproject.common.urls")),
]
# server/myproject/common/urls.py

from django.conf import settings
from django.urls import path

from myproject.common import views as common_views

urlpatterns = []

if settings.IN_STAGING or settings.IN_PROD or settings.IN_REVIEW:
    urlpatterns += [
        path(
            r".well-known/apple-developer-merchantid-domain-association",
            common_views.StripeDomainVerificationFile.as_view(),
            name="domain-verify",
        ),
    ]

Serve the file from STATIC FILES storage. In this example, the file was placed in the client/public/apple-pay directory. This gets collected to staticfiles when the app is built and so the file can be retrieved from staticfiles_storage in the directory apple-pay/...

# server/myproject/common/views.py

from django.contrib.staticfiles.storage import staticfiles_storage
from django.http import FileResponse, Http404
from django.views.generic.base import View
from rest_framework import permissions


class StripeDomainVerificationFile(View):

    permission_classes = (permissions.AllowAny,)

    def get(self, request):
        try:
            file = staticfiles_storage.open("apple-pay/apple-developer-merchantid-domain-association", "rb")
            return FileResponse(file, as_attachment=False)
        except FileNotFoundError:
            raise Http404("Static file not found")
@whusterj whusterj self-assigned this Nov 15, 2024
@whusterj whusterj added the good first issue Good for newcomers label Nov 15, 2024
@whusterj
Copy link
Member Author

Thanks @JSicardiNC for this example implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant