Skip to content
This repository was archived by the owner on Apr 7, 2021. It is now read-only.

Commit 6eac3c5

Browse files
committed
Remove KeychainAccess from SDK
1 parent d49959f commit 6eac3c5

File tree

7 files changed

+96
-118
lines changed

7 files changed

+96
-118
lines changed

Examples/Demo.xcodeproj/project.pbxproj

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
183FDA8F1E7510F10085589F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 183FDA8E1E7510F10085589F /* Assets.xcassets */; };
1414
183FDA921E7510F10085589F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 183FDA901E7510F10085589F /* LaunchScreen.storyboard */; };
1515
183FDA9D1E7510F10085589F /* DemoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 183FDA9C1E7510F10085589F /* DemoTests.swift */; };
16-
6538472725F35D6300A6D7D9 /* StudyplusSDK.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 658C822725F21569002291FE /* StudyplusSDK.framework */; };
1716
6538472825F35D6300A6D7D9 /* StudyplusSDK.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 658C822725F21569002291FE /* StudyplusSDK.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
17+
65D2B4CA25F35968002135D2 /* KeychainAccess in Frameworks */ = {isa = PBXBuildFile; productRef = 65D2B4C925F35968002135D2 /* KeychainAccess */; };
1818
/* End PBXBuildFile section */
1919

2020
/* Begin PBXContainerItemProxy section */
@@ -75,6 +75,7 @@
7575
buildActionMask = 2147483647;
7676
files = (
7777
6538472725F35D6300A6D7D9 /* StudyplusSDK.framework in Frameworks */,
78+
65D2B4CA25F35968002135D2 /* KeychainAccess in Frameworks */,
7879
);
7980
runOnlyForDeploymentPostprocessing = 0;
8081
};
@@ -164,6 +165,7 @@
164165
);
165166
name = Demo;
166167
packageProductDependencies = (
168+
65D2B4C925F35968002135D2 /* KeychainAccess */,
167169
);
168170
productName = Demo;
169171
productReference = 183FDA851E7510F00085589F /* Demo.app */;
@@ -565,6 +567,14 @@
565567
};
566568
};
567569
/* End XCRemoteSwiftPackageReference section */
570+
571+
/* Begin XCSwiftPackageProductDependency section */
572+
65D2B4C925F35968002135D2 /* KeychainAccess */ = {
573+
isa = XCSwiftPackageProductDependency;
574+
package = 65AEE17B25F2116C00821E1E /* XCRemoteSwiftPackageReference "KeychainAccess" */;
575+
productName = KeychainAccess;
576+
};
577+
/* End XCSwiftPackageProductDependency section */
568578
};
569579
rootObject = 183FDA5E1E7510920085589F /* Project object */;
570580
}

Lib/StudyplusSDK.xcodeproj/project.pbxproj

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 52;
6+
objectVersion = 46;
77
objects = {
88

99
/* Begin PBXBuildFile section */
@@ -16,7 +16,6 @@
1616
183FDAB11E754DCB0085589F /* StudyplusAPIRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 183FDAB01E754DCB0085589F /* StudyplusAPIRequest.swift */; };
1717
183FDAB31E7561B90085589F /* StudyplusRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 183FDAB21E7561B90085589F /* StudyplusRecord.swift */; };
1818
183FDAB51E7584C50085589F /* StudyplusRecordAmount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 183FDAB41E7584C50085589F /* StudyplusRecordAmount.swift */; };
19-
65AEE16D25F2111600821E1E /* KeychainAccess in Frameworks */ = {isa = PBXBuildFile; productRef = 65AEE16C25F2111600821E1E /* KeychainAccess */; };
2019
/* End PBXBuildFile section */
2120

