Skip to content

Commit a86fe39

Browse files
committed
Added an utility to generate the Polkit policy.
1 parent 4ab5596 commit a86fe39

26 files changed

Lines changed: 123 additions & 86 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ lib/app.dart
5656
**/*.g.dart
5757
/assets/images/*.si
5858
/assets/images/**/*.si
59+
snap/meta/polkit
5960

6061
# Firebase files
6162
firebase

bin/generate_polkit_policy.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
for (UnlockReason reason in UnlockReason.values) {
27+
policyFileContent += '''
28+
<action id="app.openauthenticator.${reason.name}">
29+
<description gettext-domain="polkit.app.openauthenticator">${translations["en"]!["authenticationRequired"]}</description>
30+
<message gettext-domain="polkit.app.openauthenticator">${translations["en"]!["localAuthentication(map)"][reason.name]}</message>
31+
<defaults>
32+
<allow_any>auth_self</allow_any>
33+
<allow_inactive>auth_self</allow_inactive>
34+
<allow_active>auth_self</allow_active>
35+
</defaults>
36+
</action>
37+
''';
38+
}
39+
policyFileContent += '</policyconfig>';
40+
File policyFile = File('snap/meta/polkit/polkit.app.openauthenticator.policy');
41+
policyFile.parent.createSync(recursive: true);
42+
policyFile.writeAsStringSync(policyFileContent);
43+
stdout.writeln('Done : ${policyFile.path}.');
44+
45+
for (String locale in locales) {
46+
if (locale == 'en') {
47+
continue;
48+
}
49+
stdout.writeln('Generating $locale translation...');
50+
String translationFileContent = '''msgid "${translations["en"]!["authenticationRequired"]}"
51+
msgstr "${translations[locale]!["authenticationRequired"]}"
52+
''';
53+
for (UnlockReason reason in UnlockReason.values) {
54+
translationFileContent += '''
55+
msgid "${translations["en"]!["localAuthentication(map)"][reason.name]}"
56+
msgstr "${translations[locale]!["localAuthentication(map)"][reason.name]}"
57+
''';
58+
}
59+
File translationFile = File('snap/meta/polkit/po/$locale.po');
60+
translationFile.parent.createSync(recursive: true);
61+
translationFile.writeAsStringSync(translationFileContent);
62+
stdout.writeln('Done : ${translationFile.path}.');
63+
}
64+
}

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)