Skip to content

Commit 1ce1434

Browse files
feat install sveltiacms (#192)
* Create a custom Sveltia CMS OAuth * static: move showcase content to gitsubmodule * showcase is deployed on every MR (feature env)
1 parent ef60f56 commit 1ce1434

File tree

30 files changed

+348
-58
lines changed

30 files changed

+348
-58
lines changed

.github/workflows/deploy-feature.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,12 @@ jobs:
5656
options: |
5757
-l feature -e branch=${{ env.BRANCH_NAME }}
5858
key: ${{ secrets.SSH_PRIVATE_KEY }}
59+
- name: Deploy static
60+
uses: dawidd6/action-ansible-playbook@v2
61+
with:
62+
playbook: showcase.yml
63+
directory: deploy
64+
vault_password: ${{secrets.ANSIBLE_VAULT_KEY}}
65+
options: |
66+
-l feature -e branch=${{ env.BRANCH_NAME }}
67+
key: ${{ secrets.SSH_PRIVATE_KEY }}

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "static/content"]
2+
path = static/content
3+
url = git@github.com:TelesCoop/iarbre-showcase-content.git
4+
branch = main

back/decapcms_auth/README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Sveltia CMS / Decap CMS Github Oauth Django Application
2+
3+
Add Github Authentication to DecapCMS through yor Django Application.
4+
5+
## Getting Started
6+
7+
### 1. Create and register your Github OAuth Application
8+
9+
Please read [official doc](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/creating-an-oauth-app).
10+
11+
Authorization callback URL should be :
12+
13+
```
14+
https://<your application url>/callback
15+
```
16+
17+
Copy-paste application id and client secret.
18+
19+
### 2. Install Decap CMS in your Django Project
20+
21+
#### `settings.py` :
22+
23+
- Install application :
24+
25+
```py
26+
INSTALLED_APPS = [
27+
...,
28+
"decapcms_auth",
29+
]
30+
```
31+
32+
- Set up required variables :
33+
34+
```py
35+
36+
DECAP_CMS_AUTH = {
37+
"OAUTH_CLIENT_ID": "<public application client id>",
38+
"OAUTH_CLIENT_SECRET": "<private application client secret>"
39+
"SCOPE": "repo,user"
40+
}
41+
```
42+
43+
⚠️ `OAUTH_CLIENT_SECRET` should not be publicly disclosed.
44+
45+
- Define this environment variable:
46+
47+
```py
48+
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
49+
```
50+
51+
#### `urls.py`
52+
53+
Include the Decap CMS urls :
54+
55+
```py
56+
57+
from decapcms_auth import urls as decapcmsauth_urls
58+
...
59+
urlpatterns = [
60+
...
61+
path("cms/", include(decapcmsauth_urls)),
62+
...
63+
]
64+
```
65+
66+
### 3. In your Decap CMS config.yml
67+
68+
```yml
69+
backend:
70+
name: github
71+
branch: main
72+
repo: <your repo>
73+
base_url: <base url of your application>
74+
auth_endpoint: /cms/auth # /cms
75+
```

back/decapcms_auth/__init__.py

Whitespace-only changes.

back/decapcms_auth/apps.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.apps import AppConfig
2+
3+
4+
class DecapcmsAuthConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "decapcms_auth"
7+
verbose_name = "DecapCMS auth config"

back/decapcms_auth/migrations/__init__.py

Whitespace-only changes.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<html>
2+
<body>
3+
<script>
4+
(function () {
5+
function recieveMessage(e) {
6+
console.log("recieveMessage %o", e);
7+
// send message to main window with da app
8+
const provider = "github"
9+
const state = "success"
10+
window.opener.postMessage(
11+
`authorization:${provider}:${state}:${JSON.stringify({{content | safe}})}`,
12+
e.origin,
13+
);
14+
}
15+
window.addEventListener("message", recieveMessage, false);
16+
// Start handshare with parent
17+
console.log("Sending message: %o", "github");
18+
window.opener.postMessage("authorizing:github", "*");
19+
})();
20+
</script>
21+
</body>
22+
</html>

