dehydrate login methods from rocketchat.js
This commit is contained in:
parent
ff44417b32
commit
1afc794028
|
@ -234,7 +234,7 @@ class LoginServices extends React.PureComponent<ILoginServicesProps, any> {
|
||||||
AppleAuthentication.AppleAuthenticationScope.EMAIL
|
AppleAuthentication.AppleAuthenticationScope.EMAIL
|
||||||
]
|
]
|
||||||
});
|
});
|
||||||
|
// @ts-ignore
|
||||||
await RocketChat.loginOAuthOrSso({ fullName, email, identityToken });
|
await RocketChat.loginOAuthOrSso({ fullName, email, identityToken });
|
||||||
} catch {
|
} catch {
|
||||||
logEvent(events.ENTER_WITH_APPLE_F);
|
logEvent(events.ENTER_WITH_APPLE_F);
|
||||||
|
|
|
@ -49,6 +49,7 @@ import { sanitizeLikeString } from '../../lib/database/utils';
|
||||||
import { CustomIcon } from '../../lib/Icons';
|
import { CustomIcon } from '../../lib/Icons';
|
||||||
import { IMessage } from '../../definitions/IMessage';
|
import { IMessage } from '../../definitions/IMessage';
|
||||||
import { forceJpgExtension } from './forceJpgExtension';
|
import { forceJpgExtension } from './forceJpgExtension';
|
||||||
|
import { IUser } from '../../definitions';
|
||||||
|
|
||||||
if (isAndroid) {
|
if (isAndroid) {
|
||||||
require('./EmojiKeyboard');
|
require('./EmojiKeyboard');
|
||||||
|
@ -80,12 +81,7 @@ interface IMessageBoxProps {
|
||||||
editing: boolean;
|
editing: boolean;
|
||||||
threadsEnabled: boolean;
|
threadsEnabled: boolean;
|
||||||
isFocused(): boolean;
|
isFocused(): boolean;
|
||||||
user: {
|
user: IUser;
|
||||||
id: string;
|
|
||||||
_id: string;
|
|
||||||
username: string;
|
|
||||||
token: string;
|
|
||||||
};
|
|
||||||
roomType: string;
|
roomType: string;
|
||||||
tmid: string;
|
tmid: string;
|
||||||
replyWithMention: boolean;
|
replyWithMention: boolean;
|
||||||
|
|
|
@ -1,28 +1,29 @@
|
||||||
import Model from '@nozbe/watermelondb/Model';
|
import Model from '@nozbe/watermelondb/Model';
|
||||||
|
|
||||||
|
import { IUserEmail } from './IUser';
|
||||||
|
import { UserStatus } from './UserStatus';
|
||||||
|
|
||||||
export interface ILoggedUser {
|
export interface ILoggedUser {
|
||||||
id: string;
|
id: string;
|
||||||
token: string;
|
token: string;
|
||||||
username: string;
|
username: string;
|
||||||
name: string;
|
name: string;
|
||||||
language?: string;
|
language?: string;
|
||||||
status: string;
|
status: UserStatus;
|
||||||
statusText?: string;
|
statusText?: string;
|
||||||
customFields: object;
|
customFields?: {
|
||||||
statusLivechat: string;
|
[key: string]: any;
|
||||||
emails: string[];
|
};
|
||||||
roles: string[];
|
statusLivechat?: string;
|
||||||
|
emails?: IUserEmail[];
|
||||||
|
roles?: string[];
|
||||||
avatarETag?: string;
|
avatarETag?: string;
|
||||||
isFromWebView: boolean;
|
isFromWebView: boolean;
|
||||||
settings?: {
|
showMessageInMainThread: boolean;
|
||||||
preferences: {
|
enableMessageParserEarlyAdoption: boolean;
|
||||||
showMessageInMainThread: boolean;
|
|
||||||
enableMessageParserEarlyAdoption: boolean;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ILoginResult {
|
export interface ILoginResultFromServer {
|
||||||
status: string;
|
status: string;
|
||||||
authToken: string;
|
authToken: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
|
|
|
@ -4,6 +4,7 @@ type TRocketChat = typeof rocketchat;
|
||||||
|
|
||||||
export interface IRocketChat extends TRocketChat {
|
export interface IRocketChat extends TRocketChat {
|
||||||
sdk: any;
|
sdk: any;
|
||||||
|
shareSDK: any;
|
||||||
activeUsersSubTimeout: any;
|
activeUsersSubTimeout: any;
|
||||||
roomsSub: any;
|
roomsSub: any;
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,6 +65,7 @@ import getUserInfo from './services/getUserInfo';
|
||||||
// Services
|
// Services
|
||||||
import sdk from './services/sdk';
|
import sdk from './services/sdk';
|
||||||
import toggleFavorite from './services/toggleFavorite';
|
import toggleFavorite from './services/toggleFavorite';
|
||||||
|
import { login, loginTOTP, loginWithPassword, loginOAuthOrSso, getLoginServices, _determineAuthType } from './services/connect';
|
||||||
import * as restAPis from './services/restApi';
|
import * as restAPis from './services/restApi';
|
||||||
|
|
||||||
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
const TOKEN_KEY = 'reactnativemeteor_usertoken';
|
||||||
|
@ -487,103 +488,10 @@ const RocketChat = {
|
||||||
return this.methodCallWrapper('e2e.resetOwnE2EKey');
|
return this.methodCallWrapper('e2e.resetOwnE2EKey');
|
||||||
},
|
},
|
||||||
|
|
||||||
loginTOTP(params, loginEmailPassword, isFromWebView = false) {
|
loginTOTP,
|
||||||
return new Promise(async (resolve, reject) => {
|
loginWithPassword,
|
||||||
try {
|
loginOAuthOrSso,
|
||||||
const result = await this.login(params, isFromWebView);
|
login,
|
||||||
return resolve(result);
|
|
||||||
} catch (e) {
|
|
||||||
if (e.data?.error && (e.data.error === 'totp-required' || e.data.error === 'totp-invalid')) {
|
|
||||||
const { details } = e.data;
|
|
||||||
try {
|
|
||||||
const code = await twoFactor({ method: details?.method || 'totp', invalid: details?.error === 'totp-invalid' });
|
|
||||||
|
|
||||||
if (loginEmailPassword) {
|
|
||||||
reduxStore.dispatch(setUser({ username: params.user || params.username }));
|
|
||||||
|
|
||||||
// Force normalized params for 2FA starting RC 3.9.0.
|
|
||||||
const serverVersion = reduxStore.getState().server.version;
|
|
||||||
if (compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '3.9.0')) {
|
|
||||||
const user = params.user ?? params.username;
|
|
||||||
const password = params.password ?? params.ldapPass ?? params.crowdPassword;
|
|
||||||
params = { user, password };
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(this.loginTOTP({ ...params, code: code?.twoFactorCode }, loginEmailPassword));
|
|
||||||
}
|
|
||||||
|
|
||||||
return resolve(
|
|
||||||
this.loginTOTP({
|
|
||||||
totp: {
|
|
||||||
login: {
|
|
||||||
...params
|
|
||||||
},
|
|
||||||
code: code?.twoFactorCode
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
} catch {
|
|
||||||
// twoFactor was canceled
|
|
||||||
return reject();
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
loginWithPassword({ user, password }) {
|
|
||||||
let params = { user, password };
|
|
||||||
const state = reduxStore.getState();
|
|
||||||
|
|
||||||
if (state.settings.LDAP_Enable) {
|
|
||||||
params = {
|
|
||||||
username: user,
|
|
||||||
ldapPass: password,
|
|
||||||
ldap: true,
|
|
||||||
ldapOptions: {}
|
|
||||||
};
|
|
||||||
} else if (state.settings.CROWD_Enable) {
|
|
||||||
params = {
|
|
||||||
username: user,
|
|
||||||
crowdPassword: password,
|
|
||||||
crowd: true
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.loginTOTP(params, true);
|
|
||||||
},
|
|
||||||
|
|
||||||
async loginOAuthOrSso(params, isFromWebView = true) {
|
|
||||||
const result = await this.loginTOTP(params, false, isFromWebView);
|
|
||||||
reduxStore.dispatch(loginRequest({ resume: result.token }, false, isFromWebView));
|
|
||||||
},
|
|
||||||
|
|
||||||
async login(credentials, isFromWebView = false) {
|
|
||||||
const sdk = this.shareSDK || this.sdk;
|
|
||||||
// RC 0.64.0
|
|
||||||
await sdk.login(credentials);
|
|
||||||
const { result } = sdk.currentLogin;
|
|
||||||
const user = {
|
|
||||||
id: result.userId,
|
|
||||||
token: result.authToken,
|
|
||||||
username: result.me.username,
|
|
||||||
name: result.me.name,
|
|
||||||
language: result.me.language,
|
|
||||||
status: result.me.status,
|
|
||||||
statusText: result.me.statusText,
|
|
||||||
customFields: result.me.customFields,
|
|
||||||
statusLivechat: result.me.statusLivechat,
|
|
||||||
emails: result.me.emails,
|
|
||||||
roles: result.me.roles,
|
|
||||||
avatarETag: result.me.avatarETag,
|
|
||||||
isFromWebView,
|
|
||||||
showMessageInMainThread: result.me.settings?.preferences?.showMessageInMainThread ?? true,
|
|
||||||
enableMessageParserEarlyAdoption: result.me.settings?.preferences?.enableMessageParserEarlyAdoption ?? true
|
|
||||||
};
|
|
||||||
return user;
|
|
||||||
},
|
|
||||||
logout,
|
logout,
|
||||||
logoutOtherLocations() {
|
logoutOtherLocations() {
|
||||||
const { id: userId } = reduxStore.getState().login.user;
|
const { id: userId } = reduxStore.getState().login.user;
|
||||||
|
@ -928,59 +836,8 @@ const RocketChat = {
|
||||||
prefs = { ...prefs, ...param };
|
prefs = { ...prefs, ...param };
|
||||||
return UserPreferences.setMapAsync(SORT_PREFS_KEY, prefs);
|
return UserPreferences.setMapAsync(SORT_PREFS_KEY, prefs);
|
||||||
},
|
},
|
||||||
async getLoginServices(server) {
|
getLoginServices,
|
||||||
try {
|
_determineAuthType,
|
||||||
let loginServices = [];
|
|
||||||
const loginServicesResult = await fetch(`${server}/api/v1/settings.oauth`).then(response => response.json());
|
|
||||||
|
|
||||||
if (loginServicesResult.success && loginServicesResult.services) {
|
|
||||||
const { services } = loginServicesResult;
|
|
||||||
loginServices = services;
|
|
||||||
|
|
||||||
const loginServicesReducer = loginServices.reduce((ret, item) => {
|
|
||||||
const name = item.name || item.buttonLabelText || item.service;
|
|
||||||
const authType = this._determineAuthType(item);
|
|
||||||
|
|
||||||
if (authType !== 'not_supported') {
|
|
||||||
ret[name] = { ...item, name, authType };
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}, {});
|
|
||||||
reduxStore.dispatch(setLoginServices(loginServicesReducer));
|
|
||||||
} else {
|
|
||||||
reduxStore.dispatch(setLoginServices({}));
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.log(error);
|
|
||||||
reduxStore.dispatch(setLoginServices({}));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_determineAuthType(services) {
|
|
||||||
const { name, custom, showButton = true, service } = services;
|
|
||||||
|
|
||||||
const authName = name || service;
|
|
||||||
|
|
||||||
if (custom && showButton) {
|
|
||||||
return 'oauth_custom';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (service === 'saml') {
|
|
||||||
return 'saml';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (service === 'cas') {
|
|
||||||
return 'cas';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (authName === 'apple' && isIOS) {
|
|
||||||
return 'apple';
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: remove this after other oauth providers are implemented. e.g. Drupal, github_enterprise
|
|
||||||
const availableOAuth = ['facebook', 'github', 'gitlab', 'google', 'linkedin', 'meteor-developer', 'twitter', 'wordpress'];
|
|
||||||
return availableOAuth.includes(authName) ? 'oauth' : 'not_supported';
|
|
||||||
},
|
|
||||||
roomTypeToApiType,
|
roomTypeToApiType,
|
||||||
readThreads(tmid) {
|
readThreads(tmid) {
|
||||||
const serverVersion = reduxStore.getState().server.version;
|
const serverVersion = reduxStore.getState().server.version;
|
||||||
|
|
|
@ -6,18 +6,29 @@ import { selectServerFailure } from '../../../actions/server';
|
||||||
import { twoFactor } from '../../../utils/twoFactor';
|
import { twoFactor } from '../../../utils/twoFactor';
|
||||||
import { compareServerVersion } from '../../utils';
|
import { compareServerVersion } from '../../utils';
|
||||||
import { store } from '../../auxStore';
|
import { store } from '../../auxStore';
|
||||||
import { loginRequest, setUser } from '../../../actions/login';
|
import { loginRequest, setLoginServices, setUser } from '../../../actions/login';
|
||||||
import sdk from './sdk';
|
import sdk from './sdk';
|
||||||
import I18n from '../../../i18n';
|
import I18n from '../../../i18n';
|
||||||
import { MIN_ROCKETCHAT_VERSION } from '../rocketchat';
|
import { MIN_ROCKETCHAT_VERSION } from '../rocketchat';
|
||||||
import { ICredentials, ILoggedUser } from '../../../definitions';
|
import { ICredentials, ILoggedUser, IRocketChat } from '../../../definitions';
|
||||||
|
import { isIOS } from '../../../utils/deviceInfo';
|
||||||
|
|
||||||
async function login(credentials: ICredentials, isFromWebView = false) {
|
interface IServices {
|
||||||
|
[index: string]: string | boolean;
|
||||||
|
name: string;
|
||||||
|
custom: boolean;
|
||||||
|
showButton: boolean;
|
||||||
|
buttonLabelText: string;
|
||||||
|
service: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function login(this: IRocketChat, credentials: ICredentials, isFromWebView = false): Promise<ILoggedUser | undefined> {
|
||||||
|
const sdk = this.shareSDK || this.sdk;
|
||||||
// RC 0.64.0
|
// RC 0.64.0
|
||||||
await sdk.login(credentials);
|
await sdk.login(credentials);
|
||||||
const result = sdk.currentLogin?.result;
|
const result = sdk.currentLogin?.result;
|
||||||
if (result) {
|
if (result) {
|
||||||
const user = {
|
const user: ILoggedUser = {
|
||||||
id: result.userId,
|
id: result.userId,
|
||||||
token: result.authToken,
|
token: result.authToken,
|
||||||
username: result.me.username,
|
username: result.me.username,
|
||||||
|
@ -38,10 +49,15 @@ async function login(credentials: ICredentials, isFromWebView = false) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function loginTOTP(params: ICredentials, loginEmailPassword?: boolean, isFromWebView = false): Promise<ILoggedUser> {
|
function loginTOTP(
|
||||||
|
this: IRocketChat,
|
||||||
|
params: ICredentials,
|
||||||
|
loginEmailPassword?: boolean,
|
||||||
|
isFromWebView = false
|
||||||
|
): Promise<ILoggedUser> {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
const result = await login(params, isFromWebView);
|
const result = await this.login(params, isFromWebView);
|
||||||
if (result) {
|
if (result) {
|
||||||
return resolve(result);
|
return resolve(result);
|
||||||
}
|
}
|
||||||
|
@ -62,11 +78,11 @@ function loginTOTP(params: ICredentials, loginEmailPassword?: boolean, isFromWeb
|
||||||
params = { user, password };
|
params = { user, password };
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(loginTOTP({ ...params, code: code?.twoFactorCode }, loginEmailPassword));
|
return resolve(this.loginTOTP({ ...params, code: code?.twoFactorCode }, loginEmailPassword));
|
||||||
}
|
}
|
||||||
|
|
||||||
return resolve(
|
return resolve(
|
||||||
loginTOTP({
|
this.loginTOTP({
|
||||||
totp: {
|
totp: {
|
||||||
login: {
|
login: {
|
||||||
...params
|
...params
|
||||||
|
@ -86,7 +102,7 @@ function loginTOTP(params: ICredentials, loginEmailPassword?: boolean, isFromWeb
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function loginWithPassword({ user, password }: { user: string; password: string }) {
|
function loginWithPassword(this: IRocketChat, { user, password }: { user: string; password: string }) {
|
||||||
let params: ICredentials = { user, password };
|
let params: ICredentials = { user, password };
|
||||||
const state = store.getState();
|
const state = store.getState();
|
||||||
|
|
||||||
|
@ -105,11 +121,11 @@ function loginWithPassword({ user, password }: { user: string; password: string
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return loginTOTP(params, true);
|
return this.loginTOTP(params, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loginOAuthOrSso(params: ICredentials, isFromWebView = true) {
|
async function loginOAuthOrSso(this: IRocketChat, params: ICredentials, isFromWebView = true) {
|
||||||
const result = await loginTOTP(params, false, isFromWebView);
|
const result = await this.loginTOTP(params, false, isFromWebView);
|
||||||
store.dispatch(loginRequest({ resume: result.token }, false, isFromWebView));
|
store.dispatch(loginRequest({ resume: result.token }, false, isFromWebView));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,6 +209,61 @@ async function getWebsocketInfo({ server }: { server: string }) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getLoginServices(this: IRocketChat, server: string) {
|
||||||
|
try {
|
||||||
|
let loginServices = [];
|
||||||
|
const loginServicesResult = await fetch(`${server}/api/v1/settings.oauth`).then(response => response.json());
|
||||||
|
|
||||||
|
if (loginServicesResult.success && loginServicesResult.services) {
|
||||||
|
const { services } = loginServicesResult;
|
||||||
|
loginServices = services;
|
||||||
|
|
||||||
|
const loginServicesReducer = loginServices.reduce((ret: IServices[], item: IServices) => {
|
||||||
|
const name = item.name || item.buttonLabelText || item.service;
|
||||||
|
const authType = this._determineAuthType(item);
|
||||||
|
|
||||||
|
if (authType !== 'not_supported') {
|
||||||
|
ret[name as unknown as number] = { ...item, name, authType };
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}, {});
|
||||||
|
store.dispatch(setLoginServices(loginServicesReducer));
|
||||||
|
} else {
|
||||||
|
store.dispatch(setLoginServices({}));
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
store.dispatch(setLoginServices({}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function _determineAuthType(services: IServices) {
|
||||||
|
const { name, custom, showButton = true, service } = services;
|
||||||
|
|
||||||
|
const authName = name || service;
|
||||||
|
|
||||||
|
if (custom && showButton) {
|
||||||
|
return 'oauth_custom';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (service === 'saml') {
|
||||||
|
return 'saml';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (service === 'cas') {
|
||||||
|
return 'cas';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (authName === 'apple' && isIOS) {
|
||||||
|
return 'apple';
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: remove this after other oauth providers are implemented. e.g. Drupal, github_enterprise
|
||||||
|
const availableOAuth = ['facebook', 'github', 'gitlab', 'google', 'linkedin', 'meteor-developer', 'twitter', 'wordpress'];
|
||||||
|
return availableOAuth.includes(authName) ? 'oauth' : 'not_supported';
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
login,
|
login,
|
||||||
loginTOTP,
|
loginTOTP,
|
||||||
|
@ -202,5 +273,7 @@ export {
|
||||||
abort,
|
abort,
|
||||||
disconnect,
|
disconnect,
|
||||||
getServerInfo,
|
getServerInfo,
|
||||||
getWebsocketInfo
|
getWebsocketInfo,
|
||||||
|
getLoginServices,
|
||||||
|
_determineAuthType
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,13 +6,13 @@ import { twoFactor } from '../../../utils/twoFactor';
|
||||||
import { useSsl } from '../../../utils/url';
|
import { useSsl } from '../../../utils/url';
|
||||||
import reduxStore from '../../createStore';
|
import reduxStore from '../../createStore';
|
||||||
import { Serialized, MatchPathPattern, OperationParams, PathFor, ResultFor } from '../../../definitions/rest/helpers';
|
import { Serialized, MatchPathPattern, OperationParams, PathFor, ResultFor } from '../../../definitions/rest/helpers';
|
||||||
import { ILoginResult } from '../../../definitions';
|
import { ICredentials, ILoginResultFromServer } from '../../../definitions';
|
||||||
|
|
||||||
class Sdk {
|
class Sdk {
|
||||||
private sdk: typeof Rocketchat;
|
private sdk: typeof Rocketchat;
|
||||||
private code: any;
|
private code: any;
|
||||||
currentLogin: {
|
currentLogin: {
|
||||||
result: ILoginResult;
|
result: ILoginResultFromServer;
|
||||||
} | null = null;
|
} | null = null;
|
||||||
|
|
||||||
// TODO: We need to stop returning the SDK after all methods are dehydrated
|
// TODO: We need to stop returning the SDK after all methods are dehydrated
|
||||||
|
@ -163,8 +163,8 @@ class Sdk {
|
||||||
return this.sdk.onStreamData(...args);
|
return this.sdk.onStreamData(...args);
|
||||||
}
|
}
|
||||||
|
|
||||||
login(...args: any[]) {
|
login(credentials: ICredentials) {
|
||||||
return this.sdk.login(...args);
|
return this.sdk.login(credentials);
|
||||||
}
|
}
|
||||||
|
|
||||||
checkAndReopen() {
|
checkAndReopen() {
|
||||||
|
|
|
@ -102,6 +102,7 @@ class AuthenticationWebView extends React.PureComponent<IAuthenticationWebView,
|
||||||
this.setState({ logging: true });
|
this.setState({ logging: true });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// @ts-ignore
|
||||||
RocketChat.loginOAuthOrSso(params);
|
RocketChat.loginOAuthOrSso(params);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.warn(e);
|
console.warn(e);
|
||||||
|
|
Loading…
Reference in New Issue