Skip to content

Commit

Permalink
local dev proxy for developing with tls
Browse files Browse the repository at this point in the history
  • Loading branch information
harrybrwn committed Apr 15, 2023
1 parent 8f982e4 commit 168873a
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 96 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ docker-compose*.yml

# macOS-specific files
.DS_Store

# Certificates
*.crt
*.key
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PUBLIC_API_HOST=https://api.hrry.local
PUBLIC_OIDC_URL=https://auth.hrry.local
PUBLIC_API_HOST=https://api.hrry.me-local
PUBLIC_OIDC_URL=https://auth.hrry.me-local
PUBLIC_OIDC_CLIENT_ID="testid"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ pnpm-debug.log*

# macOS-specific files
.DS_Store

# Certificates
*.crt
*.key
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ COPY --from=static-builder /opt/harrybrwn.github.io/dist /
#
FROM nginx:${NGINX_VERSION}-alpine as nginx
COPY --from=static-builder /opt/harrybrwn.github.io/dist /var/www/harrybrwn.github.io
COPY config/ /etc/nginx/
COPY config/nginx/ /etc/nginx/
# Just for lols
RUN sed -i 's/Server: nginx/Server: butts/g' /usr/sbin/nginx
ENV NGINX_ENVSUBST_TEMPLATE_SUFFIX=".conf"
Expand Down
2 changes: 1 addition & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default defineConfig({
syntaxHighlight: "prism",
},
server: {
port: 80,
port: 3000,
},
vite: {
plugins: [ViteYaml()],
Expand Down
22 changes: 22 additions & 0 deletions config/dev-proxy.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {
server_name _ localhost hrry.local hrry.me-local;
listen 80;
listen 443 ssl http2;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_certificate /etc/nginx/certs/hrry.me.crt;
ssl_certificate_key /etc/nginx/certs/hrry.me.key;
server_tokens off;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $http_host;
proxy_pass http://dev:3000;
}
}
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: "3.9"

services:
dev:
networks:
- hrry.me

nginx:
networks:
- hrry.me

dev-tls-proxy:
image: nginx:1.23.3
networks:
- hrry.me
ports:
- "80:80"
- "443:443"
volumes:
- ./config/dev-proxy.conf:/etc/nginx/conf.d/default.conf:ro
- ./config/certs:/etc/nginx/certs:ro

networks:
hrry.me:
external: true
8 changes: 0 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ services:
- NGINX_VERSION=1.23.3
- GITHUB_REF_NAME=${GITHUB_REF_NAME:-main}
- GITHUB_SHA=${GITHUB_SHA:-}
networks:
- hrry.me
ports:
- "80:80"
- "443:443"
Expand All @@ -31,8 +29,6 @@ services:
context: .
target: dev
command: ["yarn", "dev", "--host"]
networks:
- hrry.me
working_dir: /opt/harrybrwn.github.io
volumes:
- .:/opt/harrybrwn.github.io
Expand All @@ -42,7 +38,3 @@ services:
user: 1000:1000
ports:
- "3000:3000"

networks:
hrry.me:
external: true
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"dev:server": "node scripts/generate.js && ASTRO_OUTPUT=server astro dev",
"build:server": "scripts/build-server.sh",
"preview:server": "ASTRO_OUTPUT=server astro preview",
"container": "docker container run --rm -it -p 3000:3000 -e PORT=3000 --name astro-server harrybrwn/harrybrwn.github.io-server:latest"
"container": "docker container run --rm -it -p 3000:3000 -e PORT=3000 --name astro-server harrybrwn/harrybrwn.github.io-server:latest",
"image": "docker buildx bake -f docker-bake.hcl"
},
"workspaces": [
"./packages/astro/*",
Expand Down
48 changes: 0 additions & 48 deletions src/lib/api/oidc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { load_pkce, delete_pkce } from "./pkce";

export interface OAuth2Token {
access_token: string;
expires_in: number;
Expand Down Expand Up @@ -45,49 +43,3 @@ export const consent = async (challenge: string): Promise<RedirectTarget> => {
body: JSON.stringify({ consent_challenge: challenge }),
}).then((res: Response) => res.json());
};

