Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Herohtar committed Jul 14, 2018
0 parents commit 6451c18
Show file tree
Hide file tree
Showing 10 changed files with 203 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Files and directories created by pub
.dart_tool/
.packages
.pub/
build/
pubspec.lock

# Directory created by dartdoc
doc/api/
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [1.0.0] - 2018-07-13

* Initial release.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Herohtar

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# credentials_helper

The idea behind this package is to provide a simple way to use various credentials in a project (particularly during development/testing) that is less prone to result in a private API key or the like being commited or published along with your code.

You can store your API keys, usernames, passwords, etc. in a JSON file that is either listed in your `.gitignore` or completely outside the project directory and load them into this class. You could also retrieve them from other JSON sources.

## Example
```dart
import 'package:credentials_helper/credentials_helper.dart';
void main() {
Credentials credentials = Credentials.fromFile('credentials.json');
myApiCall(apiKey: credentials.apiKey);
}
```
9 changes: 9 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:credentials_helper/credentials_helper.dart';

void main() {

Credentials credentials = Credentials.fromFile('credentials.json');

myApiCall(apiKey: credentials.apiKey);

}
1 change: 1 addition & 0 deletions lib/credentials_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'package:credentials_helper/src/credentials.dart';
38 changes: 38 additions & 0 deletions lib/src/credentials.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:convert';
import 'dart:io';

import 'package:json_annotation/json_annotation.dart';

part 'credentials.g.dart';

/// Holds various types of credentials
@JsonSerializable(includeIfNull: false)
class Credentials extends Object with _$CredentialsSerializerMixin {
final String apiKey;

final String username;

final String password;

/// Constructs a new [Credentials] object.
Credentials(
{
this.apiKey,
this.username,
this.password
}
);

/// Constructs a new [Credentials] object from a JSON map
factory Credentials.fromJson(Map<String, dynamic> json) => _$CredentialsFromJson(json);

/// Constructs a new [Credentials] object from a JSON file
factory Credentials.fromFile(String fileName) {
return Credentials.fromJson(json.decode(File(fileName).readAsStringSync()));
}

/// Writes the current [Credentials] to a JSON file
void toFile(String fileName) {
File(fileName).writeAsStringSync(json.encode(this.toJson()), flush: true);
}
}
34 changes: 34 additions & 0 deletions lib/src/credentials.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: credentials_helper
description: Provides a helper class to simplify loading credentials from JSON
version: 1.0.0
author: "Herohtar <[email protected]>"
homepage: https://github.com/herohtar/credentials_helper

dependencies:
json_annotation: ^0.2.9

dev_dependencies:
test: ^1.2.0
build_runner: ^0.9.1
json_serializable: ^0.5.8

environment:
sdk: ">=2.0.0-dev.58.0 <2.0.0"
54 changes: 54 additions & 0 deletions test/credentials_helper_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'dart:convert';
import 'dart:io';

import 'package:test/test.dart';
import 'package:credentials_helper/credentials_helper.dart';

void main() {
const String apiKey = 'test-api-key';
const String username = 'testUserName';
const String password = 'testPassword';
const String fileName = 'test_credentials.json';
const String jsonString = '{"apiKey":"$apiKey","username":"$username","password":"$password"}';

test('tests the default constructor', () {
Credentials credentials = Credentials(
apiKey: apiKey,
username: username,
password: password
);

expect(credentials.apiKey, apiKey);
expect(credentials.username, username);
expect(credentials.password, password);
expect(json.encode(credentials.toJson()), jsonString);
});

test('tests the .fromJson constructor', () {
Credentials credentials = Credentials.fromJson(json.decode(jsonString));

expect(credentials.apiKey, apiKey);
expect(credentials.username, username);
expect(credentials.password, password);
expect(json.encode(credentials.toJson()), jsonString);
});

test('tests .toFile and .fromFile', () {
Credentials originalCredentials = Credentials.fromJson(json.decode(jsonString));

if (File(fileName).existsSync()) {
File(fileName).deleteSync();
}

originalCredentials.toFile(fileName);
expect(File(fileName).existsSync(), true);
expect(File(fileName).readAsStringSync(), jsonString);

Credentials newCredentials = Credentials.fromFile(fileName);
expect(newCredentials.apiKey, originalCredentials.apiKey);
expect(newCredentials.username, originalCredentials.username);
expect(newCredentials.password, originalCredentials.password);

File(fileName).deleteSync();
});
}

0 comments on commit 6451c18

Please sign in to comment.