-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: admin routes and basic authentication
- Loading branch information
Showing
5 changed files
with
134 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { fetch, Headers, RequestInit, HeadersInit } from 'undici' | ||
Check warning on line 1 in packages/repco-cli/src/client.ts GitHub Actions / build-and-test
|
||
|
||
interface RepcoRequestInit extends RequestInit { body?: any } | ||
export async function request(path: string, init: RepcoRequestInit = {}) { | ||
const url = process.env.REPCO_URL + '/api/admin' + path | ||
const token = process.env.REPCO_ADMIN_TOKEN | ||
const headers = new Headers(init.headers) | ||
headers.set('authorization', 'Bearer ' + token) | ||
headers.set('accept', 'application/json') | ||
|
||
if (init.body) { | ||
init.body = JSON.stringify(init.body) | ||
headers.set('content-type', 'application/json') | ||
} | ||
|
||
const res = await fetch(url, { ...init, headers }) | ||
if (!res.ok) { | ||
const text = await res.text() | ||
let error | ||
try { | ||
const data = JSON.parse(text) | ||
error = data.error | ||
} catch (_err) { | ||
error = text | ||
} | ||
throw new Error('Remote error: ' + error) | ||
} | ||
return res.json() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import express from 'express' | ||
import { ServerError } from '../error.js' | ||
|
||
export const router = express.Router() | ||
|
||
const ADMIN_TOKEN = process.env.REPCO_ADMIN_TOKEN | ||
|
||
// check auth | ||
router.use((req, res, next) => { | ||
if (!ADMIN_TOKEN || ADMIN_TOKEN.length < 16) { | ||
res.locals.log.warn('ADMIN_TOKEN is not set or too short (min 16 characters needed). Admin access disabled.') | ||
return next(new ServerError(403, 'Unauthorized')) | ||
} | ||
|
||
const authHeader = req.headers['authorization']; | ||
if (!authHeader || !authHeader.startsWith('Bearer ')) { | ||
return next(new ServerError(403, 'Unauthorized')) | ||
} | ||
|
||
const token = authHeader.substring(7) | ||
if (token != ADMIN_TOKEN) { | ||
return next(new ServerError(403, 'Unauthorized')) | ||
} | ||
|
||
next() | ||
}) | ||
|
||
router.get('/test', async (req, res) => { | ||
await new Promise((resolve) => setTimeout(resolve, 1000)) | ||
res.send({ ok: true }) | ||
}) | ||
|
||
router.post('/test', async (req, res) => { | ||
const body = req.body | ||
console.log('received body', body) | ||
res.send({ ok: true }) | ||
}) | ||
|
||
|
||
// create repo | ||
router.post('/repo', async (req, res) => { | ||
const body = req.body | ||
console.log('received body', body) | ||
res.send({ ok: true }) | ||
}) | ||
// list repos | ||
router.get('/repo', async (req, res) => { | ||
const body = req.body | ||
console.log('received body', body) | ||
res.send({ ok: true }) | ||
}) | ||
|
||
|
||
// create datasource | ||
router.post('/repo/:repodid/ds', async (req, res) => { | ||
const body = req.body | ||
console.log('received body', body) | ||
res.send({ ok: true }) | ||
}) | ||
|
||
// modify datasource | ||
router.put('/repo/:repodid/ds/:dsuid', async (req, res) => { | ||
const body = req.body | ||
console.log('received body', body) | ||
res.send({ ok: true }) | ||
}) | ||
// get datasource | ||
router.get('/repo/:repodid/ds/:dsuid', async (req, res) => { | ||
const body = req.body | ||
console.log('received body', body) | ||
res.send({ ok: true }) | ||
}) | ||
// list datasources | ||
router.get('/repo/:repodid/ds', async (req, res) => { | ||
const body = req.body | ||
console.log('received body', body) | ||
res.send({ ok: true }) | ||
}) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters