Merge branch 'fix/apple-auth-breaks-dev' into 'main'

Disable Apple authentication if required environment variables are not provided

See merge request PronounsPage/PronounsPage!344
This commit is contained in:
Andrea Vos 2023-06-02 08:25:02 +00:00
commit 5097e6c7a1

View File

@ -17,7 +17,35 @@ const getAppleClientSecret = () => {
});
}
module.exports.config = {
function appleIsEnabled() {
const conditions = [
"APPLE_CLIENT_ID",
"APPLE_TEAM_ID",
"APPLE_KEY_ID",
"APPLE_PRIVATE_KEY"
];
let unavailable = [];
for (const condition of conditions) {
const value = process.env[condition];
// Checks if the value is not a string or empty
if (typeof value !== "string" || value.length < 1 || value.trim().length < 1) {
unavailable.push(condition);
}
}
if (unavailable.length === 0) {
return true;
}
if (unavailable.length === conditions.length) {
console.log("Apple authentication is disabled - provide the APPLE_CLIENT_ID, APPLE_TEAM_ID, and APPLE_PRIVATE_KEY to enable these");
} else {
console.warn(`Apple authentication is disabled because all required environment values were not provided (missing: ${unavailable.join(', ')})`);
}
return false;
}
const enableApple = appleIsEnabled();
const config = {
defaults: {
origin: process.env.BASE_URL,
transport: 'session',
@ -49,7 +77,13 @@ module.exports.config = {
callback: '/api/user/social/discord',
scope: ['identify', 'email'],
},
apple: {
// non-grant, but things break if it's not there
mastodon: {},
indieauth: {},
}
if (enableApple) {
config.apple = {
key: process.env.APPLE_CLIENT_ID,
secret: getAppleClientSecret(),
@ -61,12 +95,11 @@ module.exports.config = {
response_type: 'code id_token',
response_mode: 'form_post',
},
},
// non-grant, but things break if it's not there
mastodon: {},
indieauth: {},
}
}
module.exports.config = config;
module.exports.handlers = {
twitter(r) {
return {