publication-igid
Version:
Authorization gateway relying on an auth service for providing user editing interfaces
145 lines (121 loc) • 5 kB
JavaScript
const { DBClass } = require('copious-transitions')
const { MessageRelayer } = require('message-relay-services')
const CategoricalRelays = require('../cat_relays.js')
let g_persistence = false
//
const apiKeys = require(process.cwd() + '/local/api_keys')
// ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
//
// store_cache -- in DBClass --> set_key_value --> (M)g_keyValueDB. ..._LRUManager
class PublicationDBClass extends DBClass {
//
//
constructor() {
g_persistence = new CategoricalRelays()
super(false,false,g_persistence,false)
this.derivation_keys = {}
this.json_relay = false
}
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
}
// override
async establish_admin_endpoints(endpoints) {
let relayer = false
if ( endpoints ) {
relayer = new new MessageRelayer(endpoints)
} else {
endpoints = new MessageRelayer(apiKeys.endpoints)
}
this.json_relay = endpoints
if ( endpoints ) {
await this.json_relay.ready() // make sure this is available in the current verison of message-relay-services
}
}
// // 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))
}
}
//
//
module.exports = new PublicationDBClass()