more universal oauth key storage

This commit is contained in:
Lauren Liberda 2021-12-13 00:29:26 +01:00
parent 4ee534a297
commit 41aac655fe
No known key found for this signature in database
GPG Key ID: 734C629FD04BD319
2 changed files with 12 additions and 7 deletions

View File

@ -1,11 +1,12 @@
-- Up
CREATE TABLE mastodon_oauth (
CREATE TABLE oauth_keys (
instance TEXT NOT NULL PRIMARY KEY,
provider TEXT NOT NULL,
client_id TEXT NOT NULL,
client_secret TEXT NOT NULL
);
-- Down
DROP TABLE mastodon_oauth;
DROP TABLE oauth_keys;

View File

@ -22,9 +22,13 @@ const config = {
const router = Router();
const mastodonGetOAuthKeys = async (db, instance, dbOnly = false) => {
const existingKeys = await db.get(
SQL`SELECT client_id, client_secret FROM mastodon_oauth WHERE instance = ${instance}`);
const mastodonGetOAuthKeys = async (db, instance) => {
const existingKeys = await db.get(SQL`
SELECT client_id, client_secret
FROM oauth_keys
WHERE instance = ${instance}
AND provider = 'mastodon'
`);
if (existingKeys) {
return existingKeys;
}
@ -43,8 +47,8 @@ const mastodonGetOAuthKeys = async (db, instance, dbOnly = false) => {
}).then(res => res.json());
assert(keys.client_id && keys.client_secret && !keys.error);
db.get(SQL`
INSERT INTO mastodon_oauth (instance, client_id, client_secret)
VALUES (${instance}, ${keys.client_id}, ${keys.client_secret})
INSERT INTO oauth_keys (instance, provider, client_id, client_secret)
VALUES (${instance}, 'mastodon', ${keys.client_id}, ${keys.client_secret})
`);
return keys;
};