2023-09-28 18:54:38 -05:00
const db = require ( "better-sqlite3" ) ( "brainz-social.db" ) ;
db . pragma ( "journal_mode = WAL" ) ;
2023-09-27 07:03:36 -05:00
2023-09-28 18:54:38 -05:00
module . exports = {
2023-09-27 07:03:36 -05:00
db ,
getConfig : ( key ) => {
2023-09-28 18:54:38 -05:00
const row = db . prepare ( "SELECT * FROM config WHERE key = ?" ) . get ( key ) ;
2023-10-04 20:54:45 -05:00
return row ? row . value : null ;
2023-09-27 07:03:36 -05:00
} ,
setConfig : ( key , value ) => {
db . prepare ( "INSERT OR REPLACE INTO config (key, value) VALUES(?, ?);" ) . run ( key , value ) ;
} ,
createApplication : ( client _name , redirect _uri , scopes , website , client _id , client _secret ) => {
db . prepare ( "INSERT INTO applications (client_name, redirect_uri, scopes, website, client_id, client_secret) VALUES (?, ?, ?, ?, ?, ?);" ) . run ( client _name , redirect _uri , scopes , website , client _id , client _secret ) ;
2023-09-27 08:30:39 -05:00
} ,
getApplication : ( client _id ) => {
return db . prepare ( "SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE client_id = ?" ) . get ( client _id ) ;
} ,
2023-09-27 17:31:59 -05:00
getApplicationById : ( id ) => {
return db . prepare ( "SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE id = ?" ) . get ( id ) ;
} ,
2023-09-27 08:30:39 -05:00
createToken : ( token , scope , application _id , user _id , created _at ) => {
2023-10-05 20:16:00 -05:00
db . prepare ( "INSERT INTO oauth_tokens (token, scopes, application_id, user_id, created_at, revoked) VALUES (?, ?, ?, ?, ?, false)" ) . run ( token , scope , application _id , user _id , created _at ) ;
2023-09-27 17:31:59 -05:00
} ,
getTokenData : ( token ) => {
return db . prepare ( "SELECT application_id, user_id, created_at, revoked FROM oauth_tokens WHERE token = ?" ) . get ( token ) ;
2023-09-27 19:29:55 -05:00
} ,
revokeToken : ( token ) => {
db . prepare ( "UPDATE oauth_tokens SET revoked = true WHERE token = ?" ) . run ( token ) ;
2023-09-28 18:36:35 -05:00
} ,
getAccountByUsername : ( username ) => {
return db . prepare ( "SELECT id, username, email, password_hash, account_tier FROM accounts WHERE username = ?" ) . get ( username ) ;
} ,
createAccount : ( username , email , password _hash ) => {
db . prepare ( "INSERT INTO accounts (username, email, password_hash, account_tier) VALUES (?, ?, ?, 0)" ) . run ( username , email , password _hash ) ;
2023-09-30 16:46:07 -05:00
} ,
checkAuthCookie : ( cookie _value ) => {
return db . prepare ( "SELECT id, cookie_value, created_at, user_id, revoked FROM cookies WHERE cookie_value = ?" ) . get ( cookie _value ) ;
} ,
revokeAuthCookie : ( cookie _value ) => {
db . prepare ( "UPDATE cookies SET revoked = true WHERE cookie_value = ?" ) . run ( cookie _value ) ;
} ,
createAuthCookie : ( cookie _value , created _at , user _id ) => {
db . prepare ( "INSERT INTO cookies (cookie_value, created_at, user_id, revoked) VALUES (?, ?, ?, false)" ) . run ( cookie _value , created _at , user _id ) ;
2023-10-01 16:02:20 -05:00
} ,
2023-10-01 19:45:45 -05:00
createCsrfToken : ( url , created _at , cookie _value ) => {
const db _row _id = db . prepare ( "INSERT INTO csrf_token (url, created_at, cookie_value) VALUES (?, ?, ?)" ) . run ( url , created _at , cookie _value ) . lastInsertRowid ;
2023-10-01 16:02:20 -05:00
return db . prepare ( "SELECT id FROM csrf_token WHERE rowid = ?" ) . get ( db _row _id ) ;
} ,
createCsrfTokenAssociation : ( ... ids ) => {
for ( const source _id in ids ) {
for ( const destination _id in ids ) {
db . prepare ( "INSERT INTO csrf_token_relations (source_id, destination_id) VALUES (?, ?)" ) . run ( source _id , destination _id ) ;
}
}
} ,
removeAssociatedCsrfTokens : ( id ) => {
db . prepare ( "DELETE FROM csrf_token WHERE id IN (SELECT destination_id AS id FROM csrf_token_relations WHERE source_id = ?)" ) . run ( id ) ;
} ,
getCsrfToken : ( url ) => {
2023-10-01 19:45:45 -05:00
return db . prepare ( "SELECT id, url, created_at, cookie_value FROM csrf_token WHERE url = ?" ) . get ( url ) ;
2023-10-02 19:38:21 -05:00
} ,
createOauthCode : ( code , application _id , user _id , scopes , created _at ) => {
2023-10-04 00:42:53 -05:00
db . prepare ( "INSERT INTO oauth_code (code, application_id, user_id, scopes, created_at, revoked) VALUES (?, ?, ?, ?, ?, false)" ) . run ( code , application _id , user _id , scopes , created _at ) ;
} ,
getOauthCode : ( code ) => {
return db . prepare ( "SELECT code, application_id, user_id, scopes, created_at, revoked FROM oauth_code WHERE code = ?" ) . get ( code ) ;
} ,
revokeOauthCode : ( code ) => {
db . prepare ( "UPDATE oauth_code SET revoked = true WHERE code = ?" ) . run ( code ) ;
2023-10-05 23:07:56 -05:00
} ,
selectApplicationByAuthToken : ( token ) => {
return db . prepare ( "SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE id in (SELECT application_id as id FROM oauth_tokens WHERE token = ?);" ) . get ( token ) ;
} ,
getVapidKey : ( ) => {
2023-10-06 22:00:20 -05:00
const vapidPublic = db . prepare ( "SELECT value FROM config WHERE key = vapid_key_public" ) . get ( ) ;
const vapidPrivate = db . prepare ( "SELECT value FROM config WHERE key = vapid_key_private" ) . get ( ) ;
if ( vapidPublic . value && vapidPrivate . value ) {
return { public : vapidPublic , private : vapidPrivate } ;
2023-10-05 23:07:56 -05:00
} else {
return null ;
}
} ,
setVapidKey : ( publicKey , privateKey ) => {
db . prepare ( "INSERT INTO config (key, value) VALUES (vapid_key_public, ?)" ) . run ( publicKey ) ;
db . prepare ( "INSERT INTO config (key, value) VALUES (vapid_key_private, ?)" ) . run ( privateKey ) ;
2023-09-27 07:03:36 -05:00
}
} ;