Add request and register functions

Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
Fred Boniface 2023-04-04 22:12:38 +01:00
parent 14e0a93d86
commit 330e8e02a0
5 changed files with 34 additions and 31 deletions

View File

@ -1,41 +1,31 @@
const axios = require('axios')
const log = require('../utils/log.utils')
const util = require('../utils/auth.utils')
const db = require('../services/dbAccess.services')
const mail = require('../services/mail.services')
const clean = require('../utils/sanitizer.utils')
const domList= require('../configs/domains.configs')
async function createRegKey(){
async function createRegKey(eml){
const domain = clean.splitDomain(eml) // Obtain mail domain as that is all that needs storing
if (domain in domList.valid) {
const uuid = util.generateKey();
return uuid.token;
db.addRegReq(await uuid, await domain)
mail.sendRegister("mail", "uuid");
return 201
} else {
return 401
}
}
async function regUser(req) {
if (util.checkRequest(req.key) == true) {
apiKey = await db.addUser(hash)
apiKey = await db.addUser(await util.generateKey())
return {status: 201, message: "User added", api_key: apiKey}
} else {
return {status: 401, message: "Unautorised"}
}
}
async function sendToGitea(body) {
let key = process.env.OWL_GIT_ISSUEBOT
let url = process.env.OWL_GIT_APIENDPOINT
let opts = {
headers: {
Authorization: key
}
}
var res = await axios.post(url, body, opts)
// Need to read the output from the POST and pass the result upwards to the client.
if (res.status == 201) {
log.out("issueService.sendToGitea: Issue sent to Gitea", "info")
return {status: res.status,message:"issue created"}
} else {
log.out(`issueService.sendToGitea: Failed to send issue to Gitea: ${res.body}`, "err")
return {status: res.status,message:"issue not created"}
}
}
module.exports = {
regUser,
createRegKey

View File

@ -42,6 +42,15 @@ async function addUser(uuid, domain) {
return res;
}
async function addRegReq(uuid) {
log.out(`dbAccess.addRegReq: Adding registration request`)
let doc = {uuid: uuid, time: new Date}
await client.connect();
let col = db.collection("registrations");
res = col.insertOne(doc);
return res;
}
async function userAtime(uuid) {
log.out(`dbAccess.userAtime: Updating access time for user`)
let q = {uuid: uuid};
@ -56,5 +65,6 @@ module.exports = {
query,
increment,
addUser,
userAtime
userAtime,
addRegReq
}

View File

@ -5,17 +5,14 @@ const db = require('../services/dbAccess.services')
// Checks users registration key against issued keys
async function checkUser(key) {
q = {uuid: key};
res = db.query(q);
res = db.query("registrations", q);
log.out(`authUtils.checkUser: DB Query answer: ${await res}`)
return await res
}
// Creates an API key for a user
async function generateKey() {
const saltRounds = 10
const token = crypto.randomUUID()
const hash = bcrypt.hash(token, saltRounds);
return {user_key: token, hash_key: await hash}
return crypto.randomUUID()
};
module.export = {

View File

@ -38,8 +38,14 @@ function cleanNrcc(input) {
return rmPara;
}
async function splitDomain(mail) {
split = mail.split("@")
return split[1]
}
module.exports = {
cleanApiEndpointTxt,
cleanApiEndpointNum,
cleanNrcc
cleanNrcc,
splitDomain
}