export const getOIDCToken = (
code: string,
oidc_url: string,
clientId: string
) => {
let pkce = load_pkce();
if (pkce == null) {
return Promise.reject(new Error("failed to load pkce state"));
}
let u = new URL("/oauth2/token", oidc_url);
let req = {
code: code,
grant_type: "authorization_code",
client_id: clientId,
code_verifier: pkce.verifier,
};
return (
fetch(u.toString(), {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: new URLSearchParams(req),
})
// parse the token and handle errors
.then(async (res: Response) => {
if (!res.ok) {
try {
const message = await res.json();
return Promise.reject(new Error(message.error_description));
} catch (e) {
return Promise.reject(new Error(res.statusText));
}
}
return res.json();
})
// Save token to localStorage and return it
.then((token: OAuth2Token) => {
let raw = JSON.stringify(token);
localStorage.setItem("oidc_token", raw);
delete_pkce();
return token;
})
);
};
2 changes: 1 addition & 1 deletion src/lib/keyboard-shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const listener = (themer: () => void, help: Modal | null): Listener => {
// TODO make this into a modal instead of a redirect.
if (!ev.ctrlKey && !ev.shiftKey) {
ev.preventDefault();
window.location.href = "/login";
window.location.href = "/login/";
}
break;
case "?":
Expand Down
2 changes: 1 addition & 1 deletion src/modified.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"2023-01-11T04:58:30.000Z"
],
"content/Free Will.md": [
"2023-04-06T14:53:08.274Z"
"2023-04-14T03:30:38.022Z"
],
"content/GPG.md": [
"2023-01-12T18:21:03.000Z"
Expand Down
81 changes: 48 additions & 33 deletions src/pages/login.astro
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import Loading from "~/components/Loading.astro";
<script>
import { isEmail } from "~/lib/email";
import { Client } from "~/lib/api";
import { consent, getOIDCToken, type RedirectTarget } from "~/lib/api/oidc";
import { consent, type RedirectTarget } from "~/lib/api/oidc";

const handleError = (err: Error, parent?: HTMLElement) => {
console.error(err);
Expand All @@ -50,6 +50,8 @@ import Loading from "~/components/Loading.astro";
parent.appendChild(error);
}
};
const sleep = (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

const getCookie = (name: string) => {
const value = `; ${document.cookie}`;
Expand Down Expand Up @@ -81,7 +83,21 @@ import Loading from "~/components/Loading.astro";
credsBox.style.visibility = "hidden";
}

if (consent_challenge) {
if (authToken !== null && login_challenge) {
api
.login({ login_challenge })
.then(async (res: RedirectTarget) => {
if (!res.redirect_to) {
throw new Error("no redirect target");
}
await sleep(1000);
window.location.href = res.redirect_to;
})
.catch((err: Error) => {
handleError(err, form);
form.reset();
});
} else if (consent_challenge) {
consent(consent_challenge)
.then((res: RedirectTarget) => {
window.location.href = res.redirect_to;
Expand All @@ -90,38 +106,37 @@ import Loading from "~/components/Loading.astro";
handleError(err, form);
form.reset();
});
} else {
form.addEventListener("submit", (e: SubmitEvent) => {
e.preventDefault();
let d = new FormData(e.target as HTMLFormElement);
let ident = d.get("identifier") as string;
let req = {
password: d.get("password") as string,
email: "",
username: "",
login_challenge: null,
};
if (isEmail(ident)) {
req.email = ident;
} else {
req.username = ident;
}
api
.login(req)
.then((blob: RedirectTarget) => {
console.log(blob);
if (blob.redirect_to) {
window.location.href = blob.redirect_to;
} else {
window.location.pathname = "/";
}
})
.catch((err: Error) => {
handleError(err, form);
form.reset();
});
});
}

form.addEventListener("submit", (e: SubmitEvent) => {
e.preventDefault();
let d = new FormData(e.target as HTMLFormElement);
let ident = d.get("identifier") as string;
let req = {
password: d.get("password") as string,
email: "",
username: "",
login_challenge: null,
};
if (isEmail(ident)) {
req.email = ident;
} else {
req.username = ident;
}
api
.login(req)
.then((blob: RedirectTarget) => {
if (blob.redirect_to) {
window.location.href = blob.redirect_to;
} else {
window.location.pathname = "/";
}
})
.catch((err: Error) => {
handleError(err, form);
form.reset();
});
});
</script>

<style is:global>
Expand Down

0 comments on commit 168873a

Please sign in to comment.