-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for DeviceToken cookie, and workaround for the idx cookie (…
- Loading branch information
1 parent
569f6fa
commit bd98eae
Showing
10 changed files
with
188 additions
and
8 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
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
82 changes: 82 additions & 0 deletions
82
Sources/OktaIdx/Extensions/InteractionCodeFlow+DeviceIdentifier.swift
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,82 @@ | ||
// | ||
// Copyright (c) 2022-Present, Okta, Inc. and/or its affiliates. All rights reserved. | ||
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") | ||
// | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// | ||
// See the License for the specific language governing permissions and limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import AuthFoundation | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
#endif | ||
|
||
#if os(watchOS) | ||
import WatchKit | ||
#endif | ||
|
||
extension Keychain { | ||
static let deviceIdentifierKey = "com.okta.mobile.deviceIdentifier" | ||
} | ||
|
||
extension InteractionCodeFlow { | ||
static var systemDeviceIdentifier: UUID? { | ||
#if canImport(UIKit) && (os(iOS) || os(macOS) || os(tvOS)) | ||
if let uuid = UIDevice.current.identifierForVendor { | ||
return uuid | ||
} | ||
#elseif os(watchOS) | ||
if let uuid = WKInterfaceDevice.current().identifierForVendor { | ||
return uuid | ||
} | ||
#endif | ||
|
||
return nil | ||
} | ||
|
||
static var keychainDeviceIdentifier: UUID? { | ||
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) | ||
if let data = try? Keychain.Search(account: Keychain.deviceIdentifierKey).get().value, | ||
let string = String(data: data, encoding: .utf8), | ||
let uuid = UUID(uuidString: string) | ||
{ | ||
return uuid | ||
} | ||
|
||
let uuid = UUID() | ||
guard let uuidData = uuid.uuidString.data(using: .utf8) else { | ||
return nil | ||
} | ||
|
||
do { | ||
_ = try Keychain.Item(account: Keychain.deviceIdentifierKey, | ||
value: uuidData).save() | ||
return uuid | ||
} catch { | ||
return nil | ||
} | ||
#else | ||
return nil | ||
#endif | ||
} | ||
|
||
/// Unique identifier for this device, encoded to limit the character count. This is used within | ||
/// an outgoing Cookie named `dt` to enable "Remember this device" trust options within OIE. | ||
static var deviceIdentifier: String? { | ||
guard var identifier = systemDeviceIdentifier ?? keychainDeviceIdentifier | ||
else { | ||
return nil | ||
} | ||
|
||
let data = Data(bytes: &identifier, count: 16) | ||
let deviceToken = data.base64EncodedString() | ||
|
||
return deviceToken | ||
} | ||
} |
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 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,56 @@ | ||
// | ||
// Copyright (c) 2022-Present, Okta, Inc. and/or its affiliates. All rights reserved. | ||
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") | ||
// | ||
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// | ||
// See the License for the specific language governing permissions and limitations under the License. | ||
// | ||
|
||
import XCTest | ||
@testable import OktaIdx | ||
|
||
#if canImport(UIKit) | ||
import UIKit | ||
#endif | ||
|
||
#if canImport(WatchKit) | ||
import WatchKit | ||
#endif | ||
|
||
final class DeviceIdentifierTests: XCTestCase { | ||
#if canImport(UIKit) && (os(iOS) || os(macOS) || os(tvOS)) | ||
func testSystemDeviceIdentifier() throws { | ||
XCTAssertEqual(InteractionCodeFlow.systemDeviceIdentifier, UIDevice.current.identifierForVendor) | ||
} | ||
#elseif canImport(WatchKit) | ||
func testSystemDeviceIdentifier() throws { | ||
XCTAssertEqual(InteractionCodeFlow.systemDeviceIdentifier, WKInterfaceDevice.current().identifierForVendor) | ||
} | ||
#endif | ||
|
||
#if canImport(UIKit) && (os(iOS) || os(macOS) || os(tvOS) || canImport(WatchKit)) | ||
func testDeviceIdentifier() throws { | ||
let identifier = try XCTUnwrap(InteractionCodeFlow.deviceIdentifier) | ||
|
||
// Device Token string _must_ be 32 characters or less. | ||
XCTAssertLessThanOrEqual(identifier.count, 32) | ||
|
||
let data = try XCTUnwrap(Data(base64Encoded: identifier, options: .ignoreUnknownCharacters)) | ||
|
||
var nsuuid: NSUUID? | ||
data.withUnsafeBytes { (unsafeBytes) in | ||
let bytes = unsafeBytes.bindMemory(to: UInt8.self).baseAddress! | ||
nsuuid = NSUUID(uuidBytes: bytes) | ||
} | ||
|
||
let uuidString = try XCTUnwrap(nsuuid?.uuidString) | ||
let uuid = try XCTUnwrap(UUID(uuidString: uuidString)) | ||
|
||
XCTAssertEqual(uuid, InteractionCodeFlow.systemDeviceIdentifier) | ||
} | ||
#endif | ||
} |
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