Skip to content

Commit

Permalink
fix(oauth2): Allow to specify to show only Google Workspace accounts …
Browse files Browse the repository at this point in the history
…on OAuth2 login screen
  • Loading branch information
andris9 committed Oct 21, 2024
1 parent 2ff26e3 commit a3b2412
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lib/oauth/gmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class GmailOauth {
this.serviceClientEmail = opts.serviceClientEmail;
this.serviceKey = opts.serviceKey;

this.workspaceAccounts = !!opts.workspaceAccounts;

this.clientId = opts.clientId;
this.clientSecret = opts.clientSecret;
this.redirectUrl = opts.redirectUrl;
Expand All @@ -171,6 +173,10 @@ class GmailOauth {
url.searchParams.set('scope', this.scopes.join(' '));
url.searchParams.set('access_type', 'offline');

if (this.workspaceAccounts) {
url.searchParams.set('hd', '*');
}

if (opts.email) {
url.searchParams.set('login_hint', opts.email);
}
Expand Down
11 changes: 10 additions & 1 deletion lib/oauth2-apps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,9 @@ class OAuth2AppsHandler {
let redirectUrl = appData.redirectUrl;
let scopes = formatExtraScopes(appData.extraScopes, appData.baseScopes, GMAIL_SCOPES, appData.skipScopes);

let googleProjectId = appData.projectIdv;
let workspaceAccounts = appData.googleWorkspaceAccounts;

if (!clientId || !clientSecret || !redirectUrl) {
let error = Boom.boomify(new Error('OAuth2 credentials not set up for Gmail'), { statusCode: 400 });
throw error;
Expand All @@ -1076,6 +1079,8 @@ class OAuth2AppsHandler {
clientSecret,
redirectUrl,
scopes,
googleProjectId,
workspaceAccounts,
setFlag: async flag => {
try {
if (appData.legacy) {
Expand All @@ -1095,12 +1100,15 @@ class OAuth2AppsHandler {

case 'gmailService': {
let serviceClient = appData.serviceClient;
let googleProjectId = appData.projectIdv;

let serviceClientEmail = appData.serviceClientEmail;
let serviceKey = appData.serviceKey ? await decrypt(appData.serviceKey) : null;

let scopes = formatExtraScopes(appData.extraScopes, appData.baseScopes, GMAIL_SCOPES, appData.skipScopes);

let googleProjectId = appData.projectIdv;
let workspaceAccounts = appData.googleWorkspaceAccounts;

if (!serviceClient || !serviceKey) {
let error = Boom.boomify(new Error('OAuth2 credentials not set up for Gmail'), { statusCode: 400 });
throw error;
Expand All @@ -1114,6 +1122,7 @@ class OAuth2AppsHandler {
googleProjectId,
serviceClientEmail,
scopes,
workspaceAccounts,
setFlag: async flag => {
try {
if (appData.legacy) {
Expand Down
8 changes: 7 additions & 1 deletion lib/routes-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ const {
oauthCreateSchema,
accountIdSchema,
defaultAccountTypeSchema,
googleProjectIdSchema
googleProjectIdSchema,
googleWorkspaceAccountsSchema
} = require('./schemas');
const fs = require('fs');
const pathlib = require('path');
Expand Down Expand Up @@ -421,6 +422,11 @@ const oauthUpdateSchema = {

googleProjectId: googleProjectIdSchema,

googleWorkspaceAccounts: googleWorkspaceAccountsSchema.when('provider', {
is: 'gmail',
then: Joi.optional().default(false)
}),

serviceClientEmail: Joi.string()
.trim()
.allow('')
Expand Down
9 changes: 8 additions & 1 deletion lib/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,11 @@ const accountSchemas = {
};

const googleProjectIdSchema = Joi.string().trim().allow('', false, null).max(256).example('project-name-425411').description('Google Cloud Project ID');
const googleWorkspaceAccountsSchema = Joi.boolean()
.truthy('Y', 'true', '1', 'on')
.falsy('N', 'false', 0, '')
.example(false)
.description('Show only Google Workspace accounts on the OAuth2 login page');

const oauthCreateSchema = {
name: Joi.string().trim().empty('').max(256).example('My Gmail App').required().description('Application name'),
Expand Down Expand Up @@ -1298,6 +1303,7 @@ const oauthCreateSchema = {
.description('Service client ID for 2-legged OAuth2 applications'),

googleProjectId: googleProjectIdSchema,
googleWorkspaceAccounts: googleWorkspaceAccountsSchema,

serviceClientEmail: Joi.string()
.trim()
Expand Down Expand Up @@ -1506,7 +1512,8 @@ module.exports = {
defaultAccountTypeSchema,
fromAddressSchema,
outboxEntrySchema,
googleProjectIdSchema
googleProjectIdSchema,
googleWorkspaceAccountsSchema
};

/*
Expand Down
17 changes: 17 additions & 0 deletions views/partials/oauth_form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@
</div>
{{/unless}}


{{#if activeGmail}}
<div class="form-group form-check">

<input type="checkbox" class="form-check-input {{#if errors.googleWorkspaceAccounts}}is-invalid{{/if}}"
id="googleWorkspaceAccounts" name="googleWorkspaceAccounts" {{#if
values.googleWorkspaceAccounts}}checked{{/if}} />
<label class="form-check-label" for="googleWorkspaceAccounts">Show only Google Workspace accounts on the
OAuth2 login page</label>
{{#if errors.googleWorkspaceAccounts}}
<span class="invalid-feedback">{{errors.googleWorkspaceAccounts}}</span>
{{/if}}
<small class="form-text text-muted">When enabled, only Google Workspace accounts will be available on the
Gmail OAuth2 login screen.</small>
</div>
{{/if}}

{{#unless activeGmailService}}
<div class="form-group form-check">

Expand Down
6 changes: 5 additions & 1 deletion workers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ const {
defaultAccountTypeSchema,
fromAddressSchema,
outboxEntrySchema,
googleProjectIdSchema
googleProjectIdSchema,
googleWorkspaceAccountsSchema
} = require('../lib/schemas');

const listMessageFolderPathDescription =
Expand Down Expand Up @@ -6852,6 +6853,7 @@ const init = async () => {
serviceClient: Joi.string().example('9103965568215821627203').description('Service client ID for 2-legged OAuth2 applications'),

googleProjectId: googleProjectIdSchema,
googleWorkspaceAccounts: googleWorkspaceAccountsSchema,

serviceClientEmail: Joi.string()
.email()
Expand Down Expand Up @@ -6980,6 +6982,7 @@ const init = async () => {
.description('Redirect URL for 3-legged OAuth2 applications'),

googleProjectId: googleProjectIdSchema,
googleWorkspaceAccounts: googleWorkspaceAccountsSchema,

serviceClientEmail: Joi.string()
.email()
Expand Down Expand Up @@ -7154,6 +7157,7 @@ const init = async () => {
.description('Service client ID for 2-legged OAuth2 applications'),

googleProjectId: googleProjectIdSchema,
googleWorkspaceAccounts: googleWorkspaceAccountsSchema,

serviceClientEmail: Joi.string()
.email()
Expand Down

0 comments on commit a3b2412

Please sign in to comment.