Files
Zero/apps/server/src/lib/auth-providers.ts
Adam 1523b37047 No Microsoft (#1288)
# READ CAREFULLY THEN REMOVE

Remove bullet points that are not relevant.

PLEASE REFRAIN FROM USING AI TO WRITE YOUR CODE AND PR DESCRIPTION. IF YOU DO USE AI TO WRITE YOUR CODE PLEASE PROVIDE A DESCRIPTION AND REVIEW IT CAREFULLY. MAKE SURE YOU UNDERSTAND THE CODE YOU ARE SUBMITTING USING AI.

- Pull requests that do not follow these guidelines will be closed without review or comment.
- If you use AI to write your PR description your pr will be close without review or comment.
- If you are unsure about anything, feel free to ask for clarification.

## Description

Please provide a clear description of your changes.

---

## Type of Change

Please delete options that are not relevant.

- [ ] 🐛 Bug fix (non-breaking change which fixes an issue)
- [ ]  New feature (non-breaking change which adds functionality)
- [ ] 💥 Breaking change (fix or feature with breaking changes)
- [ ] 📝 Documentation update
- [ ] 🎨 UI/UX improvement
- [ ] 🔒 Security enhancement
- [ ]  Performance improvement

## Areas Affected

Please check all that apply:

- [ ] Email Integration (Gmail, IMAP, etc.)
- [ ] User Interface/Experience
- [ ] Authentication/Authorization
- [ ] Data Storage/Management
- [ ] API Endpoints
- [ ] Documentation
- [ ] Testing Infrastructure
- [ ] Development Workflow
- [ ] Deployment/Infrastructure

## Testing Done

Describe the tests you've done:

- [ ] Unit tests added/updated
- [ ] Integration tests added/updated
- [ ] Manual testing performed
- [ ] Cross-browser testing (if UI changes)
- [ ] Mobile responsiveness verified (if UI changes)

## Security Considerations

For changes involving data or authentication:

- [ ] No sensitive data is exposed
- [ ] Authentication checks are in place
- [ ] Input validation is implemented
- [ ] Rate limiting is considered (if applicable)

## Checklist

- [ ] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document
- [ ] My code follows the project's style guidelines
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in complex areas
- [ ] I have updated the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix/feature works
- [ ] All tests pass locally
- [ ] Any dependent changes are merged and published

## Additional Notes

Add any other context about the pull request here.

## Screenshots/Recordings

Add screenshots or recordings here if applicable.

---

_By submitting this pull request, I confirm that my contribution is made under the terms of the project's license._
2025-06-10 23:42:14 -07:00

113 lines
3.4 KiB
TypeScript

export interface EnvVarInfo {
name: string;
source: string;
defaultValue?: string;
}
export interface ProviderConfig {
id: string;
name: string;
requiredEnvVars: string[];
envVarInfo?: EnvVarInfo[];
config: unknown;
required?: boolean;
isCustom?: boolean;
customRedirectPath?: string;
}
export const customProviders: ProviderConfig[] = [
// {
// id: "zero",
// name: "Zero",
// requiredEnvVars: [],
// config: {},
// isCustom: true,
// customRedirectPath: "/zero/signup"
// }
];
export const authProviders = (env: Record<string, string>): ProviderConfig[] => [
{
id: 'google',
name: 'Google',
requiredEnvVars: ['GOOGLE_CLIENT_ID', 'GOOGLE_CLIENT_SECRET'],
envVarInfo: [
{ name: 'GOOGLE_CLIENT_ID', source: 'Google Cloud Console' },
{ name: 'GOOGLE_CLIENT_SECRET', source: 'Google Cloud Console' },
],
config: {
prompt: env.FORCE_GOOGLE_AUTH ? 'consent' : undefined,
accessType: 'offline',
scope: [
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email',
],
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
},
required: true,
},
// {
// id: 'microsoft',
// name: 'Microsoft',
// requiredEnvVars: ['MICROSOFT_CLIENT_ID', 'MICROSOFT_CLIENT_SECRET'],
// envVarInfo: [
// { name: 'MICROSOFT_CLIENT_ID', source: 'Microsoft Azure App ID' },
// { name: 'MICROSOFT_CLIENT_SECRET', source: 'Microsoft Azure App Password' },
// ],
// config: {
// clientId: env.MICROSOFT_CLIENT_ID,
// clientSecret: env.MICROSOFT_CLIENT_SECRET,
// redirectUri: env.MICROSOFT_REDIRECT_URI,
// scope: [
// 'https://graph.microsoft.com/User.Read',
// 'https://graph.microsoft.com/Mail.ReadWrite',
// 'https://graph.microsoft.com/Mail.Send',
// 'offline_access',
// ],
// authority: 'https://login.microsoftonline.com/common',
// responseType: 'code',
// prompt: 'consent',
// loginHint: 'email',
// disableProfilePhoto: true,
// },
// required: false,
// },
];
export function isProviderEnabled(provider: ProviderConfig, env: Record<string, string>): boolean {
if (provider.isCustom) return true;
const hasEnvVars = provider.requiredEnvVars.every((envVar) => !!env[envVar]);
if (provider.required && !hasEnvVars) {
console.error(`Required provider "${provider.id}" is not configured properly.`);
console.error(
`Missing environment variables: ${provider.requiredEnvVars.filter((envVar) => !env[envVar]).join(', ')}`,
);
}
return hasEnvVars;
}
export function getSocialProviders(env: Record<string, string>) {
const socialProviders = Object.fromEntries(
authProviders(env)
.map((provider) => {
if (isProviderEnabled(provider, env)) {
return [provider.id, provider.config] as [string, unknown];
} else if (provider.required) {
throw new Error(
`Required provider "${provider.id}" is not configured properly. Check your environment variables.`,
);
} else {
console.warn(`Provider "${provider.id}" is not configured properly. Skipping.`);
return null;
}
})
.filter((provider) => provider !== null),
);
return socialProviders;
}