-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6451c18
Showing
10 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## [1.0.0] - 2018-07-13 | ||
|
||
* Initial release. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export 'package:credentials_helper/src/credentials.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} |