2221
/* Begin PBXContainerItemProxy section */
@@ -49,7 +48,6 @@
4948
isa = PBXFrameworksBuildPhase;
5049
buildActionMask = 2147483647;
5150
files = (
52-
65AEE16D25F2111600821E1E /* KeychainAccess in Frameworks */,
5351
);
5452
runOnlyForDeploymentPostprocessing = 0;
5553
};
@@ -135,7 +133,6 @@
135133
);
136134
name = StudyplusSDK;
137135
packageProductDependencies = (
138-
65AEE16C25F2111600821E1E /* KeychainAccess */,
139136
);
140137
productName = StudyplusSDK;
141138
productReference = 183FDA671E7510920085589F /* StudyplusSDK.framework */;
@@ -191,7 +188,6 @@
191188
);
192189
mainGroup = 183FDA5D1E7510920085589F;
193190
packageReferences = (
194-
65AEE16B25F2111600821E1E /* XCRemoteSwiftPackageReference "KeychainAccess" */,
195191
);
196192
productRefGroup = 183FDA681E7510920085589F /* Products */;
197193
projectDirPath = "";
@@ -367,8 +363,7 @@
367363
MTL_ENABLE_DEBUG_INFO = NO;
368364
OTHER_SWIFT_FLAGS = "-D RELEASE";
369365
SDKROOT = iphoneos;
370-
SWIFT_COMPILATION_MODE = wholemodule;
371-
SWIFT_OPTIMIZATION_LEVEL = "-O";
366+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
372367
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
373368
TARGETED_DEVICE_FAMILY = "1,2";
374369
VALIDATE_PRODUCT = YES;
@@ -393,11 +388,7 @@
393388
INFOPLIST_FILE = StudyplusSDK/Info.plist;
394389
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
395390
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
396-
LD_RUNPATH_SEARCH_PATHS = (
397-
"$(inherited)",
398-
"@executable_path/Frameworks",
399-
"@loader_path/Frameworks",
400-
);
391+
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
401392
MARKETING_VERSION = 2.0.1;
402393
OTHER_SWIFT_FLAGS = "$(inherited)";
403394
PRODUCT_BUNDLE_IDENTIFIER = com.studyplus.StudyplusSDK;
@@ -423,11 +414,7 @@
423414
INFOPLIST_FILE = StudyplusSDK/Info.plist;
424415
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
425416
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
426-
LD_RUNPATH_SEARCH_PATHS = (
427-
"$(inherited)",
428-
"@executable_path/Frameworks",
429-
"@loader_path/Frameworks",
430-
);
417+
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
431418
MARKETING_VERSION = 2.0.1;
432419
OTHER_SWIFT_FLAGS = "$(inherited)";
433420
PRODUCT_BUNDLE_IDENTIFIER = com.studyplus.StudyplusSDK;
@@ -442,11 +429,7 @@
442429
buildSettings = {
443430
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
444431
INFOPLIST_FILE = StudyplusSDKTests/Info.plist;
445-
LD_RUNPATH_SEARCH_PATHS = (
446-
"$(inherited)",
447-
"@executable_path/Frameworks",
448-
"@loader_path/Frameworks",
449-
);
432+
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
450433
PRODUCT_BUNDLE_IDENTIFIER = com.studyplus.StudyplusSDKTests;
451434
PRODUCT_NAME = "$(TARGET_NAME)";
452435
SWIFT_VERSION = 5.0;
@@ -458,11 +441,7 @@
458441
buildSettings = {
459442
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
460443
INFOPLIST_FILE = StudyplusSDKTests/Info.plist;
461-
LD_RUNPATH_SEARCH_PATHS = (
462-
"$(inherited)",
463-
"@executable_path/Frameworks",
464-
"@loader_path/Frameworks",
465-
);
444+
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
466445
PRODUCT_BUNDLE_IDENTIFIER = com.studyplus.StudyplusSDKTests;
467446
PRODUCT_NAME = "$(TARGET_NAME)";
468447
SWIFT_VERSION = 5.0;
@@ -500,25 +479,6 @@
500479
defaultConfigurationName = Release;
501480
};
502481
/* End XCConfigurationList section */
503-
504-
/* Begin XCRemoteSwiftPackageReference section */
505-
65AEE16B25F2111600821E1E /* XCRemoteSwiftPackageReference "KeychainAccess" */ = {
506-
isa = XCRemoteSwiftPackageReference;
507-
repositoryURL = "https://github.com/kishikawakatsumi/KeychainAccess";
508-
requirement = {
509-
kind = upToNextMajorVersion;
510-
minimumVersion = 4.2.2;
511-
};
512-
};
513-
/* End XCRemoteSwiftPackageReference section */
514-
515-
/* Begin XCSwiftPackageProductDependency section */
516-
65AEE16C25F2111600821E1E /* KeychainAccess */ = {
517-
isa = XCSwiftPackageProductDependency;
518-
package = 65AEE16B25F2111600821E1E /* XCRemoteSwiftPackageReference "KeychainAccess" */;
519-
productName = KeychainAccess;
520-
};
521-
/* End XCSwiftPackageProductDependency section */
522482
};
523483
rootObject = 183FDA5E1E7510920085589F /* Project object */;
524484
}

