Skip to content

Commit

Permalink
Require login after sent to background: add toggle in Security settings
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Dec 15, 2022
1 parent 8b684a9 commit 47c7e53
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
"views.Settings.Security.deletePIN": "Delete PIN",
"views.Settings.Security.deleteDuressPIN": "Delete Duress PIN",
"views.Settings.Security.scramblePIN": "Scramble PIN numbers",
"views.Settings.Security.loginBackground": "Require login after app returns from background",
"views.SparkQRScanner.text": "Scan a Spark QR code",
"views.SparkQRScanner.error": "Error fetching Spark config",
"views.ImportAccount.title": "Import account",
Expand Down
2 changes: 2 additions & 0 deletions stores/SettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface Settings {
pin?: string;
duressPin?: string;
scramblePin?: boolean;
loginBackground?: boolean;
authenticationAttempts?: number;
fiat?: string;
locale?: string;
Expand Down Expand Up @@ -164,6 +165,7 @@ export default class SettingsStore {
enableMempoolRates: true
},
scramblePin: true,
loginBackground: false,
fiat: DEFAULT_FIAT
};
@observable public loading = false;
Expand Down
78 changes: 75 additions & 3 deletions views/Settings/Security.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ interface SecurityProps {

interface SecurityState {
scramblePin: boolean;
loginBackground: boolean;
displaySecurityItems: Array<any>;
pinExists: boolean;
passphraseExists: boolean;
}

const possibleSecurityItems = [
Expand Down Expand Up @@ -54,8 +56,10 @@ export default class Security extends React.Component<
> {
state = {
scramblePin: true,
loginBackground: false,
displaySecurityItems: [],
pinExists: false
pinExists: false,
passphraseExists: false
};

async componentDidMount() {
Expand All @@ -64,7 +68,8 @@ export default class Security extends React.Component<
const settings = await getSettings();

this.setState({
scramblePin: settings.scramblePin ?? true
scramblePin: settings.scramblePin ?? true,
loginBackground: settings.loginBackground ?? false
});

if (settings.pin) {
Expand All @@ -73,6 +78,12 @@ export default class Security extends React.Component<
});
}

if (settings.passphrase) {
this.setState({
passphraseExists: true
});
}

// Three cases:
// 1) If no passphrase or pin is set, allow user to set passphrase or pin
// 2) If passphrase is set, allow user to set passphrase or duress passphrase
Expand Down Expand Up @@ -166,7 +177,13 @@ export default class Security extends React.Component<

render() {
const { navigation, SettingsStore } = this.props;
const { scramblePin, displaySecurityItems, pinExists } = this.state;
const {
scramblePin,
displaySecurityItems,
pinExists,
passphraseExists,
loginBackground
} = this.state;
const { setSettings, getSettings } = SettingsStore;

const BackButton = () => (
Expand Down Expand Up @@ -259,6 +276,61 @@ export default class Security extends React.Component<
/>
</ListItem>
)}
{(pinExists || passphraseExists) && (
<ListItem
containerStyle={{
backgroundColor: themeColor('background')
}}
>
<ListItem.Content>
<ListItem.Title
style={{
color: themeColor('secondaryText'),
fontFamily: 'Lato-Regular'
}}
>
{localeString(
'views.Settings.Security.loginBackground'
)}
</ListItem.Title>
</ListItem.Content>
<Switch
value={scramblePin}
onValueChange={async () => {
this.setState({
loginBackground: !loginBackground
});
const settings = await getSettings();
setSettings(
JSON.stringify({
nodes: settings.nodes,
theme: settings.theme,
selectedNode: settings.selectedNode,
fiat: settings.fiat,
passphrase: settings.passphrase,
duressPassphrase:
settings.duressPassphrase,
pin: settings.pin,
duressPin: settings.duressPin,
scramblePin: settings.scramblePin,
authenticationAttempts:
settings.authenticationAttempts,
locale: settings.locale,
privacy: settings.privacy,
loginBackground: !loginBackground
})
);
}}
trackColor={{
false: '#767577',
true: themeColor('highlight')
}}
style={{
alignSelf: 'flex-end'
}}
/>
</ListItem>
)}
</ScrollView>
);
}
Expand Down
13 changes: 11 additions & 2 deletions views/Wallet/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,19 @@ export default class Wallet extends React.Component<WalletProps, WalletState> {

handleAppStateChange = (nextAppState: any) => {
const { SettingsStore } = this.props;
const { settings } = SettingsStore;
const { settings, implementation } = SettingsStore;
const { loginBackground } = settings;
const loginRequired = settings && (settings.passphrase || settings.pin);

if (nextAppState.match(/inactive|background/) && loginRequired) {
if (
nextAppState.match(/inactive|background/) &&
loginRequired &&
loginBackground
) {
if (implementation === 'lightning-node-connect') {
RESTUtils.disconnect();
}

RNRestart.Restart();
}
};
Expand Down

0 comments on commit 47c7e53

Please sign in to comment.