Skip to content

Commit

Permalink
Add per-field obfuscation
Browse files Browse the repository at this point in the history
  • Loading branch information
ThexXTURBOXx committed Aug 12, 2022
1 parent bee3944 commit ab46b95
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 10 additions & 1 deletion packages/envied/lib/src/envied_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Envied {
/// value when being accessed the first time.
/// Please note that the values can not be offered with
/// the const qualifier, but only with final.
/// **Can be overridden by the per-field obfuscate option!**
final bool obfuscate;

const Envied(
Expand All @@ -46,5 +47,13 @@ class EnviedField {
/// The environment variable name specified in the `.env` file to generate for the annotated variable
final String? varName;

const EnviedField({this.varName});
/// Allows this values to be encrypted using a random
/// generated key that is then XOR'd with the encrypted
/// value when being accessed the first time.
/// Please note that the values can not be offered with
/// the const qualifier, but only with final.
/// **Overrides the per-class obfuscate option!**
final bool? obfuscate;

const EnviedField({this.varName, this.obfuscate});
}
8 changes: 7 additions & 1 deletion packages/envied_generator/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ class EnviedGenerator extends GeneratorForAnnotation<Envied> {
if (enviedFieldChecker.hasAnnotationOf(fieldEl)) {
DartObject? dartObject = enviedFieldChecker.firstAnnotationOf(fieldEl);
ConstantReader reader = ConstantReader(dartObject);

String varName =
reader.read('varName').literalValue as String? ?? fieldEl.name;

String? varValue;
if (envs.containsKey(varName)) {
varValue = envs[varName];
} else if (Platform.environment.containsKey(varName)) {
varValue = Platform.environment[varName];
}
return (config.obfuscate ? generateLineEncrypted : generateLine)(

final bool obfuscate =
reader.read('obfuscate').literalValue as bool? ?? config.obfuscate;

return (obfuscate ? generateLineEncrypted : generateLine)(
fieldEl,
varValue,
);
Expand Down

0 comments on commit ab46b95

Please sign in to comment.