captcha-igid
Version:
Authorization service for an intergalactic session
167 lines (145 loc) • 5.25 kB
JavaScript
const { DBClass } = require('copious-transitions')
async function run_persistence() { // describe the entry point to super storage
}
async function dropConnections() {
}
//
const apiKeys = require(process.cwd() + '/local/api_keys')
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// store_cache -- in DBClass --> set_key_value --> (M)g_keyValueDB. ..._LRUManager
class CaptchaIgidDBClass extends DBClass {
//
//
constructor() {
//
super(false,false,false,false)
//
this.derivation_keys = {}
this.user_persitence
//
}
initialize(conf) {
//
//
let xxHash32Class = false
let seeder = 0
if ( conf.xxhash === "cmake" ) {
const {XXHash32} = require('xxhash32-node-cmake')
xxHash32Class = XXHash32
} else {
const { XXHash32 } = require('xxhash-addon');
xxHash32Class = XXHash32
if ( apiKeys && apiKeys.hash_seed && Array.isArray(apiKeys.hash_seed) && (apiKeys.hash_seed.length === 4) ) {
seeder = Buffer.from(apiKeys.hash_seed)
} else {
seeder = Buffer.from([0, 0, 0, 0])
}
}
//
let seed = (apiKeys && apiKeys.hash_seed) ? apiKeys.hash_seed : seeder // Math.floor(Math.random()*10000) + 17
let hasher = new xxHash32Class(seed)
//
super.initialize(conf)
this.set_hasher(hasher)
this.domain = conf.domain
}
// // FileAndRelays will use the MessageRelayer from global_persistence to send the user data to the backend...
// // apiKeys = require(process.cwd() + '/local/api_keys') configures it...
// // The relayer may talk to a relay service or an endpoint.... (for captcha, it will be user endpoint...)
// // the end points are categorical handlers that are tied to message pathways... in this case a 'user' pathway..
// // (see path_handlers.js)
// // //
async store_user(fdata,all) {
if ( G_users_trns.tagged('user') ) {
let [udata,axioms] = G_users_trns.update(fdata) // custom user storage (seconday service) clean up fields
//
let key_key = G_users_trns.kv_store_key() // application key for key-value store from data object
let key = udata[key_key]
if ( all ) {
let ucwid = fdata.ucwid
this.derivation_keys[ucwid] = axioms
}
// store the user in just the LRU (key value db)
await this.store_cache(key,udata,G_users_trns.back_ref()); // this app will use cache to echo persitent storage
//return relationship_id
if ( this.pdb ) {
this.pdb.set_on_path(udata,'admin-users')
}
}
}
// // //
async fetch_user(fdata,all) {
if ( G_users_trns.from_cache() ) {
let udata = await this.fetch_user_from_key_value_store(fdata[G_users_trns.kv_store_key()])
if ( udata ) {
if ( all && this.derivation_keys[udata.ucwid] ) {
let axioms = this.derivation_keys[udata.ucwid]
udata.public_derivation = axioms.public_derivation
udata.signer_public_key = axioms.signer_public_key
}
return(udata)
}
}
return(false) // never saw this user
}
// exists
// // a bit more severe than fetch... will fail by default when going to persistent storage
async exists(collection,post_body) {
let query = post_body
if ( G_users_trns.tagged(collection) ) {
if ( G_users_trns.from_cache() ) {
// try to find the user in value storage (local LRU or in-house horizontally scaled LRU)
let udata = await this.fetch_user_from_key_value_store(post_body[G_users_trns.kv_store_key()])
if ( udata ) {
return(true)
}
}
query = G_users_trns.existence_query(post_body)
}
// failing a local lookup go to the persistant big database in the sky or nothing
return(await super.exists(collection,query))
}
// //
async update_user(udata) {
//
let key_key = G_users_trns.kv_store_key()
let key = udata[key_key]
//
this.store_cache(key,udata,G_users_trns.back_ref()); // this app will use cache to echo persitent storage
await super.update_user(udata,key_key) // use persitent storage (change the remotely stored disk record + local cache (static))
}
// publish(topic,path,message)
store(transition,data) {
if ( G_contact_trns.tagged(transition) ) {
data.conversation = this.domain
// persistence subsystem adds _tracking
if ( this.pdb ) {
this.pdb.publish('publish-contact','admin-contacts',data)
}
}
}
drop(transition,data) {
if ( G_contact_trns.tagged(transition) ) {
data.conversation = this.domain
// the object tracking should be known at this point
if ( this.pdb ) {
this.pdb.publish('delete-contact','admin-contacts',data)
}
}
}
//
disconnect() {
return new Promise((resolve,reject) => {
g_persistence.client_going_down()
if ( this.key_value_db && this.key_value_db.disconnect(true) ) {
resolve(true)
} else {
reject(false)
}
})
}
}
//
//
module.exports = new CaptchaIgidDBClass()