Created
April 12, 2020 17:25
-
-
Save prawnsalad/883bc79df3d664a2d447a0c1e7a68aa1 to your computer and use it in GitHub Desktop.
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
const request = require('request-promise'); | |
const tokens = require('../../kiwibnc/src/libs/tokens'); | |
let config = {}; | |
/** | |
* Add these to your config file: | |
* [extension.autoregister] | |
* config.auth_api_url="" // your HTTP API for authing users | |
* config.default_net_name="" | |
* config.network_address="" | |
* config.network_port=6667 | |
* config.network_tls=false | |
* | |
* Complete authExternal() below. It should return true/false if the user+pass | |
* is correct. | |
* You need to create a HTTP service somewhere that takes the user/pass POST and auths it. | |
*/ | |
module.exports.init = async function init(hooks, app) { | |
config = app.conf.get('extension.autoregister', {}); | |
hooks.on('auth', onBncAuth); | |
}; | |
async function onBncAuth(event) { | |
// event = {username, networkName, password, client: con, userId: null, network: null, isAdmin: false} | |
event.skipFurtherEvents(); | |
let userDb = event.client.userDb; | |
let username = event.username; | |
let password = event.password; | |
if (tokens.isUserToken(event.password)) { | |
// Using a BNC internal token | |
let authUsername = await authToken(event.client, password); | |
if (!authUsername) { | |
await event.client.writeMsg('ERROR', 'Invalid password'); | |
event.client.close(); | |
event.preventDefault(); | |
return; | |
} | |
username = authUsername; | |
} else { | |
// Using a normal user/password. Auth with anope | |
let authSuccess = await authExternal(event.username, event.password); | |
if (!authSuccess) { | |
await event.client.writeMsg('ERROR', 'Invalid password'); | |
event.client.close(); | |
event.preventDefault(); | |
return; | |
} | |
} | |
let user = await userDb.getUser(username); | |
if (!user) { | |
user = await userDb.addUser(username, password); | |
await user.save(); | |
await addDefaultNetwork(event.client, user, password); | |
} | |
event.userId = user.id; | |
event.isAdmin = user.admin; | |
} | |
async function authToken(client, token) { | |
let userDb = client.userDb; | |
let user = await userDb.authUserToken(token); | |
if (!user) { | |
return false; | |
} | |
return user.username; | |
} | |
async function authExternal(username, password) { | |
try { | |
let reqOpts = { | |
url: config.auth_api_url, | |
method: 'POST', | |
json: true, | |
form: { | |
username, | |
password, | |
}, | |
}; | |
let resp = await request(reqOpts); | |
// resp === json object | |
// return true is auth is correct | |
// return false if auth is wrong | |
} catch (err) { | |
l.error(err); | |
return false; | |
} | |
} | |
async function addDefaultNetwork(con, user, password) { | |
let network = con.db.factories.Network(); | |
network.user_id = user.id; | |
network.name = config.default_net_name; | |
network.host = config.network_address; | |
network.port = config.network_port || 6667; | |
network.tls = !!config.network_tls; | |
network.nick = user.username; | |
network.username = user.username; | |
network.realname = user.username; | |
network.sasl_account = user.username; | |
network.sasl_pass = password; | |
await network.save(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment