Skip to content

Commit

Permalink
Merge pull request #1893 from ably/fix/1892-deviceSecret-in-registration
Browse files Browse the repository at this point in the history
[ECO-4711] Added `deviceSecret` in device registration POST.
  • Loading branch information
maratal authored Mar 26, 2024
2 parents ecec03d + 2eaa256 commit 2fc86d7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Source/ARTJsonLikeEncoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ - (NSData *)encodeDeviceDetails:(ARTDeviceDetails *)deviceDetails error:(NSError
return [self encode:[self deviceDetailsToDictionary:deviceDetails] error:error];
}

- (NSData *)encodeLocalDevice:(ARTLocalDevice *)device error:(NSError **)error {
return [self encode:[self localDeviceToDictionary:device] error:error];
}

- (ARTDeviceDetails *)decodeDeviceDetails:(NSData *)data error:(NSError **)error {
return [self deviceDetailsFromDictionary:[self decodeDictionary:data error:nil] error:error];
}
Expand Down Expand Up @@ -627,7 +631,7 @@ - (NSDictionary *)tokenDetailsToDictionary:(ARTTokenDetails *)tokenDetails {
return dictionary;
}

- (NSDictionary *)deviceDetailsToDictionary:(ARTDeviceDetails *)deviceDetails {
- (NSMutableDictionary *)deviceDetailsToDictionary:(ARTDeviceDetails *)deviceDetails {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

dictionary[@"id"] = deviceDetails.id;
Expand All @@ -643,6 +647,12 @@ - (NSDictionary *)deviceDetailsToDictionary:(ARTDeviceDetails *)deviceDetails {
return dictionary;
}

- (NSDictionary *)localDeviceToDictionary:(ARTLocalDevice *)device {
NSMutableDictionary *dictionary = [self deviceDetailsToDictionary:device];
dictionary[@"deviceSecret"] = device.secret;
return dictionary;
}

- (ARTDeviceDetails *)deviceDetailsFromDictionary:(NSDictionary *)input error:(NSError * __autoreleasing *)error {
ARTLogVerbose(_logger, @"RS:%p ARTJsonLikeEncoder<%@>: deviceDetailsFromDictionary %@", _rest, [_delegate formatAsString], input);

Expand Down
3 changes: 2 additions & 1 deletion Source/ARTPushActivationStateMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ - (void)deviceRegistration:(ARTErrorInfo *)error {
// Asynchronous HTTP request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"/push/deviceRegistrations"]];
request.HTTPMethod = @"POST";
request.HTTPBody = [[self->_rest defaultEncoder] encodeDeviceDetails:local error:nil];
request.HTTPBody = [[self->_rest defaultEncoder] encodeLocalDevice:local error:nil];
[request setValue:[[self->_rest defaultEncoder] mimeType] forHTTPHeaderField:@"Content-Type"];

ARTLogDebug(self->_logger, @"%@: device registration with request %@", NSStringFromClass(self.class), request);
Expand Down Expand Up @@ -298,6 +298,7 @@ - (void)syncDevice {
request.HTTPMethod = @"PUT";
request.HTTPBody = [[self->_rest defaultEncoder] encodeDeviceDetails:local error:nil];
[request setValue:[[self->_rest defaultEncoder] mimeType] forHTTPHeaderField:@"Content-Type"];
[request setDeviceAuthentication:local];

ARTLogDebug(self->_logger, @"%@: sync device with request %@", NSStringFromClass(self.class), request);
[self->_rest executeRequest:request withAuthOption:ARTAuthenticationOn completion:^(NSHTTPURLResponse *response, NSData *data, NSError *error) {
Expand Down
4 changes: 4 additions & 0 deletions Source/include/Ably/ARTEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@class ARTDeviceIdentityTokenDetails;
@class ARTDevicePushDetails;
@class ARTPushChannelSubscription;
@class ARTLocalDevice;

@protocol ARTPushRecipient;

Expand Down Expand Up @@ -67,6 +68,9 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable NSData *)encodeDeviceDetails:(ARTDeviceDetails *)deviceDetails error:(NSError *_Nullable *_Nullable)error;
- (nullable ARTDeviceDetails *)decodeDeviceDetails:(NSData *)data error:(NSError *_Nullable *_Nullable)error;

// LocalDevice
- (nullable NSData *)encodeLocalDevice:(ARTLocalDevice *)device error:(NSError *_Nullable *_Nullable)error;

// ChannelDetails
- (nullable ARTChannelDetails *)decodeChannelDetails:(NSData *)data error:(NSError *_Nullable *_Nullable)error;

Expand Down
1 change: 1 addition & 0 deletions Test/Tests/PushActivationStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ class PushActivationStateMachineTests: XCTestCase {
XCTAssertEqual(url.host, rest.internal.options.restUrl().host)
XCTAssertEqual(request.httpMethod, "POST")
XCTAssertEqual(body.value(forKey: "id") as? String, rest.device.id)
XCTAssertEqual(body.value(forKey: "deviceSecret") as? String, rest.device.secret)
XCTAssertEqual(body.value(forKey: "push") as? [String: [String: AnyHashable]], expectedPushRecipient)
XCTAssertEqual(body.value(forKey: "formFactor") as? String, expectedFormFactor)
XCTAssertEqual(body.value(forKey: "platform") as? String, expectedPlatform)
Expand Down

0 comments on commit 2fc86d7

Please sign in to comment.