11const crypto = require ( 'node:crypto' )
2- const { PGPool } = require ( './pg_pool' )
3- const pool = new PGPool ( )
4-
5- function getAdminSessionView ( ) {
6- return `SELECT p.key AS personakey, s.event_id, s.authorization_time, s.scope, s.idp_token
7- FROM usher.tenants t
8- JOIN usher.personas p ON p.tenantkey = t.key
9- JOIN usher.sessions s ON s.personakey = p.key`
10- }
11-
12- function getAdminTenantPersonaView ( ) {
13- return `SELECT p.key as personakey
14- FROM usher.tenants t
15- JOIN usher.personas p ON p.tenantkey = t.key`
16- }
2+ const { usherDb } = require ( './knex' )
173
184async function getSessionPersonaKey ( subClaim , userContext = '' , issClaim ) {
19- const sql = getAdminSessionView ( ) + ' WHERE sub_claim = $1 AND p.user_context = $2 AND iss_claim = $3'
20- const sessionKeyResult = await pool . query ( sql , [ subClaim , userContext , issClaim ] )
21- return ( sessionKeyResult . rows . length === 0 ? null : sessionKeyResult . rows [ 0 ] . personakey )
5+ const results = await usherDb ( 'tenants as t' )
6+ . join ( 'personas as p' , 't.key' , '=' , 'p.tenantkey' )
7+ . join ( 'sessions as s' , 'p.key' , '=' , 's.personakey' )
8+ . select ( 'p.key as personakey' , 's.event_id' , 's.authorization_time' , 's.scope' , 's.idp_token' )
9+ . where ( 'sub_claim' , subClaim )
10+ . where ( 'p.user_context' , userContext )
11+ . where ( 'iss_claim' , issClaim )
12+
13+ return ( results . length === 0 ? null : results [ 0 ] . personakey )
2214}
2315
2416async function getPersonaKey ( subClaim , userContext = '' , issClaim ) {
25- const sql = getAdminTenantPersonaView ( ) + ' WHERE sub_claim = $1 AND p.user_context = $2 AND iss_claim = $3'
26- const personaKeyResult = await pool . query ( sql , [ subClaim , userContext , issClaim ] )
27- return personaKeyResult . rows . length === 0 ? null : personaKeyResult . rows [ 0 ] . personakey
17+ const results = await usherDb ( 'tenants as t' )
18+ . join ( 'personas as p' , 't.key' , '=' , 'p.tenantkey' )
19+ . select ( 'p.key as personakey' )
20+ . where ( 'sub_claim' , subClaim )
21+ . where ( 'p.user_context' , userContext )
22+ . where ( 'iss_claim' , issClaim )
23+
24+ return results . length === 0 ? null : results [ 0 ] . personakey
2825}
2926
27+ /**
28+ * Gets the most recent session record for the given User
29+ * @param {string } subClaim
30+ * @param {string } userContext
31+ * @param {string } issClaim
32+ * @returns An object representing the session record or null if no session exists
33+ */
3034async function getSessionBySubIss ( subClaim , userContext , issClaim ) {
3135 const personaKey = await getSessionPersonaKey ( subClaim , userContext , issClaim )
3236 if ( ! personaKey ) {
3337 return null
3438 }
35- const sql = 'SELECT * FROM usher.sessions WHERE personakey = $1'
36- const sessionRowResult = await pool . query ( sql , [ personaKey ] )
37- return sessionRowResult . rows [ 0 ]
39+ const results = await usherDb ( 'sessions' ) . select ( ) . where ( 'personakey' , personaKey )
40+ . orderBy ( 'authorization_time' , 'desc' )
41+ . first ( )
42+ return results || null // force null return if no results instead of undefined
3843}
3944
45+ /**
46+ * Get a session record by a given session `event_id`
47+ * @param {string } eventId The session event_id to look up
48+ * @returns An object representing the session record
49+ */
4050async function getSessionByEventId ( eventId ) {
41- const sql = 'SELECT * FROM usher.sessions WHERE event_id = $1'
42- const sessionRowResult = await pool . query ( sql , [ eventId ] )
43- return sessionRowResult . rows . length === 0 ? null : sessionRowResult . rows [ 0 ]
51+ const results = await usherDb ( 'sessions' ) . select ( ) . where ( 'event_id' , eventId )
52+ return results . length === 0 ? null : results [ 0 ]
4453}
4554
4655async function insertSessionBySubIss (
@@ -63,10 +72,16 @@ async function insertSessionBySubIss (
6372}
6473
6574async function insertSessionByPersonaKey ( personakey , eventId , authorizationTime , idpExpirationTime , scope , idpToken ) {
66- const sql = `INSERT INTO usher.sessions
67- (personakey, event_id, authorization_time, idp_expirationtime, scope, idp_token)
68- VALUES ($1, $2, $3, $4, $5, $6)`
69- return pool . query ( sql , [ personakey , eventId , authorizationTime , idpExpirationTime , scope , idpToken ] )
75+ const results = await usherDb ( 'sessions' ) . insert ( {
76+ personakey,
77+ event_id : eventId ,
78+ authorization_time : authorizationTime ,
79+ idp_expirationtime : idpExpirationTime ,
80+ scope,
81+ idp_token : idpToken
82+ } )
83+ . returning ( '*' )
84+ return results ?. [ 0 ]
7085}
7186
7287async function updateSessionBySubIss ( subClaim , userContext , issClaim , authorizationTime , idpExpirationTime , scope , idpToken ) {
@@ -75,9 +90,16 @@ async function updateSessionBySubIss (subClaim, userContext, issClaim, authoriza
7590 throw new Error ( `Session does not exist for persona (sub_claim=${ subClaim } user_context = ${ userContext } iss_claim=${ issClaim } )` )
7691 }
7792
78- const sql = 'UPDATE usher.sessions SET authorization_time = $1, idp_expirationtime = $2, scope = $3, idp_token = $4 WHERE personakey = $5'
79- const results = await pool . query ( sql , [ authorizationTime , idpExpirationTime , scope , idpToken , personaKey ] )
80- return results . rows
93+ const [ results ] = await usherDb ( 'sessions' )
94+ . where ( 'personakey' , personaKey )
95+ . update ( {
96+ authorization_time : authorizationTime ,
97+ idp_expirationtime : idpExpirationTime ,
98+ scope,
99+ idp_token : idpToken
100+ } )
101+ . returning ( '*' )
102+ return results
81103}
82104
83105/**
@@ -110,10 +132,9 @@ async function deleteSessionBySubIss (subClaim, userContext, issClaim) {
110132 return deleteReturn
111133}
112134
113- async function deleteSessionByPersonaKey ( personakey ) {
114- const sql = 'DELETE FROM usher.sessions WHERE personakey = $1'
115- const deleteReturn = await pool . query ( sql , [ personakey ] )
116- if ( deleteReturn . rowCount === 1 ) {
135+ async function deleteSessionByPersonaKey ( personaKey ) {
136+ const deleteResults = await usherDb ( 'sessions' ) . where ( 'personakey' , personaKey ) . del ( )
137+ if ( deleteResults === 1 ) {
117138 return 'Delete successful'
118139 } else {
119140 return 'Delete unsuccessful'
0 commit comments