back/decapcms_auth/urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from django.urls import path
2+
from rest_framework import routers
3+
4+
from .views import callback, auth
5+
6+
router = routers.DefaultRouter()
7+
8+
urlpatterns = [
9+
path("auth/", auth),
10+
path("callback/", callback),
11+
]

back/decapcms_auth/views.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from requests_oauthlib import OAuth2Session
2+
from django.conf import settings
3+
from django.shortcuts import redirect, render
4+
from django.http import HttpResponseBadRequest
5+
6+
AUTHORIZATION_BASE_URL = "https://github.com/login/oauth/authorize"
7+
TOKEN_URL = "https://github.com/login/oauth/access_token"
8+
CLIENT_ID = settings.DECAP_CMS_AUTH["OAUTH_CLIENT_ID"]
9+
CLIENT_SECRET = settings.DECAP_CMS_AUTH["OAUTH_CLIENT_SECRET"]
10+
SCOPE = settings.DECAP_CMS_AUTH["SCOPE"]
11+
12+
13+
def auth(request):
14+
"""Redirect to Github auth"""
15+
github = OAuth2Session(client_id=CLIENT_ID, scope=SCOPE)
16+
authorization_url, _ = github.authorization_url(AUTHORIZATION_BASE_URL)
17+
return redirect(authorization_url)
18+
19+
20+
def callback(request):
21+
"""Retrieve access token"""
22+
state = request.GET.get("state", "")
23+
try:
24+
github = OAuth2Session(CLIENT_ID, state=state, scope=SCOPE)
25+
token = github.fetch_token(
26+
TOKEN_URL,
27+
client_secret=CLIENT_SECRET,
28+
authorization_response=request.get_full_path(),
29+
)
30+
content = {"token": token.get("access_token", ""), "provider": "github"}
31+
return render(request, "decapcms_auth/callback.html", {"content": content})
32+
33+
except BaseException:
34+
return HttpResponseBadRequest()

back/iarbre_data/settings.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import getconf
1717
from django.http import Http404
1818

19+
# Required for decap auth
20+
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
21+
1922
# Build paths inside the project like this: BASE_DIR / 'subdir'.
2023
BASE_DIR = Path(__file__).resolve().parent.parent
2124
DATA_DIR = BASE_DIR / "file_data"
@@ -64,6 +67,7 @@
6467
"django_extensions",
6568
"telescoop_backup",
6669
"rest_framework",
70+
"decapcms_auth",
6771
]
6872

6973
MIDDLEWARE = [
@@ -76,6 +80,10 @@
7680
"django.middleware.clickjacking.XFrameOptionsMiddleware",
7781
]
7882

83+
# Mandatory for Decap CMS Auth
84+
# https://docs.djangoproject.com/en/5.1/ref/middleware/#cross-origin-opener-policy
85+
SECURE_CROSS_ORIGIN_OPENER_POLICY = None
86+
7987
if IS_LOCAL_DEV:
8088
CORS_ALLOW_ALL_ORIGINS = True
8189
CORS_ALLOW_METHODS = [
@@ -233,6 +241,13 @@
233241
"djangorestframework_camel_case.parser.CamelCaseJSONParser",
234242
),
235243
}
244+
245+
DECAP_CMS_AUTH = {
246+
"OAUTH_CLIENT_ID": config.getstr("github_oauth.client_id"),
247+
"OAUTH_CLIENT_SECRET": config.getstr("github_oauth.client_secret"),
248+
"SCOPE": "repo,user",
249+
}
250+
236251
# For macOS users, we need to set the GDAL_LIBRARY_PATH and GEOS_LIBRARY_PATH to the path of the libraries
237252
if sys.platform == "darwin":
238253
GDAL_LIBRARY_PATH = os.environ.get("GDAL_LIBRARY_PATH")

0 commit comments

Comments
 (0)