Lib/StudyplusSDK.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 0 additions & 16 deletions
This file was deleted.

Lib/StudyplusSDK/Studyplus.swift

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
// THE SOFTWARE.
2626

2727
import UIKit
28-
import KeychainAccess
2928

3029
/**
3130
The class for using Studyplus.
@@ -78,6 +77,9 @@ final public class Studyplus {
7877

7978
private let accessTokenStoreKey: String = "accessToken"
8079
private let usernameStoreKey: String = "username"
80+
private var serviceName: String {
81+
return "Studyplus_iOS_SDK_\(consumerKey)"
82+
}
8183

8284
/// Opens the login screen by invoking the Studyplus application.
8385
/// If Studyplus app is not installed, open the Studyplus page in AppStore.
@@ -94,13 +96,7 @@ final public class Studyplus {
9496
///
9597
/// Studyplusアプリとの連携を解除します。
9698
public func logout() {
97-
98-
let chain = keychain()
99-
do {
100-
try chain.remove(usernameStoreKey)
101-
try chain.remove(accessTokenStoreKey)
102-
} catch {
103-
}
99+
deleteKey()
104100
}
105101

106102
/// Returns to whether or not it is connected with Studyplus application.
@@ -118,12 +114,22 @@ final public class Studyplus {
118114
///
119115
/// - Returns: accessToken
120116
public func accessToken() -> String? {
121-
122-
do {
123-
return try keychain().get(accessTokenStoreKey)
124-
} catch {
117+
let query = [
118+
kSecClass: kSecClassGenericPassword,
119+
kSecAttrService: serviceName,
120+
kSecAttrSynchronizable: kSecAttrSynchronizableAny,
121+
kSecMatchLimit: kSecMatchLimitOne,
122+
kSecReturnData: true,
123+
kSecAttrAccount: accessTokenStoreKey
124+
] as CFDictionary
125+
126+
var item: CFTypeRef?
127+
let status = SecItemCopyMatching(query, &item)
128+
guard status == errSecSuccess, let data = item as? Data else {
125129
return nil
126130
}
131+
132+
return String(data: data, encoding: .utf8)
127133
}
128134

129135
/// Username of Studyplus account. It is set when the auth or login is successful.
@@ -132,12 +138,22 @@ final public class Studyplus {
132138
///
133139
/// - Returns: username
134140
public func username() -> String? {
135-
136-
do {
137-
return try keychain().get(usernameStoreKey)
138-
} catch {
141+
let query = [
142+
kSecClass: kSecClassGenericPassword,
143+
kSecAttrService: serviceName,
144+
kSecAttrSynchronizable: kSecAttrSynchronizableAny,
145+
kSecMatchLimit: kSecMatchLimitOne,
146+
kSecReturnData: true,
147+
kSecAttrAccount: usernameStoreKey
148+
] as CFDictionary
149+
150+
var item: CFTypeRef?
151+
let status = SecItemCopyMatching(query, &item)
152+
guard status == errSecSuccess, let data = item as? Data else {
139153
return nil
140154
}
155+
156+
return String(data: data, encoding: .utf8)
141157
}
142158

143159
/// Posts a new study record to Studyplus.
@@ -148,7 +164,9 @@ final public class Studyplus {
148164
/// - studyRecord: see StudyplusRecord
149165
/// - success: callback when success to post the studyRecord
150166
/// - failure: callback when failure to post the studyRecord
151-
public func post(studyRecord: StudyplusRecord, success: @escaping () -> Void, failure: @escaping (_ error: StudyplusError) -> Void) {
167+
public func post(studyRecord: StudyplusRecord,
168+
success: @escaping () -> Void,
169+
failure: @escaping (_ error: StudyplusError) -> Void) {
152170

153171
guard StudyplusRecord.durationRange ~= Int(studyRecord.duration) else {
154172
failure(.postRecordFailed)
@@ -204,19 +222,36 @@ final public class Studyplus {
204222

205223
switch appDelegateUrl.pathComponents[1] {
206224
case "success":
207-
208-
let accessToken: String = appDelegateUrl.pathComponents[2].trimmingCharacters(in: .whitespacesAndNewlines)
209-
let username: String = appDelegateUrl.pathComponents[3].trimmingCharacters(in: .whitespacesAndNewlines)
210-
211-
let chain = keychain()
212-
do {
213-
try chain.set(accessToken, key: accessTokenStoreKey)
214-
try chain.set(username, key: usernameStoreKey)
225+
let accessToken: Data = appDelegateUrl
226+
.pathComponents[2]
227+
.trimmingCharacters(in: .whitespacesAndNewlines)
228+
.data(using: .utf8, allowLossyConversion: false)!
229+
let username: Data = appDelegateUrl
230+
.pathComponents[3]
231+
.trimmingCharacters(in: .whitespacesAndNewlines)
232+
.data(using: .utf8, allowLossyConversion: false)!
233+
234+
deleteKey()
235+
let statusAccessToken = SecItemAdd([
236+
kSecClass: kSecClassGenericPassword,
237+
kSecAttrService: serviceName,
238+
kSecAttrSynchronizable: kSecAttrSynchronizableAny,
239+
kSecAttrAccount: accessTokenStoreKey,
240+
kSecValueData: accessToken
241+
] as CFDictionary, nil)
242+
let statusUsername = SecItemAdd([
243+
kSecClass: kSecClassGenericPassword,
244+
kSecAttrService: serviceName,
245+
kSecAttrSynchronizable: kSecAttrSynchronizableAny,
246+
kSecAttrAccount: usernameStoreKey,
247+
kSecValueData: username
248+
] as CFDictionary, nil)
249+
250+
if statusAccessToken == noErr && statusUsername == noErr {
215251
delegate?.studyplusDidSuccessToLogin()
216-
} catch {
252+
} else {
217253
delegate?.studyplusDidFailToLogin(error: .unknownReason("Could not access Keychain."))
218254
}
219-
220255
case "fail":
221256

222257
if let errorCode: Int = Int(appDelegateUrl.pathComponents[2]) {
@@ -231,7 +266,7 @@ final public class Studyplus {
231266

232267
default:
233268
#if DEBUG
234-
print("StudyplusSDK: Unknown format: \(appDelegateUrl.absoluteString)")
269+
print("StudyplusSDK: Unknown format: \(appDelegateUrl.absoluteString)")
235270
#endif
236271
return false
237272
}
@@ -290,11 +325,6 @@ final public class Studyplus {
290325
self.consumerSecret = consumerSecret
291326
}
292327

293-
private func keychain() -> Keychain {
294-
let serviceName: String = "Studyplus_iOS_SDK_\(consumerKey)"
295-
return Keychain(service: serviceName)
296-
}
297-
298328
private func openStudyplus(command: String) {
299329

300330
guard UIApplication.shared.canOpenURL(URL(string: "studyplus://")!) else {
@@ -335,4 +365,12 @@ final public class Studyplus {
335365

336366
return true
337367
}
368+
369+
private func deleteKey() {
370+
SecItemDelete([
371+
kSecClass: kSecClassGenericPassword,
372+
kSecAttrService: serviceName,
373+
kSecAttrSynchronizable: kSecAttrSynchronizableAny
374+
] as CFDictionary)
375+
}
338376
}

Package.resolved

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)