mattermost-mobile/app/init/credentials.ts
Elias Nahum 2f3dfbbbfa
Update dependencies and upgrade to RN 0.76.5 (#8421)
* update dev deps

* partial update dependencies

* update watermelondb

* update rn to 0.76.5, expo to 52.0.18 and others

* upgrade android firebase

* upgrade detox deps

* fix package-lock.json

* update emm and paste-input

* update turbo-log

* update network library

* fix tests

* review feedback

* fix Keyboard blocking signIn button

* Fall back to iphone 14 iOS 17.2 simulator as app crashes on iOS 17.4

* changes in deleteCredentialsForServer is causing a crash

* withOptions x2 clearly is wrong

* re-add cli-platform-apple fix

* fix: RN 0.76.5 issue with bottom sheet disappearing (#8478)

* experiment, using view vs BottomSheetView

* revert previous & try disabling enableDynamicSizing

* revert an unintended removal

---------

Co-authored-by: yasserfaraazkhan <attitude3cena.yf@gmail.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Rahim Rahman <rahim.rahman@mattermost.com>
2025-01-16 07:11:32 -07:00

102 lines
3.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Platform} from 'react-native';
import * as KeyChain from 'react-native-keychain';
import DatabaseManager from '@database/manager';
import {logWarning} from '@utils/log';
import {getIOSAppGroupDetails} from '@utils/mattermost_managed';
export const getAllServerCredentials = async (): Promise<ServerCredential[]> => {
const serverCredentials: ServerCredential[] = [];
let serverUrls: string[];
if (Platform.OS === 'ios') {
serverUrls = await KeyChain.getAllInternetPasswordServers();
} else {
serverUrls = await KeyChain.getAllGenericPasswordServices();
}
for await (const serverUrl of serverUrls) {
const serverCredential = await getServerCredentials(serverUrl);
if (serverCredential) {
serverCredentials.push(serverCredential);
}
}
return serverCredentials;
};
export const getActiveServerUrl = async () => {
let serverUrl = await DatabaseManager.getActiveServerUrl();
if (!serverUrl) {
let serverUrls: string[];
if (Platform.OS === 'ios') {
serverUrls = await KeyChain.getAllInternetPasswordServers();
} else {
serverUrls = await KeyChain.getAllGenericPasswordServices();
}
serverUrl = serverUrls[0];
}
return serverUrl || undefined;
};
export const setServerCredentials = (serverUrl: string, token: string) => {
if (!(serverUrl && token)) {
return;
}
try {
let accessGroup;
if (Platform.OS === 'ios') {
const appGroup = getIOSAppGroupDetails();
accessGroup = appGroup.appGroupIdentifier;
}
const options: KeyChain.SetOptions = {
accessGroup,
securityLevel: KeyChain.SECURITY_LEVEL.SECURE_SOFTWARE,
};
KeyChain.setInternetCredentials(serverUrl, token, token, options);
} catch (e) {
logWarning('could not set credentials', e);
}
};
export const removeServerCredentials = async (serverUrl: string) => {
return KeyChain.resetInternetCredentials(serverUrl);
};
export const removeActiveServerCredentials = async () => {
const serverUrl = await getActiveServerUrl();
if (serverUrl) {
await removeServerCredentials(serverUrl);
}
};
export const getServerCredentials = async (serverUrl: string): Promise<ServerCredential|null> => {
try {
const credentials = await KeyChain.getInternetCredentials(serverUrl);
if (credentials) {
// TODO: Pre-Gekidou we were concatenating the deviceToken and the userId in
// credentials.username so we need to check the length of credentials.username.split(',').
// This check should be removed at some point. https://mattermost.atlassian.net/browse/MM-43483
const parts = credentials.username.split(',');
const userId = parts[parts.length - 1];
const token = credentials.password;
if (token && token !== 'undefined') {
return {serverUrl, userId, token};
}
}
return null;
} catch (e) {
return null;
}
};