platform/lib/src/commands/key.dart

43 lines
1.1 KiB
Dart
Raw Normal View History

2016-12-24 04:42:55 +00:00
import 'dart:convert';
import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:random_string/random_string.dart' as rs;
class KeyCommand extends Command {
@override
String get name => 'key';
@override
String get description => 'Generates a new `angel_auth`key.';
@override
run() async {
var secret = rs.randomAlphaNumeric(32);
print('Generated new JWT secret: $secret');
await changeSecret(new File('config/default.yaml'), secret);
await changeSecret(new File('config/production.yaml'), secret);
}
changeSecret(File file, String secret) async {
if (await file.exists()) {
2016-12-24 04:45:37 +00:00
bool foundSecret = false;
2016-12-24 04:42:55 +00:00
var sink = await file.openWrite();
2016-12-24 04:45:37 +00:00
await for (var chunk in await file.openRead().transform(UTF8.decoder)) {
2016-12-24 04:42:55 +00:00
var lines = chunk.split('\n');
for (String line in lines) {
if (line.contains('jwt_secret:')) {
2016-12-24 04:45:37 +00:00
foundSecret = true;
2016-12-24 04:42:55 +00:00
sink.writeln('jwt_secret: $secret');
2016-12-24 04:45:37 +00:00
} else
sink.writeln(line);
2016-12-24 04:42:55 +00:00
}
}
2016-12-24 04:45:37 +00:00
if (!foundSecret) sink.writeln('jwt:secret: $secret');
await sink.close();
2016-12-24 04:42:55 +00:00
}
}
}