mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-30 03:19:11 +08:00
@ -1,53 +1,64 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class PublicKeyEditor extends StatelessWidget {
|
class PublicKeyEditor extends StatelessWidget {
|
||||||
final Function valueChanged;
|
final Key formKey;
|
||||||
|
final TextEditingController _controller;
|
||||||
|
|
||||||
PublicKeyEditor(this.valueChanged);
|
PublicKeyEditor(this.formKey, this._controller);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return KeyEditor(valueChanged, _validator);
|
return KeyEditor(formKey, _controller, _validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
String _validator(String val) {
|
String _validator(String val) {
|
||||||
if (!val.startsWith("ssh-")) {
|
val = val.trim();
|
||||||
return "Invalid Public Key";
|
if (!val.startsWith("ssh-rsa ")) {
|
||||||
|
return "Invalid Public Key - Doesn't start with ssh-rsa";
|
||||||
}
|
}
|
||||||
return "";
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class PrivateKeyEditor extends StatelessWidget {
|
class PrivateKeyEditor extends StatelessWidget {
|
||||||
final Function valueChanged;
|
final Key formKey;
|
||||||
|
final TextEditingController _controller;
|
||||||
|
|
||||||
PrivateKeyEditor(this.valueChanged);
|
PrivateKeyEditor(this.formKey, this._controller);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return KeyEditor(valueChanged, _validator);
|
return KeyEditor(formKey, _controller, _validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
String _validator(String val) {
|
String _validator(String val) {
|
||||||
if (!val.startsWith("-----BEGIN RSA PRIVATE KEY-----")) {
|
val = val.trim();
|
||||||
|
if (!val.startsWith("-----BEGIN ")) {
|
||||||
return "Invalid Private Key";
|
return "Invalid Private Key";
|
||||||
}
|
}
|
||||||
if (!val.startsWith("-----END RSA PRIVATE KEY-----")) {
|
if (!val.endsWith("PRIVATE KEY-----")) {
|
||||||
return "Invalid Private Key";
|
return "Invalid Private Key";
|
||||||
}
|
}
|
||||||
return "";
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class KeyEditor extends StatelessWidget {
|
class KeyEditor extends StatelessWidget {
|
||||||
final Function valueChanged;
|
final Key formKey;
|
||||||
|
final TextEditingController textEditingController;
|
||||||
final Function validator;
|
final Function validator;
|
||||||
|
|
||||||
KeyEditor(this.valueChanged, this.validator);
|
KeyEditor(this.formKey, this.textEditingController, this.validator) {
|
||||||
|
assert(formKey != null);
|
||||||
|
assert(textEditingController != null);
|
||||||
|
assert(validator != null);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var form = Form(
|
var form = Form(
|
||||||
|
key: formKey,
|
||||||
child: Builder(builder: (context) {
|
child: Builder(builder: (context) {
|
||||||
return TextFormField(
|
return TextFormField(
|
||||||
textAlign: TextAlign.left,
|
textAlign: TextAlign.left,
|
||||||
@ -55,13 +66,7 @@ class KeyEditor extends StatelessWidget {
|
|||||||
style: Theme.of(context).textTheme.body1,
|
style: Theme.of(context).textTheme.body1,
|
||||||
autovalidate: true,
|
autovalidate: true,
|
||||||
validator: validator,
|
validator: validator,
|
||||||
onChanged: (String newVal) {
|
controller: textEditingController,
|
||||||
if (Form.of(context).validate()) {
|
|
||||||
valueChanged(newVal);
|
|
||||||
} else {
|
|
||||||
valueChanged("");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
autovalidate: true,
|
autovalidate: true,
|
||||||
|
@ -207,14 +207,23 @@ class GitHostUserProvidedKeys extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _GitHostUserProvidedKeysState extends State<GitHostUserProvidedKeys> {
|
class _GitHostUserProvidedKeysState extends State<GitHostUserProvidedKeys> {
|
||||||
String publicKey = "";
|
GlobalKey<FormState> _publicFormKey;
|
||||||
String privateKey = "";
|
GlobalKey<FormState> _privateFormKey;
|
||||||
final _publicKeyController = TextEditingController();
|
TextEditingController _publicKeyController;
|
||||||
final _privateKeyController = TextEditingController();
|
TextEditingController _privateKeyController;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
|
||||||
|
_publicFormKey = GlobalKey<FormState>();
|
||||||
|
_privateFormKey = GlobalKey<FormState>();
|
||||||
|
_publicKeyController = TextEditingController();
|
||||||
|
_privateKeyController = TextEditingController();
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var nextEnabled = publicKey.isNotEmpty && privateKey.isNotEmpty;
|
|
||||||
return Container(
|
return Container(
|
||||||
child: Column(
|
child: Column(
|
||||||
children: <Widget>[
|
children: <Widget>[
|
||||||
@ -223,32 +232,35 @@ class _GitHostUserProvidedKeysState extends State<GitHostUserProvidedKeys> {
|
|||||||
style: Theme.of(context).textTheme.headline,
|
style: Theme.of(context).textTheme.headline,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8.0),
|
const SizedBox(height: 8.0),
|
||||||
PublicKeyEditor((String newVal) {
|
PublicKeyEditor(_publicFormKey, _publicKeyController),
|
||||||
setState(() {
|
|
||||||
publicKey = newVal;
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
const SizedBox(height: 8.0),
|
const SizedBox(height: 8.0),
|
||||||
Text(
|
Text(
|
||||||
"Private Key -",
|
"Private Key -",
|
||||||
style: Theme.of(context).textTheme.headline,
|
style: Theme.of(context).textTheme.headline,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 8.0),
|
const SizedBox(height: 8.0),
|
||||||
PrivateKeyEditor((String newVal) {
|
PrivateKeyEditor(_privateFormKey, _privateKeyController),
|
||||||
setState(() {
|
|
||||||
privateKey = newVal;
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
const SizedBox(height: 16.0),
|
const SizedBox(height: 16.0),
|
||||||
GitHostSetupButton(
|
GitHostSetupButton(
|
||||||
text: "Next",
|
text: "Next",
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
if (!nextEnabled) {
|
var publicValid = _publicFormKey.currentState.validate();
|
||||||
|
var privateValid = _privateFormKey.currentState.validate();
|
||||||
|
|
||||||
|
if (!publicValid || !privateValid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var publicKey = _publicKeyController.text.trim();
|
var publicKey = _publicKeyController.text.trim();
|
||||||
|
if (!publicKey.endsWith('\n')) {
|
||||||
|
publicKey += '\n';
|
||||||
|
}
|
||||||
|
|
||||||
var privateKey = _privateKeyController.text.trim();
|
var privateKey = _privateKeyController.text.trim();
|
||||||
|
if (!privateKey.endsWith('\n')) {
|
||||||
|
privateKey += '\n';
|
||||||
|
}
|
||||||
|
|
||||||
widget.doneFunction(publicKey, privateKey);
|
widget.doneFunction(publicKey, privateKey);
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
Reference in New Issue
Block a user