mirror of
https://github.com/haorendashu/nowser.git
synced 2025-12-17 01:44:19 +01:00
auth support always
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:nowser/component/auth_dialog/auth_dialog_base_componnet.dart';
|
||||
import 'package:nowser/const/auth_result.dart';
|
||||
import 'package:nowser/const/auth_type.dart';
|
||||
import 'package:nowser/data/app.dart';
|
||||
import 'package:nowser/main.dart';
|
||||
import 'package:nowser/util/router_util.dart';
|
||||
|
||||
import '../../const/base.dart';
|
||||
@@ -48,6 +49,8 @@ class AuthDialog extends StatefulWidget {
|
||||
class _AuthDialog extends State<AuthDialog> {
|
||||
bool showDetail = false;
|
||||
|
||||
bool always = false;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
var themeData = Theme.of(context);
|
||||
@@ -113,7 +116,7 @@ class _AuthDialog extends State<AuthDialog> {
|
||||
);
|
||||
if (showDetail) {
|
||||
detailList.add(Container(
|
||||
height: 210,
|
||||
height: 142,
|
||||
width: double.infinity,
|
||||
padding: EdgeInsets.all(Base.BASE_PADDING_HALF),
|
||||
decoration: BoxDecoration(
|
||||
@@ -132,7 +135,7 @@ class _AuthDialog extends State<AuthDialog> {
|
||||
}
|
||||
|
||||
list.add(Container(
|
||||
height: 250,
|
||||
height: 180,
|
||||
width: double.infinity,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
@@ -141,6 +144,30 @@ class _AuthDialog extends State<AuthDialog> {
|
||||
),
|
||||
));
|
||||
|
||||
list.add(Container(
|
||||
child: GestureDetector(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
always = !always;
|
||||
});
|
||||
},
|
||||
behavior: HitTestBehavior.translucent,
|
||||
child: Row(
|
||||
children: [
|
||||
Checkbox(
|
||||
value: always,
|
||||
onChanged: (v) {
|
||||
setState(() {
|
||||
always = v!;
|
||||
});
|
||||
},
|
||||
),
|
||||
Text("Always"),
|
||||
],
|
||||
),
|
||||
),
|
||||
));
|
||||
|
||||
var child = Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: list,
|
||||
@@ -149,14 +176,30 @@ class _AuthDialog extends State<AuthDialog> {
|
||||
return AuthDialogBaseComponnet(
|
||||
app: widget.app,
|
||||
title: authTitle,
|
||||
onReject: onReject,
|
||||
onConfirm: onConfirm,
|
||||
pubkeyReadonly: true,
|
||||
child: child,
|
||||
);
|
||||
}
|
||||
|
||||
onReject() {
|
||||
if (always) {
|
||||
// always do reject this
|
||||
appProvider.alwaysReject(widget.app.appType!, widget.app.code!,
|
||||
widget.authType, widget.eventKind);
|
||||
}
|
||||
|
||||
RouterUtil.back(context);
|
||||
}
|
||||
|
||||
onConfirm() {
|
||||
print("auth dialog confirm!");
|
||||
if (always) {
|
||||
// always do confirm this
|
||||
appProvider.alwaysAllow(widget.app.appType!, widget.app.code!,
|
||||
widget.authType, widget.eventKind);
|
||||
}
|
||||
|
||||
RouterUtil.back(context, AuthResult.OK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,8 @@ class AuthDialogBaseComponnet extends StatefulWidget {
|
||||
|
||||
Widget child;
|
||||
|
||||
Function? onReject;
|
||||
|
||||
Function onConfirm;
|
||||
|
||||
Function(String)? onPubkeyChange;
|
||||
@@ -27,6 +29,7 @@ class AuthDialogBaseComponnet extends StatefulWidget {
|
||||
required this.app,
|
||||
required this.title,
|
||||
required this.child,
|
||||
this.onReject,
|
||||
required this.onConfirm,
|
||||
this.onPubkeyChange,
|
||||
this.pubkeyReadonly = false,
|
||||
@@ -149,7 +152,11 @@ class _AuthDialog extends State<AuthDialogBaseComponnet> {
|
||||
// ),
|
||||
FilledButton(
|
||||
onPressed: () {
|
||||
RouterUtil.back(context);
|
||||
if (widget.onReject != null) {
|
||||
widget.onReject!();
|
||||
} else {
|
||||
RouterUtil.back(context);
|
||||
}
|
||||
},
|
||||
child: Text("Cancel"),
|
||||
style: ButtonStyle(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:nostr_sdk/utils/string_util.dart';
|
||||
import 'package:nowser/const/app_type.dart';
|
||||
import 'package:nowser/const/auth_type.dart';
|
||||
import 'package:nowser/const/connect_type.dart';
|
||||
import 'package:nowser/data/app_db.dart';
|
||||
|
||||
@@ -130,4 +131,71 @@ class AppProvider extends ChangeNotifier {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void alwaysAllow(int appType, String code, int authType, int? eventKind) {
|
||||
_addAlways(appType, code, authType, eventKind, AuthResult.OK);
|
||||
}
|
||||
|
||||
void alwaysReject(int appType, String code, int authType, int? eventKind) {
|
||||
_addAlways(appType, code, authType, eventKind, AuthResult.REJECT);
|
||||
}
|
||||
|
||||
void _addAlways(
|
||||
int appType, String code, int authType, int? eventKind, int authResult) {
|
||||
var app = getApp(appType, code);
|
||||
if (app != null) {
|
||||
var permissionMap = _getPermissionMap(app);
|
||||
var key = "$authType";
|
||||
if (eventKind != null) {
|
||||
key = "$key-$eventKind";
|
||||
}
|
||||
permissionMap[key] = authResult;
|
||||
|
||||
List<String> allowAuthTypeStrs = [];
|
||||
List<String> rejectAuthTypeStrs = [];
|
||||
|
||||
List<String> allowEventKindStrs = [];
|
||||
List<String> rejectEventKindStrs = [];
|
||||
var entries = permissionMap.entries;
|
||||
for (var entry in entries) {
|
||||
var key = entry.key;
|
||||
var value = entry.value;
|
||||
|
||||
if (key.contains("-")) {
|
||||
var strs = key.split("-");
|
||||
|
||||
var eventKindStr = strs[1];
|
||||
if (value > 0) {
|
||||
allowEventKindStrs.add(eventKindStr);
|
||||
} else {
|
||||
rejectEventKindStrs.add(eventKindStr);
|
||||
}
|
||||
} else {
|
||||
var authTypeStr = key;
|
||||
if (value > 0) {
|
||||
allowAuthTypeStrs.add(authTypeStr);
|
||||
} else {
|
||||
rejectAuthTypeStrs.add(authTypeStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (allowEventKindStrs.isNotEmpty) {
|
||||
allowAuthTypeStrs
|
||||
.add("${AuthType.SIGN_EVENT}-${allowEventKindStrs.join(",")}");
|
||||
}
|
||||
if (rejectEventKindStrs.isNotEmpty) {
|
||||
rejectAuthTypeStrs
|
||||
.add("${AuthType.SIGN_EVENT}-${rejectEventKindStrs.join(",")}");
|
||||
}
|
||||
|
||||
app.alwaysAllow = allowAuthTypeStrs.join(";");
|
||||
app.alwaysReject = rejectAuthTypeStrs.join(";");
|
||||
|
||||
var appCode = getAppCode(appType, code);
|
||||
appPermissions.remove(appCode);
|
||||
|
||||
update(app);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user