Skip to content

Commit

Permalink
Generate device id/secret pair so that id always not nil unless i…
Browse files Browse the repository at this point in the history
…ssue ably/specification#180 resolved.
  • Loading branch information
maratal committed Jan 17, 2024
1 parent 1873d81 commit 79e741b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
33 changes: 20 additions & 13 deletions Source/ARTLocalDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ - (instancetype)initWithStorage:(id<ARTDeviceStorage>)storage logger:(nullable A
return self;
}

- (void)generateAndPersistPairOfDeviceIdAndSecret {
self.id = [self.class generateId];
self.secret = [self.class generateSecret];

[_storage setObject:self.id forKey:ARTDeviceIdKey];
[_storage setSecret:self.secret forDevice:self.id];
}

+ (instancetype)deviceWithStorage:(id<ARTDeviceStorage>)storage logger:(nullable ARTInternalLog *)logger {
ARTLocalDevice *device = [[ARTLocalDevice alloc] initWithStorage:storage logger:logger];
device.platform = ARTDevicePlatform;
Expand All @@ -75,8 +83,13 @@ + (instancetype)deviceWithStorage:(id<ARTDeviceStorage>)storage logger:(nullable
NSString *deviceId = [storage objectForKey:ARTDeviceIdKey];
NSString *deviceSecret = deviceId == nil ? nil : [storage secretForDevice:deviceId];

device.id = deviceId ?: [self.class generateId]; // temporarily ignore RSH8a/b, see https://github.com/ably/ably-cocoa/pull/1847#discussion_r1441954284 thread
device.secret = deviceSecret;
if (deviceId == nil || deviceSecret == nil) {
[device generateAndPersistPairOfDeviceIdAndSecret]; // Should be removed later once spec issue #180 resolved.
}
else {
device.id = deviceId;
device.secret = deviceSecret;
}

id identityTokenDetailsInfo = [storage objectForKey:ARTDeviceIdentityTokenKey];
ARTDeviceIdentityTokenDetails *identityTokenDetails = [ARTDeviceIdentityTokenDetails unarchive:identityTokenDetailsInfo withLogger:logger];
Expand Down Expand Up @@ -105,25 +118,19 @@ - (void)setupDetailsWithClientId:(NSString *)clientId {
NSString *deviceId = self.id;
NSString *deviceSecret = self.secret;

if (deviceId == nil || deviceSecret == nil) { // generate both at the same time
deviceId = [self.class generateId];
deviceSecret = [self.class generateSecret];

[_storage setObject:deviceId forKey:ARTDeviceIdKey];
[_storage setSecret:deviceSecret forDevice:deviceId];
if (deviceId == nil || deviceSecret == nil) {
[self generateAndPersistPairOfDeviceIdAndSecret];
}

self.id = deviceId;
self.secret = deviceSecret;

self.clientId = clientId;
[_storage setObject:clientId forKey:ARTClientIdKey];
}

- (void)resetDetails {
self.secret = nil;
// Should be replaced later to resetting device's id/secret once spec issue #180 resolved.
[self generateAndPersistPairOfDeviceIdAndSecret];

self.clientId = nil;
[_storage setSecret:nil forDevice:self.id];
[_storage setObject:nil forKey:ARTClientIdKey];
[self setAndPersistIdentityTokenDetails:nil];
NSArray *supportedTokenTypes = @[
Expand Down
9 changes: 6 additions & 3 deletions Test/Tests/PushActivationStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class PushActivationStateMachineTests: XCTestCase {

let stateMachine = ARTPushActivationStateMachine(rest: rest.internal, delegate: StateMachineDelegate(), logger: .init(core: MockInternalLogCore()))

XCTAssertNil(rest.device.secret)
XCTAssertNil(rest.device.clientId)

stateMachine.send(ARTPushActivationEventCalledActivate())
Expand Down Expand Up @@ -877,15 +876,19 @@ class PushActivationStateMachineTests: XCTestCase {
XCTAssertTrue(resetDetailsCalled)

// RSH3g2a
XCTAssertNil(stateMachine.rest.device.secret)
XCTAssertNil(stateMachine.rest.device.identityTokenDetails)
XCTAssertNil(stateMachine.rest.device.clientId)
XCTAssertNil(stateMachine.rest.device.push.recipient["push"])

XCTAssertNil(storage.object(forKey: ARTDeviceIdKey))
XCTAssertNil(storage.object(forKey: ARTDeviceIdentityTokenKey))
XCTAssertNil(ARTLocalDevice.apnsDeviceToken(ofType: ARTAPNSDeviceDefaultTokenType, from: storage))
XCTAssertNil(ARTLocalDevice.apnsDeviceToken(ofType: ARTAPNSDeviceLocationTokenType, from: storage))

// Should be replaced with `nil` checks after issue https://github.com/ably/specification/issues/180 resolved
XCTAssertNotNil(stateMachine.rest.device.id)
XCTAssertNotNil(stateMachine.rest.device.secret)
XCTAssertEqual(storage.keysWritten[ARTDeviceIdKey] as? String, stateMachine.rest.device.id)
XCTAssertEqual(storage.keysWritten[ARTDeviceSecretKey] as? String, stateMachine.rest.device.secret)
}

// RSH3g3
Expand Down

0 comments on commit 79e741b

Please sign in to comment.