Skip to content

Commit a073fc5

Browse files
authored
feat(linux): Migrated from PAM to Polkit.
Merge pull request #32 from Skyost/linux-polkit
2 parents 15ea27f + 7d17e57 commit a073fc5

26 files changed

+312
-311
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ lib/app.dart
5252
**/*.g.dart
5353
/assets/images/*.si
5454
/assets/images/**/*.si
55+
snap/meta/polkit
56+
open-authenticator_*.snap
5557

5658
# Firebase files
5759
firebase

bin/generate_polkit_policy.dart

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:open_authenticator/model/app_unlock/reason.dart';
5+
import 'package:path/path.dart' as path;
6+
7+
/// Generates the polkit policy file and translations as well.
8+
Future<void> main() async {
9+
stdout.writeln('Reading translations...');
10+
List<String> locales = [];
11+
List<FileSystemEntity> files = Directory('lib/i18n').listSync();
12+
for (FileSystemEntity file in files) {
13+
if (file is Directory) {
14+
locales.add(path.basename(file.path));
15+
}
16+
}
17+
Map<String, Map> translations = {};
18+
for (String locale in locales) {
19+
File translationFile = File('lib/i18n/$locale/app_unlock.json');
20+
translations[locale] = jsonDecode(translationFile.readAsStringSync());
21+
}
22+
23+
stdout.writeln('Generating polkit policy file...');
24+
String policyFileContent = '''<?xml version="1.0" encoding="UTF-8"?>
25+
<policyconfig>
26+
''';
27+
for (UnlockReason reason in UnlockReason.values) {
28+
policyFileContent += '''
29+
<action id="app.openauthenticator.${reason.name}">
30+
<description gettext-domain="polkit.app.openauthenticator">${translations["en"]!["authenticationRequired"]}</description>
31+
<message gettext-domain="polkit.app.openauthenticator">${translations["en"]!["localAuthentication(map)"][reason.name]}</message>
32+
<defaults>
33+
<allow_any>auth_self</allow_any>
34+
<allow_inactive>auth_self</allow_inactive>
35+
<allow_active>auth_self</allow_active>
36+
</defaults>
37+
</action>
38+
''';
39+
}
40+
policyFileContent += '</policyconfig>';
41+
File policyFile = File('snap/meta/polkit/polkit.app.openauthenticator.policy');
42+
policyFile.parent.createSync(recursive: true);
43+
policyFile.writeAsStringSync(policyFileContent);
44+
stdout.writeln('Done : ${policyFile.path}.');
45+
46+
for (String locale in locales) {
47+
if (locale == 'en') {
48+
continue;
49+
}
50+
stdout.writeln('Generating $locale translation...');
51+
String translationFileContent = '''msgid "${translations["en"]!["authenticationRequired"]}"
52+
msgstr "${translations[locale]!["authenticationRequired"]}"
53+
''';
54+
for (UnlockReason reason in UnlockReason.values) {
55+
translationFileContent += '''
56+
57+
msgid "${translations["en"]!["localAuthentication(map)"][reason.name]}"
58+
msgstr "${translations[locale]!["localAuthentication(map)"][reason.name]}"
59+
''';
60+
}
61+
File translationFile = File('snap/meta/polkit/po/$locale.po');
62+
translationFile.parent.createSync(recursive: true);
63+
translationFile.writeAsStringSync(translationFileContent);
64+
stdout.writeln('Done : ${translationFile.path}.');
65+
}
66+
}

