implements ask-for-on-demand-tls for testing Caddy.

modified:   README.rst
	modified:   make_post_sell/routes.py
	modified:   make_post_sell/views/misc.py
This commit is contained in:
Russell Ballestrini 2022-06-29 10:27:23 -04:00
parent 3c189c2192
commit 36930dffdd
3 changed files with 52 additions and 12 deletions

View file

@ -99,3 +99,24 @@ You may source the new Python virtual environment during development:
# source env/bin/activate.fish
. env/bin/activate
Python Pyramid Shell
==========================
If you want to use an interactive Python interpreter to interact with Make Post Sell app/models & DB:
.. code-block:: bash
pshell development.ini
.. code-block:: python
# begin the database transaction.
request.tm.begin()
# flush / commit all changes stored the the sqlachemy session.
request.dbsession.flush()
# commit/close the database transaction to really make changes.
request.tm.commit()

View file

@ -4,6 +4,8 @@ def includeme(config):
config.add_route("robots", "/robots.txt")
config.add_route("markup-editor-preview", "/markup-editor-preview")
config.add_route("ask-for-on-demand-tls", "/ask-for-on-demand-tls")
config.add_route("home", "/")
config.add_route("search", "/search")

View file

@ -5,8 +5,8 @@ from ..models.product import get_all_products
# needed for favicon
import os
from pyramid.response import (
FileResponse,
Response,
FileResponse,
Response,
)
from make_post_sell.lib.render import markdown_to_html
@ -29,29 +29,46 @@ def favicon_view(request):
def robots_view(request):
"""Load and return either default robots.txt or version from .ini"""
response = Response(
body=request.app.get(
"robots_dot_txt",
DEFAULT_ROBOTS_DOT_TXT
).lstrip()
body=request.app.get("robots_dot_txt", DEFAULT_ROBOTS_DOT_TXT).lstrip()
)
response.content_type = "text/plain"
return response
@view_config(route_name="ask-for-on-demand-tls", require_csrf=False)
def ask_for_on_demand_tls(request):
"""
This implements Caddy ask protocol defined here:
* https://caddyserver.com/docs/caddyfile/options#on-demand-tls
Caddy will make an HTTP request to the given URL with a query string of ?domain= containing
the value of the domain name. If the endpoint returns a 2xx status code, Caddy will be authorized
to obtain a certificate for that name. Any other status code will result in cancelling issuance
of the certificate.
"""
domain_name_requesting_tls = request.params.get("domain")
if domain_name_requesting_tls:
# "SELECT domain_name FROM mps_shop;"
result = request.dbsession.execute("SELECT domain_name FROM mps_shop;")
rows = result.all()
registered_domains = [r[0] for r in rows]
if domain_name_requesting_tls in registered_domains:
return Response(status_code=200)
return Response(status_code=401)
# I set require_csfr to false because new browsers
# don't save 3rd party cookies anymore.
# This ajax endpoint should work whether a user is authenticated or not.
@view_config(
route_name="markup-editor-preview",
renderer="string",
xhr=True,
require_csrf=False
route_name="markup-editor-preview", renderer="string", xhr=True, require_csrf=False
)
def markup_editor_preview(request):
"""AJAJ: Accept Markup data param, return HTML"""
return markdown_to_html(request.params["data"], request.shop)
# currently only supports markdown, but eventually we could support others.
#try:
# try:
# return markdown_to_html(request.params["data"], request.shop)
#except:
# except:
# return "we could not create markdown to html preview."