Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/custom-oauth/server/custom_oauth_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class CustomOAuth {
this.identityPath = options.identityPath;
this.tokenSentVia = options.tokenSentVia;
this.identityTokenSentVia = options.identityTokenSentVia;
this.keyField = options.keyField;
this.usernameField = (options.usernameField || '').trim();
this.emailField = (options.emailField || '').trim();
this.nameField = (options.nameField || '').trim();
Expand Down Expand Up @@ -334,7 +335,14 @@ export class CustomOAuth {
}

if (serviceData.username) {
const user = Users.findOneByUsernameAndServiceNameIgnoringCase(serviceData.username, serviceData._id, serviceName);
let user = undefined;

if (this.keyField === 'username') {
user = Users.findOneByUsernameAndServiceNameIgnoringCase(serviceData.username, serviceData._id, serviceName);
} else if (this.keyField === 'email') {
user = Users.findOneByEmailAddressAndServiceNameIgnoringCase(serviceData.email, serviceData._id, serviceName);
}

if (!user) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions app/lib/server/functions/addOAuthService.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/lib/server/methods/removeOAuthService.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Meteor.methods({
settings.removeById(`Accounts_OAuth_Custom-${ name }-button_label_color`);
settings.removeById(`Accounts_OAuth_Custom-${ name }-button_color`);
settings.removeById(`Accounts_OAuth_Custom-${ name }-login_style`);
settings.removeById(`Accounts_OAuth_Custom-${ name }-key_field`);
settings.removeById(`Accounts_OAuth_Custom-${ name }-username_field`);
settings.removeById(`Accounts_OAuth_Custom-${ name }-email_field`);
settings.removeById(`Accounts_OAuth_Custom-${ name }-name_field`);
Expand Down
3 changes: 3 additions & 0 deletions app/lib/server/startup/oAuthServicesUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ function _OAuthServicesUpdate() {
data.buttonColor = settings.get(`${ service.key }-button_color`);
data.tokenSentVia = settings.get(`${ service.key }-token_sent_via`);
data.identityTokenSentVia = settings.get(`${ service.key }-identity_token_sent_via`);
data.keyField = settings.get(`${ service.key }-key_field`);
data.usernameField = settings.get(`${ service.key }-username_field`);
data.emailField = settings.get(`${ service.key }-email_field`);
data.nameField = settings.get(`${ service.key }-name_field`);
Expand All @@ -71,6 +72,7 @@ function _OAuthServicesUpdate() {
loginStyle: data.loginStyle,
tokenSentVia: data.tokenSentVia,
identityTokenSentVia: data.identityTokenSentVia,
keyField: data.keyField,
usernameField: data.usernameField,
emailField: data.emailField,
nameField: data.nameField,
Expand Down Expand Up @@ -177,6 +179,7 @@ function customOAuthServicesInit() {
buttonColor: process.env[`${ serviceKey }_button_color`],
tokenSentVia: process.env[`${ serviceKey }_token_sent_via`],
identityTokenSentVia: process.env[`${ serviceKey }_identity_token_sent_via`],
keyField: process.env[`${ serviceKey }_key_field`],
usernameField: process.env[`${ serviceKey }_username_field`],
nameField: process.env[`${ serviceKey }_name_field`],
emailField: process.env[`${ serviceKey }_email_field`],
Expand Down
9 changes: 9 additions & 0 deletions app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,15 @@ export class Users extends Base {
return this.findOne(query, options);
}

findOneByEmailAddressAndServiceNameIgnoringCase(emailAddress, userId, serviceName, options) {
const query = {
'emails.address': String(emailAddress).trim().toLowerCase(),
[`services.${ serviceName }.id`]: userId,
};

return this.findOne(query, options);
}

findOneByUsername(username, options) {
const query = { username };

Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"Accounts_OAuth_Custom_id": "Id",
"Accounts_OAuth_Custom_Identity_Path": "Identity Path",
"Accounts_OAuth_Custom_Identity_Token_Sent_Via": "Identity Token Sent Via",
"Accounts_OAuth_Custom_Key_Field": "Key Field",
"Accounts_OAuth_Custom_Login_Style": "Login Style",
"Accounts_OAuth_Custom_Map_Channels": "Map Roles/Groups to channels",
"Accounts_OAuth_Custom_Merge_Roles": "Merge Roles from SSO",
Expand Down
1 change: 1 addition & 0 deletions server/startup/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,5 @@ import './v212';
import './v213';
import './v214';
import './v215';
import './v216';
import './xrun';
42 changes: 42 additions & 0 deletions server/startup/migrations/v216.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Migrations } from '../../../app/migrations';
import { Settings } from '../../../app/models';

Migrations.add({
version: 216,
up() {
Settings.find({ _id: /Accounts_OAuth_Custom/, i18nLabel: 'Accounts_OAuth_Custom_Enable' }).forEach(function(customOauth) {
const parts = customOauth._id.split('-');
const name = parts[1];
const id = `Accounts_OAuth_Custom-${ name }-key_field`;
if (!Settings.findOne({ _id: id })) {
Settings.insert({
_id: id,
type: 'select',
group: 'OAuth',
section: `Custom OAuth: ${ name }`,
i18nLabel: 'Accounts_OAuth_Custom_Key_Field',
persistent: true,
values: [
{
key: 'username',
i18nLabel: 'Username',
},
{
key: 'email',
i18nLabel: 'Email',
},
],
packageValue: 'username',
valueSource: 'packageValue',
ts: new Date(),
hidden: false,
blocked: false,
sorter: 103,
i18nDescription: `Accounts_OAuth_Custom-${ name }-key_field_Description`,
createdAt: new Date(),
value: 'username',
});
}
});
},
});