Skip to content

Commit

Permalink
Refactor and add secure storage module
Browse files Browse the repository at this point in the history
  • Loading branch information
dnys1 committed Mar 4, 2024
1 parent f7bebee commit 68f562a
Show file tree
Hide file tree
Showing 58 changed files with 17,657 additions and 63,241 deletions.
2 changes: 1 addition & 1 deletion packages/celest_auth/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ analyzer:
depend_on_referenced_packages: error
public_member_api_docs: ignore # TODO
exclude:
- lib/src/platform/**/*.ffi.dart
- "**/*.ffi.dart"
3 changes: 3 additions & 0 deletions packages/celest_auth/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@ dependencies {
implementation "androidx.biometric:biometric:1.2.0-alpha05"
implementation "com.google.android.gms:play-services-auth:21.0.0"
implementation "com.google.android.gms:play-services-fido:20.1.0"

// Secure Storage
implementation 'androidx.security:security-crypto:1.1.0-alpha06'
}
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()
}
}

}
1 change: 1 addition & 0 deletions packages/celest_auth/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ dependencies {
implementation "androidx.biometric:biometric:1.2.0-alpha05"
implementation "com.google.android.gms:play-services-auth:21.0.0"
implementation "com.google.android.gms:play-services-fido:20.1.0"
implementation 'androidx.security:security-crypto:1.1.0-alpha06'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<dict>
<key>com.apple.developer.associated-domains</key>
<array>
<string>webcredentials:a102-136-24-157-119.ngrok-free.app?developer=true</string>
<string>webcredentials:0a3b-136-24-157-119.ngrok-free.app?developer=true</string>
</array>
</dict>
</plist>
14 changes: 8 additions & 6 deletions packages/celest_auth/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import 'package:corks/corks.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

final authClient = AuthClient(
baseUri: Uri.https('a102-136-24-157-119.ngrok-free.app'),
);
final baseUri = Uri.https('0a3b-136-24-157-119.ngrok-free.app');
final authClient = AuthClient(baseUri: baseUri);
final passkeys = PasskeyPlatform(protocol: authClient.passkeys);

void main() {
Expand Down Expand Up @@ -46,6 +45,11 @@ class _MainAppState extends State<MainApp> {
final response = await passkeys.register(
PasskeyRegistrationRequest(
username: _controller.text,
authenticatorSelection: const AuthenticatorSelectionCriteria(
authenticatorAttachment: AuthenticatorAttachment.platform,
residentKey: ResidentKeyRequirement.preferred,
userVerification: UserVerificationRequirement.discouraged,
),
),
);
await authClient.passkeys.verifyRegistration(
Expand Down Expand Up @@ -143,9 +147,7 @@ class _MainAppState extends State<MainApp> {
onPressed: () {
setState(() {
_request = http.get(
Uri.parse(
'https://a102-136-24-157-119.ngrok-free.app/authenticated',
),
baseUri.resolve('/authenticated'),
);
});
},
Expand Down
59 changes: 0 additions & 59 deletions packages/celest_auth/ffigen.authentication_services.yaml

This file was deleted.

50 changes: 50 additions & 0 deletions packages/celest_auth/ffigen.core_foundation.yaml
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.*"
4 changes: 2 additions & 2 deletions packages/celest_auth/ffigen.darwin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: |
Regenerate bindings with `dart run ffigen --config=ffigen.darwin.yaml`.
language: "objc"
output:
bindings: "lib/src/platform/darwin/celest_auth.ffi.dart"
bindings: "lib/src/native/darwin/celest_auth.ffi.dart"
headers:
entry-points:
- "example/build/macos/Build/Products/Release/celest_auth/celest_auth.framework/Headers/celest_auth-Swift.h"
Expand All @@ -14,7 +14,7 @@ headers:
- "/System/Volumes/Data/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h"
import:
symbol-files:
- "package:celest_auth/src/platform/darwin/foundation.yaml"
- "package:celest_auth/src/native/darwin/foundation.yaml"
preamble: |
// ignore_for_file: type=lint
// ignore_for_file: return_of_invalid_type
Expand Down
6 changes: 3 additions & 3 deletions packages/celest_auth/ffigen.foundation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ description: |
Regenerate bindings with `dart run ffigen --config=ffigen.foundation.yaml`.
language: "objc"
output:
bindings: "lib/src/platform/darwin/foundation.ffi.dart"
bindings: "lib/src/native/darwin/foundation.ffi.dart"
symbol-file:
output: "package:celest_auth/src/platform/darwin/foundation.yaml"
import-path: "package:celest_auth/src/platform/darwin/foundation.ffi.dart"
output: "package:celest_auth/src/native/darwin/foundation.yaml"
import-path: "package:celest_auth/src/native/darwin/foundation.ffi.dart"
headers:
entry-points:
- "/System/Volumes/Data/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h"
Expand Down
76 changes: 76 additions & 0 deletions packages/celest_auth/ffigen.security.yaml
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
41 changes: 0 additions & 41 deletions packages/celest_auth/ffigen.uikit.yaml

This file was deleted.

Loading

0 comments on commit 68f562a

Please sign in to comment.