Skip to content

Commit

Permalink
UNTRACKED/k1ch/refactor-persona-db-layer (#117)
Browse files Browse the repository at this point in the history
* refactor: UNTRACKED/k1ch/refactor-persona-db-layer

* release: Bump up version to v2.1.1

* minor: UNTRACKED/k1ch/introduce env var for knex pool
  • Loading branch information
k1ch authored Aug 28, 2024
1 parent 3b5554a commit ef5c6a3
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 49 deletions.
8 changes: 6 additions & 2 deletions database/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ module.exports = {
searchPath: [env.PGSCHEMA, 'public'],
migrations: {
tableName: 'knex_migrations',
schemaName: env.PGSCHEMA
}
schemaName: env.PGSCHEMA,
},
pool: {
min: process.env.KNEX_POOL_MIN || 1,
max: process.env.KNEX_POOL_MAX || 100,
},
}
28 changes: 13 additions & 15 deletions database/layer/admin-persona.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
const { PGPool } = require('./pg_pool')
const pool = new PGPool()
const { usherDb } = require('./knex')
const { pgErrorHandler } = require('../utils/pgErrorHandler')

const insertPersona = async (tenantName, issClaim, subClaim, userContext) => {
const sql = `INSERT INTO usher.personas (tenantkey, sub_claim, user_context)
SELECT key, $3, $4
SELECT key, ?, ?
FROM usher.tenants
WHERE name = $1 AND iss_claim = $2`
const sqlParams = [tenantName, issClaim, subClaim, userContext]
WHERE name = ? AND iss_claim = ?`
const sqlParams = [subClaim, userContext, tenantName, issClaim]
try {
const results = await pool.query(sql, sqlParams)
const results = await usherDb.raw(sql, sqlParams)
if (results.rowCount === 1) {
return 'Insert successful'
} else {
const errTenantDoesNotExist = `Tenant does not exist matching tenantname ${tenantName} iss_claim ${issClaim}`
return `Insert failed: ${errTenantDoesNotExist}`
}
} catch (error) {
if (error.message === 'duplicate key value violates unique constraint "personas_subclaim_userscope_tenantkey_uq"') {
if (error.message.includes('duplicate key value violates unique constraint "personas_subclaim_userscope_tenantkey_uq"')) {
const errPersonaAlreadyExists = `A persona (sub_claim = ${subClaim}; user_context = ${userContext}) already exists on tenantname ${tenantName} iss_claim ${issClaim}`
return `Insert failed: ${errPersonaAlreadyExists}`
}
Expand All @@ -28,11 +26,11 @@ const insertPersona = async (tenantName, issClaim, subClaim, userContext) => {

const deletePersona = async (tenantName, issClaim, subClaim, userContext) => {
const sql = `DELETE FROM usher.personas p
WHERE EXISTS (SELECT 1 FROM usher.tenants t WHERE t.KEY = p.tenantkey AND t.name = $1 and t.iss_claim = $2)
AND p.sub_claim = $3 AND p.user_context = $4`
WHERE EXISTS (SELECT 1 FROM usher.tenants t WHERE t.KEY = p.tenantkey AND t.name = ? and t.iss_claim = ?)
AND p.sub_claim = ? AND p.user_context = ?`
const sqlParams = [tenantName, issClaim, subClaim, userContext]
try {
const deleteResult = await pool.query(sql, sqlParams)
const deleteResult = await usherDb.raw(sql, sqlParams)
if (deleteResult.rowCount === 1) {
return 'Delete successful'
} else {
Expand All @@ -45,12 +43,12 @@ const deletePersona = async (tenantName, issClaim, subClaim, userContext) => {
}

const updatePersona = async (tenantName, issClaim, oldSubClaim, newSubClaim, oldUserContext, newUserContext) => {
const sql = `UPDATE usher.personas p SET sub_claim = $4, user_context = $6
WHERE EXISTS (SELECT 1 FROM usher.tenants t WHERE t.KEY = p.tenantkey AND t.name = $1 and t.iss_claim = $2)
AND p.sub_claim = $3 AND p.user_context = $5`
const sqlParams = [tenantName, issClaim, oldSubClaim, newSubClaim, oldUserContext, newUserContext]
const sql = `UPDATE usher.personas p SET sub_claim = ?, user_context = ?
WHERE EXISTS (SELECT 1 FROM usher.tenants t WHERE t.KEY = p.tenantkey AND t.name = ? and t.iss_claim = ?)
AND p.sub_claim = ? AND p.user_context = ?`
const sqlParams = [newSubClaim, newUserContext, tenantName, issClaim, oldSubClaim, oldUserContext]
try {
const updateResult = await pool.query(sql, sqlParams)
const updateResult = await usherDb.raw(sql, sqlParams)
if (updateResult.rowCount === 1) {
return 'Update successful'
} else {
Expand Down
20 changes: 9 additions & 11 deletions database/layer/admin-personapermission.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { PGPool } = require('./pg_pool')
const pool = new PGPool()
const { usherDb } = require('./knex')
const { pgErrorHandler } = require('../utils/pgErrorHandler')

Expand All @@ -11,20 +9,20 @@ const insertPersonaPermissionByClientId = async (clientId, subClaim, permissionN
INNER JOIN usher.tenantclients tc on (c.key = tc.clientkey)
INNER JOIN usher.tenants t on (t.key = tc.tenantkey)
INNER JOIN usher.personas prs on (prs.tenantkey = t.key)
WHERE c.client_id = $1
AND prs.sub_claim = $2
AND pm.name = $3`
WHERE c.client_id = ?
AND prs.sub_claim = ?
AND pm.name = ?`
const sqlParams = [clientId, subClaim, permissionName]
try {
const results = await pool.query(sql, sqlParams)
const results = await usherDb.raw(sql, sqlParams)
if (results.rowCount === 1) {
return 'Insert successful'
} else {
const errClientPersonaPermissionDoesNotExist = `Either or all of client_id = ${clientId}; persona = ${subClaim}; permission = ${permissionName}; does not exist.`
return `Insert failed: ${errClientPersonaPermissionDoesNotExist}`
}
} catch (error) {
if (error.message === 'duplicate key value violates unique constraint "personapermissions_personakey_permissionkey_uq"') {
if (error.message.includes('duplicate key value violates unique constraint "personapermissions_personakey_permissionkey_uq"')) {
const errClientPersonaPermissionAlreadyExists = `A persona permission client_id = ${clientId}; persona ${subClaim}; is already assigned to permission ${permissionName}.`
return `Insert failed: ${errClientPersonaPermissionAlreadyExists}`
}
Expand All @@ -37,12 +35,12 @@ const deletePersonaPermissionByClientId = async (clientId, subClaim, permissionN
WHERE
EXISTS (SELECT c.key
FROM usher.clients c
JOIN usher.permissions pm ON pm.clientkey = c.key WHERE c.client_id = $1 AND pm.name = $3)
JOIN usher.permissions pm ON pm.clientkey = c.key WHERE c.client_id = ? AND pm.name = ?)
AND
EXISTS (SELECT prs.key FROM usher.personas prs WHERE prs.KEY = pp.personakey AND prs.sub_claim = $2)`
const sqlParams = [clientId, subClaim, permissionName]
EXISTS (SELECT prs.key FROM usher.personas prs WHERE prs.KEY = pp.personakey AND prs.sub_claim = ?)`
const sqlParams = [clientId, permissionName, subClaim]
try {
const results = await pool.query(sql, sqlParams)
const results = await usherDb.raw(sql, sqlParams)
if (results.rowCount === 1) {
return 'Delete successful'
} else {
Expand Down
24 changes: 11 additions & 13 deletions database/layer/admin-personarole.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
const { PGPool } = require('./pg_pool')
const pool = new PGPool()
const { usherDb } = require('./knex')
const { pgErrorHandler } = require('../utils/pgErrorHandler')

const insertPersonaRole = async (tenantName, issClaim, subClaim, userContext, clientId, rolename) => {
const sql = `INSERT INTO usher.personaroles (personakey, rolekey)
SELECT p.KEY, r.KEY
FROM usher.roles r JOIN usher.clients c ON (c.key = r.clientkey) inner join usher.tenantclients tc ON (c.key = tc.clientkey) inner JOIN usher.tenants t ON (t.key = tc.tenantkey) inner join usher.personas p on (p.tenantkey = t.key)
WHERE t.name = $1 AND t.iss_claim = $2 AND p.sub_claim = $3 AND p.user_context = $4
AND c.client_id = $5 AND r.name = $6`
WHERE t.name = ? AND t.iss_claim = ? AND p.sub_claim = ? AND p.user_context = ?
AND c.client_id = ? AND r.name = ?`
const sqlParams = [tenantName, issClaim, subClaim, userContext, clientId, rolename]
try {
const results = await pool.query(sql, sqlParams)
const results = await usherDb.raw(sql, sqlParams)
if (results.rowCount === 1) {
return 'Insert successful'
} else {
const errPersonaRoleDoesNotExist = `Either or all of client_id = ${clientId} & rolename ${rolename}; tenantname = ${tenantName} & iss_claim = ${issClaim} & sub_claim = ${subClaim} & user_context = ${userContext} does not exist`
return `Insert failed: ${errPersonaRoleDoesNotExist}`
}
} catch (error) {
if (error.message === 'duplicate key value violates unique constraint "personaroles_personakey_rolekey_uq"') {
if (error.message.includes('duplicate key value violates unique constraint "personaroles_personakey_rolekey_uq"')) {
const errPersonaRoleAlreadyExists = `A client role client_id = ${clientId} & rolename ${rolename} is already assigned to tenantname = ${tenantName} & iss_claim = ${issClaim} & sub_claim = ${subClaim} & user_context = ${userContext}`
return `Insert failed: ${errPersonaRoleAlreadyExists}`
}
Expand All @@ -36,16 +34,16 @@ const deletePersonaRole = async (tenantName, issClaim, subClaim, userContext, cl
JOIN usher.tenantclients tc ON (c.key = tc.clientkey)
JOIN usher.tenants t ON (t.key = tc.tenantkey)
JOIN usher.personas p ON (p.tenantkey = t.key)
WHERE t.name = $1
AND t.iss_claim = $2
AND p.sub_claim = $3
AND p.user_context = $4
AND c.client_id = $5
AND r.name = $6
WHERE t.name = ?
AND t.iss_claim = ?
AND p.sub_claim = ?
AND p.user_context = ?
AND c.client_id = ?
AND r.name = ?
);`
const sqlParams = [tenantName, issClaim, subClaim, userContext, clientId, rolename]
try {
const results = await pool.query(sql, sqlParams)
const results = await usherDb.raw(sql, sqlParams)
if (results.rowCount === 1) {
return 'Delete successful'
} else {
Expand Down
4 changes: 2 additions & 2 deletions database/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion database/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dmgt-tech/the-usher-server-database",
"version": "2.1.0",
"version": "2.1.1",
"description": "Database layer for TheUsher",
"scripts": {
"test": "mocha --exit",
Expand Down
2 changes: 2 additions & 0 deletions server/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ NODE_ENV=development

PGURI=postgres://postgres:tehsecure@localhost:5432/postgres?sslmode=disable
PGSCHEMA=usher
KNEX_POOL_MIN=1
KNEX_POOL_MAX=100

# TOKEN LIFETIMES
# Duration the access_token is valid:
Expand Down
6 changes: 3 additions & 3 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dmgt-tech/the-usher-server",
"version": "2.1.0",
"version": "2.1.1",
"description": "The Usher Authorization Server",
"engines": {
"node": ">=18"
Expand Down
2 changes: 1 addition & 1 deletion server/the-usher-openapi-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
license:
name: MIT
url: https://opensource.org/licenses/MIT
version: 2.1.0
version: 2.1.1
externalDocs:
description: GitHub Repository
url: https://github.com/DMGT-TECH/the-usher-server
Expand Down

0 comments on commit ef5c6a3

Please sign in to comment.