-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and add secure storage module
- Loading branch information
Showing
58 changed files
with
17,657 additions
and
63,241 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
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
61 changes: 61 additions & 0 deletions
61
packages/celest_auth/android/src/main/kotlin/dev/celest/celest_auth/CelestSecureStorage.kt
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,61 @@ | ||
package dev.celest.celest_auth | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.Activity | ||
import android.content.SharedPreferences | ||
import androidx.annotation.Keep | ||
import androidx.annotation.Nullable | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKey | ||
|
||
// TODO(dnys1): Exclude from backup: | ||
// - https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences | ||
// - https://developer.android.com/guide/topics/data/autobackup#IncludingFiles | ||
@Keep | ||
class CelestSecureStorage(private val mainActivity: Activity) { | ||
|
||
private val sharedPreferences: SharedPreferences by lazy { | ||
val masterKey = MasterKey.Builder(mainActivity) | ||
.setKeyScheme(MasterKey.KeyScheme.AES256_GCM) | ||
.build() | ||
val sharedPreferences = EncryptedSharedPreferences.create( | ||
mainActivity, | ||
"auth_secrets", | ||
masterKey, | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, | ||
) | ||
sharedPreferences | ||
} | ||
|
||
private val editor: SharedPreferences.Editor | ||
get() = sharedPreferences.edit() | ||
|
||
fun write(dataKey: String, value: String?) { | ||
with(editor) { | ||
putString(dataKey, value) | ||
apply() | ||
} | ||
} | ||
|
||
fun read(dataKey: String): String? = sharedPreferences.getString(dataKey, null) | ||
|
||
fun readAll(): Map<String, String> = sharedPreferences.all.mapValues { it.value as String } | ||
|
||
fun delete(dataKey: String): String? { | ||
val current = read(dataKey) | ||
with(editor) { | ||
remove(dataKey) | ||
apply() | ||
} | ||
return current | ||
} | ||
|
||
fun clear() { | ||
with(editor) { | ||
clear() | ||
apply() | ||
} | ||
} | ||
|
||
} |
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
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
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
This file was deleted.
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,50 @@ | ||
name: CoreFoundation | ||
description: | | ||
Bindings for Core Foundation on iOS/macOS. | ||
Regenerate bindings with `dart run ffigen --config=ffigen.core_foundation.yaml`. | ||
language: "c" | ||
output: | ||
bindings: "lib/src/native/darwin/core_foundation.ffi.dart" | ||
compiler-opts: | ||
- "-F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" | ||
headers: | ||
entry-points: | ||
- "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFDictionary.h" | ||
- "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFString.h" | ||
- "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFData.h" | ||
preamble: | | ||
// ignore_for_file: type=lint | ||
// ignore_for_file: return_of_invalid_type | ||
// ignore_for_file: unnecessary_non_null_assertion | ||
comments: | ||
style: any | ||
length: full | ||
|
||
ffi-native: | ||
exclude-all-by-default: true | ||
typedefs: | ||
include: | ||
- "CF.*" | ||
globals: | ||
include: | ||
- "kCF.*" | ||
functions: | ||
include: | ||
- CFDictionaryCreate | ||
- CFDataCreate | ||
- CFStringGetCStringPtr | ||
- CFStringGetCString | ||
- CFStringGetLength | ||
- CFStringGetMaximumSizeForEncoding | ||
- CFStringCreateWithCString | ||
- CFDataGetBytePtr | ||
- CFRelease | ||
structs: | ||
rename: | ||
"__CFString": CFString | ||
"__CFData": CFData | ||
"__CFDictionary": CFDictionary | ||
unnamed-enums: | ||
include: | ||
- "kCF.*" |
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
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
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,76 @@ | ||
name: SecurityFramework | ||
description: | | ||
Bindings for Security framework on iOS/macOS. | ||
Regenerate bindings with `dart run ffigen --config=ffigen.security.yaml`. | ||
language: "c" | ||
output: | ||
bindings: "lib/src/native/darwin/security.ffi.dart" | ||
compiler-opts: | ||
- "-F/System/Volumes/Data/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" | ||
headers: | ||
entry-points: | ||
- "/System/Volumes/Data/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Security.framework/Headers/SecItem.h" | ||
preamble: | | ||
// ignore_for_file: type=lint | ||
// ignore_for_file: return_of_invalid_type | ||
// ignore_for_file: unnecessary_non_null_assertion | ||
comments: | ||
style: any | ||
length: full | ||
library-imports: | ||
cf: "package:celest_auth/src/native/darwin/core_foundation.ffi.dart" | ||
|
||
ffi-native: | ||
exclude-all-by-default: true | ||
functions: | ||
include: | ||
- SecItemAdd | ||
- SecItemUpdate | ||
- SecItemCopyMatching | ||
- SecItemDelete | ||
- SecCopyErrorMessageString | ||
globals: | ||
include: | ||
- "kSec.*" | ||
- "kCF.*" | ||
unnamed-enums: | ||
include: | ||
- errSecSuccess | ||
- errSecItemNotFound | ||
- errSecDuplicateItem | ||
- errSecUserCanceled | ||
- errSecAuthFailed | ||
- errSecInteractionRequired | ||
- errSecMissingEntitlement | ||
- errSecInvalidOwnerEdit | ||
type-map: | ||
typedefs: | ||
CFString: | ||
lib: cf | ||
c-type: CFString | ||
dart-type: CFString | ||
CFType: | ||
lib: cf | ||
c-type: CFType | ||
dart-type: CFType | ||
CFData: | ||
lib: cf | ||
c-type: CFData | ||
dart-type: CFData | ||
CFDictionary: | ||
lib: cf | ||
c-type: CFDictionary | ||
dart-type: CFDictionary | ||
CFStringRef: | ||
lib: cf | ||
c-type: CFStringRef | ||
dart-type: CFStringRef | ||
CFTypeRef: | ||
lib: cf | ||
c-type: CFTypeRef | ||
dart-type: CFTypeRef | ||
CFDictionaryRef: | ||
lib: cf | ||
c-type: CFDictionaryRef | ||
dart-type: CFDictionaryRef |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.