From 72cca62fbf384d289f377bbfc90fe8ce2206ba9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=2E=20Mat=C3=ADas=20Quezada?= Date: Thu, 4 Aug 2022 11:44:11 +0200 Subject: [PATCH] refactor: allow multiple domain names --- src/3-github/GHRepository.ts | 4 ++-- src/3-github/gh-utils.ts | 33 ++++++++++++++++++++++++++++++++- src/5-app/useGithubAuth.ts | 23 ++++++++++------------- src/config.json | 8 ++++---- src/manifest.webmanifest | 4 ++-- 5 files changed, 50 insertions(+), 22 deletions(-) diff --git a/src/3-github/GHRepository.ts b/src/3-github/GHRepository.ts index 075ad54..9f4de34 100644 --- a/src/3-github/GHRepository.ts +++ b/src/3-github/GHRepository.ts @@ -1,5 +1,5 @@ import { POST } from '../1-core/http'; -import { COMMIT_ENDPOINT } from '../config.json'; +import { ghCommitEndpoint } from './gh-utils'; import { GithubToken } from './GithubAuth'; import { GithubGraphQlApi } from './GithubGraphQlApi'; import { GithubRestApi, MediaType } from './GithubRestApi'; @@ -160,7 +160,7 @@ export class GHRepository { this.commiting = true; - return POST(COMMIT_ENDPOINT, body, { keepalive: isUrgent }).finally( + return POST(ghCommitEndpoint, body, { keepalive: isUrgent }).finally( () => (this.commiting = false), ); } diff --git a/src/3-github/gh-utils.ts b/src/3-github/gh-utils.ts index 50ad3fb..ffd6cf5 100644 --- a/src/3-github/gh-utils.ts +++ b/src/3-github/gh-utils.ts @@ -1,8 +1,15 @@ import { getQueryParameter } from '../0-dom/getQueryParameter'; import { Note } from '../2-entities/Note'; +import { + AUTH_ENDPOINT, + CLIENT_ID_DEV, + CLIENT_ID_PROD, + COMMIT_ENDPOINT, + GH_SCOPE, + VALID_ORIGINS, +} from '../config.json'; import { GithubToken } from './GithubAuth'; import { GithubUsername } from './models/GHApiUser'; - const GH_API = 'https://api.github.com'; export const ghRepository = getQueryParameter('repo', 'pensieve-data'); @@ -20,3 +27,27 @@ export function ghAuthHeaders(token: GithubToken) { export function ghPublicPage(username: GithubUsername, note: Note) { return `https://github.com/${username}/${ghRepository}/blob/main/note/${note.id}`; } + +const isLocalHost = location.hostname === 'localhost'; + +export const appOrigin = getOrigin(); +export const ghScope = GH_SCOPE; +export const ghClientId = isLocalHost ? CLIENT_ID_DEV : CLIENT_ID_PROD; + +const endpointOrigin = isLocalHost ? VALID_ORIGINS[0] : appOrigin; + +export const ghAuthEndpoint = `${endpointOrigin}${AUTH_ENDPOINT}`; +export const ghCommitEndpoint = `${endpointOrigin}${COMMIT_ENDPOINT}`; + +function getOrigin() { + const { origin } = location; + + if ( + !isLocalHost || + !VALID_ORIGINS.some((x: string) => origin.startsWith(x)) + ) { + throw new Error(`Invalid origin: ${origin}`); + } + + return origin; +} diff --git a/src/5-app/useGithubAuth.ts b/src/5-app/useGithubAuth.ts index 3e1aa2b..01864bf 100644 --- a/src/5-app/useGithubAuth.ts +++ b/src/5-app/useGithubAuth.ts @@ -1,24 +1,21 @@ import { ClientStorage } from '@amatiasq/client-storage'; import { useState } from 'react'; import { parseParams } from '../1-core/url'; +import { + appOrigin, + ghAuthEndpoint, + ghClientId, + ghScope, +} from '../3-github/gh-utils'; import { GithubAuth, GithubToken } from '../3-github/GithubAuth'; import { GithubUsers } from '../3-github/GithubUsers'; import { GithubUsername } from '../3-github/models/GHApiUser'; -import { - APP_ROOT, - AUTH_ENDPOINT, - CLIENT_ID_DEV, - CLIENT_ID_PROD, - GH_SCOPE, -} from '../config.json'; - -const isLocalHost = location.hostname === 'localhost'; const auth = new GithubAuth({ - scope: GH_SCOPE, - endpoint: AUTH_ENDPOINT, - clientId: isLocalHost ? CLIENT_ID_DEV : CLIENT_ID_PROD, - redirectUri: isLocalHost ? location.origin : APP_ROOT, + scope: ghScope, + endpoint: ghAuthEndpoint, + clientId: ghClientId, + redirectUri: appOrigin, }); const user = new ClientStorage('notes.gh-user', { diff --git a/src/config.json b/src/config.json index b15c52d..e6788f8 100644 --- a/src/config.json +++ b/src/config.json @@ -1,9 +1,9 @@ { "GH_SCOPE": "repo gist", "GH_API": "https://api.github.com", - "AUTH_ENDPOINT": "https://pensieve.amatiasq.com/auth", - "COMMIT_ENDPOINT": "https://pensieve.amatiasq.com/commit", - "APP_ROOT": "https://pensieve.amatiasq.com/", + "AUTH_ENDPOINT": "/auth", + "COMMIT_ENDPOINT": "/commit", "CLIENT_ID_DEV": "c55d2c46c215f9a4c3cb", - "CLIENT_ID_PROD": "09580e62d66243b6b09d" + "CLIENT_ID_PROD": "09580e62d66243b6b09d", + "VALID_ORIGINS": ["https://pensieve.amatiasq.com"] } diff --git a/src/manifest.webmanifest b/src/manifest.webmanifest index 368ff71..cba768b 100644 --- a/src/manifest.webmanifest +++ b/src/manifest.webmanifest @@ -5,8 +5,8 @@ "display": "standalone", "background_color": "#1c1c1c", "theme_color": "#1c1c1c", - "start_url": "https://pensieve.amatiasq.com/", - "scope": "https://pensieve.amatiasq.com/", + "start_url": "../", + "scope": "/", "icons": [ { "src": "/assets/manifest-icon-192.png",