diff --git a/docs/REST.md b/docs/REST.md index 3160cf7..c6004a0 100644 --- a/docs/REST.md +++ b/docs/REST.md @@ -91,7 +91,9 @@ Register a new user. **Authentication**: no -**Parameters**: Username, password, confirm +**Parameters**: Username, password, confirm, invite(optional) + +Invite is an optional invite code to bypass disabled registration. **Response**: If successful, returns a json object with the users stream key. Otherwise returns `{error: "error reason"}` diff --git a/package.json b/package.json index 6c3531f..e2b210f 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,15 @@ { "name": "satyr", - "version": "0.9.3", + "version": "0.9.4", "description": "A livestreaming server.", "license": "AGPL-3.0", "author": "knotteye", "scripts": { "start": "ts-node src/index.ts", - "user": "ts-node src/cli.ts", + "cli": "ts-node src/cli.ts", "setup": "sh install/setup.sh", - "migrate": "ts-node src/migrate.ts" + "migrate": "ts-node src/migrate.ts", + "invite": "ts-node src/cli.ts --invite" }, "repository": { "type": "git", diff --git a/src/api.ts b/src/api.ts index a803d63..d421bab 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,9 +1,10 @@ import * as db from "./database"; +import * as base64id from "base64id"; import { config } from "./config"; import {unlink} from "fs"; -async function register(name: string, password: string, confirm: string): Promise { - if(!config['satyr']['registration']) return {"error":"registration disabled"}; +async function register(name: string, password: string, confirm: string, invite?: boolean): Promise { + if(!config['satyr']['registration'] && !invite) return {"error":"registration disabled"}; if(name.includes(';') || name.includes(' ') || name.includes('\'')) return {"error":"illegal characters"}; if(password !== confirm) return {"error":"mismatched passwords"}; for(let i=0;i{ return t; } -export { register, update, changepwd, changesk, login, updateChat, deleteVODs, getConfig }; \ No newline at end of file +async function genInvite(): Promise{ + var invitecode: string = base64id.generateId(); + await db.query('INSERT INTO invites (code) VALUES (\"'+invitecode+'\")'); + return invitecode; +} + +async function validInvite(code: string): Promise{ + if(typeof(code) !== "string" || code === "") return false; + var result = await db.query('SELECT code FROM invites WHERE code='+db.raw.escape(code)); + if(!result[0] || result[0]['code'] !== code) return false; + return true; +} + +async function useInvite(code: string): Promise{ + if(validInvite(code)) await db.query('DELETE FROM invites WHERE code='+db.raw.escape(code)); +} + +export { register, update, changepwd, changesk, login, updateChat, deleteVODs, getConfig, genInvite, useInvite, validInvite }; \ No newline at end of file diff --git a/src/cli.ts b/src/cli.ts index 110ecc6..1109154 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,4 +1,5 @@ -import * as db from "./database" +import * as db from "./database"; +import * as api from "./api"; import * as flags from "flags"; db.init(); @@ -6,6 +7,7 @@ db.init(); flags.defineString('adduser', '', 'User to add'); flags.defineString('rmuser', '', 'User to remove'); flags.defineString('password', '', 'password to hash'); +flags.defineBoolean('invite', false, 'generate invite code'); flags.parse(); @@ -23,4 +25,13 @@ if(flags.get('rmuser') !== ''){ else console.log("Could not remove user."); process.exit(); }); +} + +if(flags.get('invite')){ + var config = require("./config").config; + api.genInvite().then((r: string) => { + console.log('invite code: '+r); + console.log('Direct the user to https://'+config['satyr']['domain']+'/invite/'+r); + process.exit(); + }); } \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index 4bd6ec0..0fdeaa4 100644 --- a/src/config.ts +++ b/src/config.ts @@ -16,7 +16,7 @@ const config: Object = { domain: '', registration: false, email: null, - restrictedNames: [ 'live', 'user', 'users', 'register', 'login' ], + restrictedNames: [ 'live', 'user', 'users', 'register', 'login', 'invite' ], rootredirect: '/users/live', version: process.env.npm_package_version, }, localconfig['satyr']), diff --git a/src/db/2.ts b/src/db/2.ts new file mode 100644 index 0000000..cfe7ae0 --- /dev/null +++ b/src/db/2.ts @@ -0,0 +1,8 @@ +import * as db from "../database"; + +async function run () { + await db.query('CREATE TABLE IF NOT EXISTS invites(code VARCHAR(150))'); + await db.query('INSERT INTO db_meta (version) VALUES (2)'); +} + +export { run } \ No newline at end of file diff --git a/src/http.ts b/src/http.ts index b5f9c53..8eb06dc 100644 --- a/src/http.ts +++ b/src/http.ts @@ -224,6 +224,23 @@ async function initAPI() { }); }); app.post('/api/register', (req, res) => { + if("invite" in req.body){ + api.validInvite(req.body.invite).then((v) => { + if(v){ + api.register(req.body.username, req.body.password, req.body.confirm, true).then((result) => { + if(result[0]) return genToken(req.body.username).then((t) => { + res.cookie('Authorization', t, {maxAge: 604800000, httpOnly: true, sameSite: 'Lax'}); + res.json(result); + api.useInvite(req.body.invite); + return; + }); + res.json(result); + }); + } + else res.json({error: "invalid invite code"}); + }); + } + else api.register(req.body.username, req.body.password, req.body.confirm).then( (result) => { if(result[0]) return genToken(req.body.username).then((t) => { res.cookie('Authorization', t, {maxAge: 604800000, httpOnly: true, sameSite: 'Lax'}); @@ -486,6 +503,12 @@ async function initSite(openReg) { } else res.render('login.njk',njkconf); }); + app.get('/invite/:code', (req, res) => { + if(tryDecode(req.cookies.Authorization)) { + res.redirect('/profile'); + } + else res.render('invite.njk',Object.assign({icode: req.params.code}, njkconf)); + }); app.get('/register', (req, res) => { if(tryDecode(req.cookies.Authorization) || !openReg) { res.redirect(njkconf.rootredirect); diff --git a/templates/invite.njk b/templates/invite.njk new file mode 100644 index 0000000..c181635 --- /dev/null +++ b/templates/invite.njk @@ -0,0 +1,20 @@ +{% extends "base.njk" %} +{% block content %} +

You've been invited to {{ sitename }}

Already registered? Log in here.

+ +
+ Username:

+ Password:

+ Confirm:


+ + +

+ + + {% include "tos.html" %}
+ + +{% endblock %} \ No newline at end of file