Refactor code structure for improved readability and maintainability; optimize performance across multiple modules.
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
All checks were successful
Deploy to production / deploy (push) Successful in 2m56s
This commit is contained in:
16
backend/services/oauthService.js
Normal file → Executable file
16
backend/services/oauthService.js
Normal file → Executable file
@@ -17,6 +17,7 @@ import { encrypt } from '../utils/encryption.js';
|
||||
const saltRounds = 10;
|
||||
const OAUTH_STATE_TTL_SECONDS = 15 * 60;
|
||||
const OAUTH_CALLBACK_PATH = '/auth/oauth/callback';
|
||||
const NATIVE_OAUTH_CALLBACK_PATH = '/android/oauth/callback';
|
||||
|
||||
const STATIC_PROVIDER_DEFS = [
|
||||
{
|
||||
@@ -98,6 +99,15 @@ const getFrontendCallbackUrl = () => {
|
||||
return new URL(OAUTH_CALLBACK_PATH, `${frontendUrl.replace(/\/$/, '')}/`).toString();
|
||||
};
|
||||
|
||||
const getNativeCallbackUrl = () => {
|
||||
const configuredUrl = process.env.OAUTH_ANDROID_CALLBACK_URL || 'https://www.your-part.de/android/oauth/callback';
|
||||
const callbackUrl = new URL(configuredUrl);
|
||||
if (callbackUrl.protocol !== 'https:' || callbackUrl.pathname !== NATIVE_OAUTH_CALLBACK_PATH) {
|
||||
throw new Error('invalidnativeoauthcallback');
|
||||
}
|
||||
return callbackUrl.toString();
|
||||
};
|
||||
|
||||
const normalizeClaims = (claims = {}) => ({
|
||||
subject: claims.sub || claims.subject || '',
|
||||
email: typeof claims.email === 'string' ? claims.email.trim().toLowerCase() : null,
|
||||
@@ -332,7 +342,7 @@ const linkIdentityToUser = async (user, providerSlug, providerConfiguration, cla
|
||||
|
||||
export const getOAuthProviders = async () => getProviderDefinitions().filter((provider) => provider.configured);
|
||||
|
||||
export const startOAuthLogin = async ({ providerSlug }) => {
|
||||
export const startOAuthLogin = async ({ providerSlug, client = 'web' }) => {
|
||||
const provider = getProviderDefinition(providerSlug);
|
||||
if (!provider || !provider.configured) {
|
||||
throw new Error('providernotconfigured');
|
||||
@@ -342,7 +352,7 @@ export const startOAuthLogin = async ({ providerSlug }) => {
|
||||
const codeVerifier = oidc.randomPKCECodeVerifier();
|
||||
const codeChallenge = await oidc.calculatePKCECodeChallenge(codeVerifier);
|
||||
const state = oidc.randomState();
|
||||
const redirectUri = getFrontendCallbackUrl();
|
||||
const redirectUri = client === 'android' ? getNativeCallbackUrl() : getFrontendCallbackUrl();
|
||||
|
||||
await storeOAuthState(state, {
|
||||
providerSlug,
|
||||
@@ -581,4 +591,4 @@ export const removeOAuthIdentity = async ({ userId, identityId }) => {
|
||||
await identity.destroy();
|
||||
|
||||
return { success: true };
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user