lib/i18n/de/app_unlock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"text": "$app ist gesperrt.",
55
"button": "Entsperren"
66
},
7+
"authenticationRequired": "Authentifizierung erforderlich",
78
"localAuthentication(map)": {
89
"openApp": "Authentifizieren Sie sich, um auf die App zuzugreifen.",
910
"sensibleAction": "Authentifizieren Sie sich, um fortzufahren.",

lib/i18n/en/app_unlock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"text": "$app is locked.",
44
"button": "Unlock"
55
},
6+
"authenticationRequired": "Authentication required",
67
"localAuthentication(map)": {
78
"openApp": "Authenticate to access the app.",
89
"sensibleAction": "Authenticate to continue.",

lib/i18n/fr/app_unlock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"text": "$app est verrouillé.",
44
"button": "Déverrouiller"
55
},
6+
"authenticationRequired": "Authentification requise",
67
"localAuthentication(map)": {
78
"openApp": "Authentifiez-vous pour accéder à l'application.",
89
"sensibleAction": "Authentifiez-vous pour continuer.",

lib/i18n/it/app_unlock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"text": "$app è bloccata.",
44
"button": "Sblocca"
55
},
6+
"authenticationRequired": "Autenticazione richiesta",
67
"localAuthentication(map)": {
78
"openApp": "Autenticati per accedere all'app.",
89
"sensibleAction": "Autenticati per continuare.",

lib/i18n/pt/app_unlock.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"text": "$app está bloqueado.",
44
"button": "Desbloquear"
55
},
6+
"authenticationRequired": "Autenticação necessária",
67
"localAuthentication(map)": {
78
"openApp": "Autentique-se para acessar o app.",
89
"sensibleAction": "Autentique-se para continuar.",

lib/model/app_unlock/method.dart

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_riverpod/flutter_riverpod.dart';
33
import 'package:open_authenticator/i18n/translations.g.dart';
4+
import 'package:open_authenticator/model/app_unlock/reason.dart';
45
import 'package:open_authenticator/model/crypto.dart';
56
import 'package:open_authenticator/model/password_verification/methods/method.dart';
67
import 'package:open_authenticator/model/password_verification/methods/password_signature.dart';
@@ -69,7 +70,7 @@ sealed class CannotUnlockException implements Exception {}
6970
class LocalAuthenticationAppUnlockMethod extends AppUnlockMethod {
7071
@override
7172
Future<Result> _tryUnlock(BuildContext context, Ref ref, UnlockReason reason) async {
72-
bool result = await LocalAuthentication.instance.authenticate(context, translations.appUnlock.localAuthentication[reason.name] ?? 'Authenticate to access the app.');
73+
bool result = await LocalAuthentication.instance.authenticate(context, reason);
7374
return result ? const ResultSuccess() : const ResultCancelled();
7475
}
7576

@@ -174,18 +175,3 @@ class NoneAppUnlockMethod extends AppUnlockMethod {
174175
@override
175176
AppLockState get defaultAppLockState => AppLockState.unlocked;
176177
}
177-
178-
/// Configures the unlock reason for [UnlockChallenge]s.
179-
enum UnlockReason {
180-
/// The user tries the unlock challenge for opening the app.
181-
openApp,
182-
183-
/// The user tries to do a sensible action.
184-
sensibleAction,
185-
186-
/// The user tries the unlock challenge for enabling the current method.
187-
enable,
188-
189-
/// The user tries the unlock challenge for disabling the current method.
190-
disable;
191-
}

lib/model/app_unlock/reason.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// Configures the unlock reason for [UnlockChallenge]s.
2+
enum UnlockReason {
3+
/// The user tries the unlock challenge for opening the app.
4+
openApp,
5+
6+
/// The user tries to do a sensible action.
7+
sensibleAction,
8+
9+
/// The user tries the unlock challenge for enabling the current method.
10+
enable,
11+
12+
/// The user tries the unlock challenge for disabling the current method.
13+
disable;
14+
}

lib/model/app_unlock/state.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:async';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_riverpod/flutter_riverpod.dart';
55
import 'package:open_authenticator/model/app_unlock/method.dart';
6+
import 'package:open_authenticator/model/app_unlock/reason.dart';
67
import 'package:open_authenticator/model/settings/app_unlock_method.dart';
78
import 'package:open_authenticator/utils/result.dart';
89

0 commit comments

Comments
 (0)