Skip to content

Commit 0446594

Browse files
authored
Merge pull request #100 from SmoFlaDru/dev-benno
Add passkey creation and login, select activity chart time range, nav bar improvements, dark theme switching, fixes.
2 parents 306e09e + 8a13c75 commit 0446594

36 files changed

+1368
-566
lines changed

.idea/Spybot2.iml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Spybot2/settings.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
For the full list of settings and their values, see
1010
https://docs.djangoproject.com/en/4.1/ref/settings/
1111
"""
12-
12+
from datetime import timedelta
1313
from pathlib import Path
1414
import environ
1515
import os
@@ -45,7 +45,10 @@
4545
# SECURITY WARNING: don't run with debug turned on in production!
4646
DEBUG = env.bool('DEBUG', False)
4747

48-
ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1']
48+
ALLOWED_HOSTS = [SERVER_IP, TS_IP, 'localhost', '127.0.0.1', 'spybot.localhost.direct']
49+
50+
CSRF_TRUSTED_ORIGINS = [f"https://{SERVER_IP}"]
51+
4952
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
5053

5154
CSRF_COOKIE_SECURE = not env.bool('INSECURE_COOKIES', False)
@@ -94,6 +97,17 @@
9497
},
9598
]
9699

100+
MIDDLEWARE = [
101+
"django.middleware.security.SecurityMiddleware",
102+
"django.contrib.sessions.middleware.SessionMiddleware",
103+
"django.middleware.common.CommonMiddleware",
104+
"django.middleware.csrf.CsrfViewMiddleware",
105+
"django.contrib.auth.middleware.AuthenticationMiddleware",
106+
"spybot.auth.last_seen_middleware.middleware",
107+
"django.contrib.messages.middleware.MessageMiddleware",
108+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
109+
]
110+
97111
WSGI_APPLICATION = 'Spybot2.wsgi.application'
98112

99113

@@ -137,9 +151,14 @@
137151
AUTH_USER_MODEL = 'spybot.MergedUser'
138152

139153
AUTHENTICATION_BACKENDS = [
140-
'spybot.auth.backend.LinkAuthBackend',
154+
'django.contrib.auth.backends.ModelBackend',
155+
'spybot.auth.backend.link_backend.LinkAuthBackend',
141156
]
142157

158+
# Passkeys
159+
FIDO_SERVER_NAME = "Spybot local"
160+
#KEY_ATTACHMENT = passkeys.Attachment.CROSS_PLATFORM
161+
143162

144163
# Internationalization
145164
# https://docs.djangoproject.com/en/4.1/topics/i18n/

frontend/main.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import * as ApexCharts from 'apexcharts/dist/apexcharts.min.js'
22
import * as tabler from '@tabler/core/dist/js/tabler.min.js'
33
import * as htmx from 'htmx.org/dist/htmx.min.js'
4+
import * as passkeys from './passkeys';
45

56
import "@tabler/core/dist/css/tabler.min.css"
67
import "@tabler/core/dist/css/tabler-vendors.min.css"
78

8-
export { ApexCharts, tabler, htmx }
9+
10+
window.passkeys = passkeys;
11+
12+
export { ApexCharts, tabler, htmx, passkeys }

frontend/package-lock.json

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"author": "",
1010
"license": "ISC",
1111
"dependencies": {
12+
"@simplewebauthn/browser": "^10.0.0",
1213
"@tabler/core": "^1.0.0-beta20",
1314
"apexcharts": "^3.36.3",
1415
"htmx.org": "^1.9.2"

frontend/passkeys.js

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {startAuthentication, startRegistration} from '@simplewebauthn/browser'
2+
3+
const sendToServerForVerificationAndLogin = async (response) => {
4+
try {
5+
console.log("sendToServerForVerificationAndLogin:", response);
6+
const verificationResp = await fetch('/passkeys/verify-authentication', {
7+
method: 'POST',
8+
headers: {
9+
'Content-Type': 'application/json',
10+
},
11+
body: JSON.stringify(response),
12+
});
13+
const verificationJSON = await verificationResp.json();
14+
15+
// Show UI appropriate for the `verified` status
16+
if (verificationJSON && verificationJSON.verified) {
17+
console.log("success")
18+
window.location.href = '/profile';
19+
} else {
20+
console.log("error", verificationJSON);
21+
}
22+
} catch (e) {
23+
handleError(e);
24+
}
25+
}
26+
27+
const handleError = (error) => {
28+
console.log("An error occurred:", error);
29+
}
30+
31+
export const autocomplete = async () => {
32+
try {
33+
console.log("Setting up autocomplete");
34+
const options = await fetch('/passkeys/generate-authentication-options')
35+
const optionsPayload = (await options.json())["publicKey"]
36+
// delete options["allowedCredentials"]
37+
const response = await startAuthentication(optionsPayload, true)
38+
await sendToServerForVerificationAndLogin(response)
39+
} catch (e) {
40+
handleError(e);
41+
}
42+
};
43+
44+
export const create = async () => {
45+
const resp = await fetch('/passkeys/generate-registration-options');
46+
47+
let attResp;
48+
try {
49+
// Pass the options to the authenticator and wait for a response
50+
attResp = await startRegistration((await resp.json()).publicKey);
51+
} catch (error) {
52+
// Some basic error handling
53+
if (error.name === 'InvalidStateError') {
54+
throw Error('Error: Authenticator was probably already registered by user');
55+
} else {
56+
throw Error(error);
57+
}
58+
}
59+
60+
// POST the response to the endpoint that calls
61+
// @simplewebauthn/server -> verifyRegistrationResponse()
62+
const verificationResp = await fetch('/passkeys/verify-registration', {
63+
method: 'POST',
64+
headers: {
65+
'Content-Type': 'application/json',
66+
},
67+
body: JSON.stringify(attResp),
68+
});
69+
70+
// Wait for the results of verification
71+
const verificationJSON = await verificationResp.json();
72+
73+
// Show UI appropriate for the `verified` status
74+
if (verificationJSON && verificationJSON.verified) {
75+
return 'Success!';
76+
} else {
77+
throw Error(`Oh no, something went wrong! Response: <pre>${JSON.stringify(verificationJSON)}</pre>`);
78+
}
79+
}

frontend/rollup.config.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export default {
66
input: 'main.js',
77
output: {
88
dir: 'output',
9-
format: 'iife'
9+
format: 'iife',
10+
name: 'jsbundle',
1011
},
1112
plugins: [nodeResolve(), css({'output': 'main.css'})]
1213
};

0 commit comments

Comments
 (0)