mattermost-mobile/app/init/credentials.ts
Mattermost Build 49b9ec2868
MM-65085: Support Pre Shared Password on server connect (#9082) (#9096)
* feat: add shared server password to server setup

* feat: allow editing the sever

* refactor: changed password -> secret, styling and tests

* e2e: draft e2e tests

* chore: lint fix

* feat: also send preauth secret header when using native share

* fix: removed unused server database migration

credentials are being stored in the keychain

* i18n: added missing english translations

* test(e2e): simplified connection tests

* test(e2e): rework

* refactor: remove setBearerToken

* chore: restore migrations the way it was

* chore: reverted file to original state

* chore: removed unneeded test and renamed password to secret

* chore: function version

* chore: updated forms i18n keys

* chore: remove if from test

* chore: unneeded variable

* fix: add missing key on object list

* refactor: swift keychain access to retrieve all credentials in one call

* revert: edit server screen

* refactor: credentials use getGenericCredential

* fix: objc code calling old method

* fix: added scroll to login screen

* chore: variable names

* fix: avoid inline styles

* fix: Improved appVersion positioning

* Update app/screens/server/form.tsx



* feat: show error message on 403

* Revert "feat: show error message on 403"

This reverts commit f41630c767e10211adf1885321ceefd8a0931e32.

---------


(cherry picked from commit f50056f57b)

Co-authored-by: Felipe Martin <812088+fmartingr@users.noreply.github.com>
Co-authored-by: Matthew Birtch <mattbirtch@gmail.com>
2025-09-01 12:35:01 +03:00

136 lines
4.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, preauthSecret?: 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,
};
// Store main token credentials (clean format)
KeyChain.setInternetCredentials(serverUrl, token, token, options);
// Store preauth secret separately if provided
if (preauthSecret) {
KeyChain.setGenericPassword('preshared_secret', preauthSecret, {
server: serverUrl,
...options,
});
}
} catch (e) {
logWarning('could not set credentials', e);
}
};
export const removeServerCredentials = async (serverUrl: string) => {
await KeyChain.resetInternetCredentials({server: serverUrl});
try {
await KeyChain.resetGenericPassword({server: serverUrl});
} catch (e) {
// Preauth secret might not exist, ignore errors
}
};
export const removeActiveServerCredentials = async () => {
const serverUrl = await getActiveServerUrl();
if (serverUrl) {
await removeServerCredentials(serverUrl);
}
};
export const getServerCredentials = async (serverUrl: string): Promise<ServerCredential|null> => {
try {
// Get main credentials
const credentials = await KeyChain.getInternetCredentials(serverUrl);
if (!credentials) {
return null;
}
// 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 null;
}
// Get preauth secret separately
let preauthSecret: string | undefined;
try {
const preauthCredentials = await KeyChain.getGenericPassword({
server: serverUrl,
});
preauthSecret = preauthCredentials ? preauthCredentials.password : undefined;
} catch (e) {
// Preauth secret is optional, so ignore errors
preauthSecret = undefined;
}
return {
serverUrl,
userId,
token,
preauthSecret,
};
} catch (e) {
return null;
}
};