Skip to content

Commit

Permalink
Merge pull request #33 from Liftric/feat/get-all-keys
Browse files Browse the repository at this point in the history
Feature: Update to Kotlin 1.6.20 and add getAllKeys method
  • Loading branch information
Jan authored Apr 11, 2022
2 parents efb24b8 + 94656db commit 53b0321
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 11 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: CI

on: [push]
on:
pull_request:
types: [ opened, reopened, synchronize ]
branches:
- master
push:
branches:
- master

jobs:
test:
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ val stringValue: String? = store.string(forKey = "PASSWORD")
val intValue: Int? = store.int(forKey = "SECRET")
```

To check if an object is in the Keychain you can also use:
To check if an object is stored you can use:

```kotlin
val existsObject: Boolean = store.existsObject(forKey = "PASSWORD")
```

To check for which keys objects are stored:

```kotlin
val allKeys: List<String> = store.allKeys()
```

### Deleting

#### Single object
Expand All @@ -82,10 +88,6 @@ val isRemoved: Boolean = store.removeObject(forKey = "PASSWORD")

#### All objects

##### iOS

⚠️ If the service name and the access group are not null, it will only delete the objects that match the query.

```kotlin
val isCleared: Boolean = store.clear()
```
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/src/main/java/Libs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ object Android {
}

object Versions {
const val Gradle = "7.1.1"
const val Kotlin = "1.6.10"
const val Gradle = "7.1.2"
const val Kotlin = "1.6.20"
const val Versioning = "2.15.1"
const val Crypto = "1.1.0-alpha03"
const val RoboElectric = "4.5.1"
Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
kotlin.code.style=official
android.useAndroidX=true
org.gradle.jvmargs=-Xmx4096m
kotlin.native.enableDependencyPropagation=false
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.mpp.stability.nowarn=true
kotlin.mpp.enableCompatibilityMetadataVariant=true
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
17 changes: 17 additions & 0 deletions iostests/Tests/KeychainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ class KeychainTests: XCTestCase {
}
}

func testSetGetAll() throws {
let boolData = ("bool", true)
sut.set(key: boolData.0, boolValue: boolData.1)

let doubleData = ("double", 0.0)
sut.set(key: doubleData.0, doubleValue: doubleData.1)

let longData = ("long", 1)
sut.set(key: longData.0, longValue: Int64(longData.1))

let allKeys = sut.allKeys()
XCTAssertEqual(allKeys.count, 3)
XCTAssertEqual(allKeys[0], boolData.0)
XCTAssertEqual(allKeys[1], doubleData.0)
XCTAssertEqual(allKeys[2], longData.0)
}

func testExistsObject() {
XCTAssertFalse(sut.existsObject(forKey: "blank"), "Blank should not exist")
sut.set(key: "blank", stringValue: "124")
Expand Down
8 changes: 8 additions & 0 deletions src/androidMain/kotlin/com/liftric/kvault/KVault.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ actual open class KVault(context: Context, fileName: String? = null) {
}
}

/**
* Returns all keys of the objects in the SharedPreferences.
* @return A list with all keys
*/
actual fun allKeys(): List<String> {
return encSharedPrefs.all.map { it.key }
}

/**
* Deletes object with the given key from the SharedPreferences.
* @param forKey The key to query
Expand Down
6 changes: 6 additions & 0 deletions src/commonMain/kotlin/com/liftric/kvault/KVault.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ expect open class KVault {
*/
fun bool(forKey: String): Boolean?

/**
* Returns all keys of the stored objects.
* @return A list with all keys
*/
fun allKeys(): List<String>

/**
* Deletes object with the given key from the store.
* @param forKey The key to query
Expand Down
18 changes: 18 additions & 0 deletions src/commonTest/kotlin/com/liftric/kvault/KVaultTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ abstract class AbstractKVaultTest(private val keychain: KVault) {
}
}

@Test
fun testSetGetAll() {
val boolData = Pair("bool", true)
keychain.set(boolData.first, boolData.second)

val doubleData = Pair("double", Double.MIN_VALUE)
keychain.set(doubleData.first, doubleData.second)

val longData = Pair("long", Long.MAX_VALUE)
keychain.set(longData.first, longData.second)

val allKeys = keychain.allKeys()
assertEquals(allKeys.size, 3)
assertEquals(allKeys[0], boolData.first)
assertEquals(allKeys[1], doubleData.first)
assertEquals(allKeys[2], longData.first)
}

@Test
fun testExistsObject() {
assertFalse(keychain.existsObject("blank"), "blank should not exist")
Expand Down
24 changes: 24 additions & 0 deletions src/iosMain/kotlin/com/liftric/kvault/KVault.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,30 @@ actual open class KVault(
return value(forKey)?.toNSNumber()?.boolValue
}

/**
* Returns all keys of the objects in the Keychain.
* @return A list with all keys
*/
@Suppress("UNCHECKED_CAST")
actual fun allKeys(): List<String> = context {
val query = query(
kSecClass to kSecClassGenericPassword,
kSecReturnAttributes to kCFBooleanTrue,
kSecMatchLimit to kSecMatchLimitAll
)

memScoped {
val result = alloc<CFTypeRefVar>()
val isValid = SecItemCopyMatching(query, result.ptr).validate()
if (isValid) {
val items = CFBridgingRelease(result.value) as? List<Map<String, Any>>
items?.mapNotNull { it["acct"] as? String } ?: listOf()
} else {
listOf()
}
}
}

/**
* Checks if object with the given key exists in the Keychain.
* @param forKey The key to query
Expand Down

0 comments on commit 53b0321

Please sign in to comment.