platform/lib/src/commands/key.dart

31 lines
938 B
Dart
Raw Normal View History

2016-12-24 04:42:55 +00:00
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);
2016-12-31 18:19:51 +00:00
print('Generated new development JWT secret: $secret');
2016-12-24 04:42:55 +00:00
await changeSecret(new File('config/default.yaml'), secret);
2016-12-31 18:19:51 +00:00
secret = rs.randomAlphaNumeric(32);
print('Generated new production JWT secret: $secret');
2016-12-24 04:42:55 +00:00
await changeSecret(new File('config/production.yaml'), secret);
}
changeSecret(File file, String secret) async {
if (await file.exists()) {
2016-12-24 05:24:40 +00:00
var contents = await file.readAsString();
contents = contents.replaceAll(new RegExp(r'jwt_secret:[^\n]+\n?'), '');
2017-02-01 22:56:04 +00:00
await file.writeAsString(contents.trim() + '\njwt_secret: "$secret"');
2016-12-24 04:42:55 +00:00
}
}
}