Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining a custom cookie vault #63

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 26 additions & 11 deletions lib/src/common.dart
Expand Up @@ -9,23 +9,16 @@ import 'package:crypto/crypto.dart';
class Common {
const Common();

// Temporary directory
static final path = Directory.systemTemp.path;

// Creates a store
static final store = newHiveDefaultVaultStore(path: path);

// Creates a vault from the previously created store
static final vault = store.vault<String>(
name: 'cookieVault',
eventListenerMode: EventListenerMode.synchronous,
);
// The vault containing the cookies. Can be initialized before usage using `setCookieVault`,
// otherwise it will be created using `createDefaultCookieVault`.
sehnryr marked this conversation as resolved.
Show resolved Hide resolved
static Vault<String>? vault;

/// Add / Replace this [Vault] [value] for the specified [key].
///
/// * [key]: the key
/// * [value]: the value
static Future<void> storageSet(String key, String value) async {
final vault = getVault();
await vault.put(key, value);
}

Expand All @@ -35,6 +28,7 @@ class Common {
///
/// Returns a [String]
static Future<String?> storageGet(String key) async {
final vault = getVault();
return await vault.get(key);
}

Expand All @@ -44,6 +38,7 @@ class Common {
///
/// Returns `true` if the removal of the mapping for the specified [key] was successful.
static Future<bool> storageRemove(String key) async {
final vault = getVault();
await vault.remove(key);
return !await vault.containsKey(key);
}
Expand Down Expand Up @@ -111,4 +106,24 @@ class Common {

return result;
}

/// Gets or creates the cookie vault.
static Vault<String> getVault() {
return vault ?? (vault = createDefaultCookieVault());
}

/// Creates a default, plain-text Hive store and vault in the system temporary directory.
static Vault<String> createDefaultCookieVault() {
final path = Directory.systemTemp.path;
final store = newHiveDefaultVaultStore(path: path);
return store.vault<String>(
name: 'cookieVault', eventListenerMode: EventListenerMode.synchronous);
}

/// Defines the cookie vault for this process.
/// No-op if a cookie vault is already defined.
static Vault<String> setCookieVault(Vault<String> newVault) {
assert(vault == null, 'Cookie vault is already initialized');
return vault ?? (vault = newVault);
}
sehnryr marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 7 additions & 0 deletions lib/src/requests.dart
Expand Up @@ -5,6 +5,7 @@ import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart' as io_client;
import 'package:logging/logging.dart';
import 'package:stash/stash_api.dart';

import 'common.dart';
import 'event.dart';
Expand Down Expand Up @@ -293,6 +294,12 @@ class Requests {
);
}

/// Defines the `stash` `Vault` used to store cookies.
/// Must be defined before storing or accessing cookies, otherwise
/// a default one will be created on first use.
static Vault<String> setCookieVault(Vault<String> vault) =>
Common.setCookieVault(vault);
sehnryr marked this conversation as resolved.
Show resolved Hide resolved

static Future<Response> _httpRequest(HttpMethod method, String url,
{dynamic json,
dynamic body,
Expand Down