publication-igid
Version:
Authorization gateway relying on an auth service for providing user editing interfaces
160 lines (135 loc) • 6.05 kB
JavaScript
//
const { DBClass, CustomPersistenceDB } = require('copious-transitions')
const { XXHash32 } = require('xxhash32-node-cmake')
const { MessageRelayer } = require('message-relay-services')
const ChildProcDBCom = require('./db_com')
//
const apiKeys = require(process.cwd() + '/local/api_keys')
let hasher = new XXHash32(apiKeys.hash_seed)
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
const g_keyValueDB = new ChildProcDBCom("key_value",apiKeys.persistence,apiKeys.message_relays)
// these keys live as long as a session and no longer..
const g_keyValueSessions = new ChildProcDBCom("session_key_value",apiKeys.session,apiKeys.session_relay)
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
const WRITE_OBJECT_MAP_EVERY_INTERVAL = 1000*60*15 // 15 minutes
const WRITE_UNUSED_LARGE_ENTRIES_EVERY_INTERVAL = 1000*60*60 // ones an hour
//
//
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// store_cache -- in DBClass --> set_key_value --> (M)g_keyValueDB. ..._LRUManager
class CaptchaDBClass extends DBClass {
//
//
constructor() {
// pass app messages to the backend
// from the default copious-transitions == CustomPersistenceDB, CustomStaticDB
let stash_interval = WRITE_OBJECT_MAP_EVERY_INTERVAL
// initialize the persistence interface with the same communication as the CategoricalUserManager
// let message_fowarding = new MessageRelayer(apiKeys.users)
let persistenceDB = false // new CustomPersistenceDB(message_fowarding,stash_interval,'user')
// static will provide basic components if not obtained from nxinx
stash_interval = WRITE_UNUSED_LARGE_ENTRIES_EVERY_INTERVAL
let staticDB = new ChildProcDBCom("static",g_keyValueDB.message_fowarding,stash_interval,'user')
//
// The key value DB is an LRU DB (e.g. such as Redis) that takes on large values of less predictable size.
//
super(g_keyValueDB,g_keyValueSessions,persistenceDB,staticDB)
this.derivation_keys = {}
this.publication_relay = new MessageRelayer(apiKeys.uploader_lite)
}
initialize(conf) {
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
}
}
// // //
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))
}
//
disconnect() {
return new Promise((resolve,reject) => {
g_persistence.client_going_down()
if ( g_keyValueDB.disconnect(true) ) {
resolve(true)
} else {
reject(false)
}
})
}
store(transition,data) {
if ( G_publication_asset.tagged(transition) ) {
// persistence subsystem adds _tracking
this.publication_relay.publish('publish-contact','admin-contacts',data)
}
}
drop(transition,data) {
if ( G_publication_asset.tagged(transition) ) {
// the object tracking should be known at this point
this.publication_relay.publish('delete-contact','admin-contacts',data)
}
}
}
//
//
module.exports = new CaptchaDBClass()