Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login: Handle well-known data in the login response #640

Merged
merged 11 commits into from
Mar 1, 2019
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Changes in Matrix iOS SDK in 0.12.3 (2019-02-)

Improvements:
* Maintenance: Update cocopoads and pods. Automatic update to Swift4.2.
* MXCredentials: Create a new data model for it, separated from the CS API response data model (new MXLoginResponse class).
* Login: Handle well-known data in the login response - MSC1730 (vector-im/riot-ios/issues/2298).

Bug Fix:
* Crypto: Fix crash in MXKeyBackup (vector-im/riot-ios/issues/#2281).
Expand Down
32 changes: 32 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
32322A4C1E575F65005DD155 /* MXAllowedCertificates.m in Sources */ = {isa = PBXBuildFile; fileRef = 32322A4A1E575F65005DD155 /* MXAllowedCertificates.m */; };
3233606F1A403A0D0071A488 /* MXFileStore.h in Headers */ = {isa = PBXBuildFile; fileRef = 3233606D1A403A0D0071A488 /* MXFileStore.h */; settings = {ATTRIBUTES = (Public, ); }; };
323360701A403A0D0071A488 /* MXFileStore.m in Sources */ = {isa = PBXBuildFile; fileRef = 3233606E1A403A0D0071A488 /* MXFileStore.m */; };
323547D42226D3F500F15F94 /* MXWellKnown.m in Sources */ = {isa = PBXBuildFile; fileRef = 323547D22226D3F500F15F94 /* MXWellKnown.m */; };
323547D52226D3F500F15F94 /* MXWellKnown.h in Headers */ = {isa = PBXBuildFile; fileRef = 323547D32226D3F500F15F94 /* MXWellKnown.h */; settings = {ATTRIBUTES = (Public, ); }; };
323547D82226D5D600F15F94 /* MXWellKnownBaseConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = 323547D62226D5D600F15F94 /* MXWellKnownBaseConfig.h */; settings = {ATTRIBUTES = (Public, ); }; };
323547D92226D5D600F15F94 /* MXWellKnownBaseConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 323547D72226D5D600F15F94 /* MXWellKnownBaseConfig.m */; };
323547DC2226FC5700F15F94 /* MXCredentials.h in Headers */ = {isa = PBXBuildFile; fileRef = 323547DA2226FC5700F15F94 /* MXCredentials.h */; settings = {ATTRIBUTES = (Public, ); }; };
323547DD2226FC5700F15F94 /* MXCredentials.m in Sources */ = {isa = PBXBuildFile; fileRef = 323547DB2226FC5700F15F94 /* MXCredentials.m */; };
323C5A081A70E53500FB0549 /* MXToolsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 323C5A071A70E53500FB0549 /* MXToolsTests.m */; };
323E0C5B1A306D7A00A31D73 /* MXEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = 323E0C591A306D7A00A31D73 /* MXEvent.h */; settings = {ATTRIBUTES = (Public, ); }; };
323E0C5C1A306D7A00A31D73 /* MXEvent.m in Sources */ = {isa = PBXBuildFile; fileRef = 323E0C5A1A306D7A00A31D73 /* MXEvent.m */; };
Expand Down Expand Up @@ -436,6 +442,12 @@
32322A4A1E575F65005DD155 /* MXAllowedCertificates.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXAllowedCertificates.m; sourceTree = "<group>"; };
3233606D1A403A0D0071A488 /* MXFileStore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXFileStore.h; sourceTree = "<group>"; };
3233606E1A403A0D0071A488 /* MXFileStore.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXFileStore.m; sourceTree = "<group>"; };
323547D22226D3F500F15F94 /* MXWellKnown.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXWellKnown.m; sourceTree = "<group>"; };
323547D32226D3F500F15F94 /* MXWellKnown.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXWellKnown.h; sourceTree = "<group>"; };
323547D62226D5D600F15F94 /* MXWellKnownBaseConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXWellKnownBaseConfig.h; sourceTree = "<group>"; };
323547D72226D5D600F15F94 /* MXWellKnownBaseConfig.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXWellKnownBaseConfig.m; sourceTree = "<group>"; };
323547DA2226FC5700F15F94 /* MXCredentials.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXCredentials.h; sourceTree = "<group>"; };
323547DB2226FC5700F15F94 /* MXCredentials.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MXCredentials.m; sourceTree = "<group>"; };
323C5A071A70E53500FB0549 /* MXToolsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXToolsTests.m; sourceTree = "<group>"; };
323E0C591A306D7A00A31D73 /* MXEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXEvent.h; sourceTree = "<group>"; };
323E0C5A1A306D7A00A31D73 /* MXEvent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXEvent.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -824,6 +836,8 @@
320BBF3A1D6C7D9D0079890E /* EventsEnumerator */,
323F3F9020D3F0C700D26D6A /* Filters */,
32114A831A262CE000FF2EC4 /* Store */,
323547DA2226FC5700F15F94 /* MXCredentials.h */,
323547DB2226FC5700F15F94 /* MXCredentials.m */,
F0173EAA1FCF0E8800B5F6A3 /* MXGroup.h */,
F0173EAB1FCF0E8900B5F6A3 /* MXGroup.m */,
320DFDCA19DD99B60068622A /* MXRoom.h */,
Expand Down Expand Up @@ -972,6 +986,17 @@
path = MXFileStore;
sourceTree = "<group>";
};
323547D12226D3F500F15F94 /* AutoDiscovery */ = {
isa = PBXGroup;
children = (
323547D62226D5D600F15F94 /* MXWellKnownBaseConfig.h */,
323547D72226D5D600F15F94 /* MXWellKnownBaseConfig.m */,
323547D32226D3F500F15F94 /* MXWellKnown.h */,
323547D22226D3F500F15F94 /* MXWellKnown.m */,
);
path = AutoDiscovery;
sourceTree = "<group>";
};
323F3F9020D3F0C700D26D6A /* Filters */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1059,6 +1084,7 @@
3281E8B219E42DFE00976E1A /* JSONModels */ = {
isa = PBXGroup;
children = (
323547D12226D3F500F15F94 /* AutoDiscovery */,
3275FD9121A6B46600B9C13D /* Authentication */,
3281E8B319E42DFE00976E1A /* MXJSONModel.h */,
3281E8B419E42DFE00976E1A /* MXJSONModel.m */,
Expand Down Expand Up @@ -1554,6 +1580,7 @@
32954019216385F100E300FC /* MXServerNoticeContent.h in Headers */,
327187851DA7D0220071C818 /* MXOlmDecryption.h in Headers */,
32D7767D1A27860600FC4AA2 /* MXMemoryStore.h in Headers */,
323547D52226D3F500F15F94 /* MXWellKnown.h in Headers */,
32FFB4F0217E146A00C96002 /* MXRecoveryKey.h in Headers */,
32CE6FB81A409B1F00317F1E /* MXFileStoreMetaData.h in Headers */,
32A31BC820D401FC005916C7 /* MXRoomFilter.h in Headers */,
Expand Down Expand Up @@ -1583,6 +1610,7 @@
32B76EA320FDE2BE00B095F6 /* MXRoomMembersCount.h in Headers */,
32C6F93319DD814400EA4E9C /* MatrixSDK.h in Headers */,
324BE46C1E422766008D99D4 /* MXMegolmSessionData.h in Headers */,
323547DC2226FC5700F15F94 /* MXCredentials.h in Headers */,
021AFBA62179E91900742B2C /* MXEncryptedContentKey.h in Headers */,
327187891DA7DCE50071C818 /* MXOlmEncryption.h in Headers */,
32A1514E1DAF897600400192 /* MXOlmSessionResult.h in Headers */,
Expand Down Expand Up @@ -1623,6 +1651,7 @@
32A31BC420D3FFB0005916C7 /* MXFilter.h in Headers */,
32A151481DAF7C0C00400192 /* MXKey.h in Headers */,
32BBAE6D2178E99100D85F46 /* MXKeyBackupVersion.h in Headers */,
323547D82226D5D600F15F94 /* MXWellKnownBaseConfig.h in Headers */,
32A151461DAF7C0C00400192 /* MXDeviceInfo.h in Headers */,
32CAB1071A91EA34008C5BB9 /* MXPushRuleRoomMemberCountConditionChecker.h in Headers */,
C61A4CC41E5F38CB00442158 /* SwiftMatrixSDK.h in Headers */,
Expand Down Expand Up @@ -1926,6 +1955,7 @@
02CAD43A217DD12F0074700B /* MXContentScanEncryptedBody.m in Sources */,
F03EF5091DF071D5009DF592 /* MXEncryptedAttachments.m in Sources */,
B172857D2100D4F60052C51E /* MXSendReplyEventDefaultStringLocalizations.m in Sources */,
323547DD2226FC5700F15F94 /* MXCredentials.m in Sources */,
32FA10CF1FA1C9F700E54233 /* MXOutgoingRoomKeyRequest.m in Sources */,
32322A4C1E575F65005DD155 /* MXAllowedCertificates.m in Sources */,
F03EF5051DF01596009DF592 /* MXLRUCache.m in Sources */,
Expand All @@ -1934,6 +1964,7 @@
3275FD9921A6B53300B9C13D /* MXLoginPolicyData.m in Sources */,
3240969E1F9F751600DBA607 /* MXPushRuleSenderNotificationPermissionConditionChecker.m in Sources */,
71DE22E01BC7C51200284153 /* MXReceiptData.m in Sources */,
323547D92226D5D600F15F94 /* MXWellKnownBaseConfig.m in Sources */,
327E37B71A974F75007F026F /* MXLogger.m in Sources */,
324BE4691E3FADB1008D99D4 /* MXMegolmExportEncryption.m in Sources */,
32A31BBF20D3F2EC005916C7 /* MXFilterObject.m in Sources */,
Expand Down Expand Up @@ -1981,6 +2012,7 @@
C6F935891E5B3BE600FC34BF /* MXEventTimeline.swift in Sources */,
C6F9358B1E5B3BE600FC34BF /* MXJSONModels.swift in Sources */,
32FA10C21FA1C9EE00E54233 /* MXOutgoingRoomKeyRequestManager.m in Sources */,
323547D42226D3F500F15F94 /* MXWellKnown.m in Sources */,
320DFDE519DD99B60068622A /* MXRestClient.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 1 addition & 0 deletions MatrixSDK/Crypto/Data/Store/MXCryptoStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#ifdef MX_CRYPTO

#import "MXJSONModels.h"
#import "MXCredentials.h"

#import <OLMKit/OLMKit.h>
#import "MXOlmSession.h"
Expand Down
94 changes: 94 additions & 0 deletions MatrixSDK/Data/MXCredentials.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
Copyright 2019 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with 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/Foundation.h>

@class MXLoginResponse;

NS_ASSUME_NONNULL_BEGIN

/**
The `MXCredentials` class contains credentials to communicate with the Matrix
Client-Server API.
*/
@interface MXCredentials : NSObject

/**
The homeserver url (ex: "https://matrix.org").
*/
@property (nonatomic, nullable) NSString *homeServer;

/**
The identity server url (ex: "https://vector.im").
*/
@property (nonatomic, nullable) NSString *identityServer;

/**
The obtained user id.
*/
@property (nonatomic, nullable) NSString *userId;

/**
The access token to create a MXRestClient
*/
@property (nonatomic, nullable) NSString *accessToken;

/**
The device id.
*/
@property (nonatomic, nullable) NSString *deviceId;

/**
The homeserver name (ex: "matrix.org").
*/
- (nullable NSString *)homeServerName;

/**
The server certificate trusted by the user (nil when the server is trusted by the device).
*/
@property (nonatomic, nullable) NSData *allowedCertificate;

/**
The ignored server certificate (set when the user ignores a certificate change).
*/
@property (nonatomic, nullable) NSData *ignoredCertificate;


/**
Simple MXCredentials construtor

@param homeServer the homeserver URL.
@param userId the user id.
@param accessToken the user access token.
@return a MXCredentials instance.
*/
- (instancetype)initWithHomeServer:(NSString*)homeServer
userId:(nullable NSString*)userId
accessToken:(nullable NSString*)accessToken;

/**
Create credentials from a login or register response.

@param loginResponse the login or register response.
@param defaultCredentials credentials to use if loginResponse data cannot be trusted or missing.
@return a MXCredentials instance.
*/
- (instancetype)initWithLoginResponse:(MXLoginResponse*)loginResponse
andDefaultCredentials:(nullable MXCredentials*)defaultCredentials;

@end

NS_ASSUME_NONNULL_END
69 changes: 69 additions & 0 deletions MatrixSDK/Data/MXCredentials.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2019 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with 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 "MXCredentials.h"

#import "MXJSONModels.h"

@implementation MXCredentials

- (instancetype)initWithHomeServer:(NSString *)homeServer userId:(NSString *)userId accessToken:(NSString *)accessToken
{
self = [super init];
if (self)
{
_homeServer = [homeServer copy];
_userId = [userId copy];
_accessToken = [accessToken copy];
}
return self;
}

- (instancetype)initWithLoginResponse:(MXLoginResponse*)loginResponse
andDefaultCredentials:(MXCredentials*)defaultCredentials
{
self = [super init];
if (self)
{
_userId = loginResponse.userId;
_accessToken = loginResponse.accessToken;
_deviceId = loginResponse.deviceId;

// Use wellknown data first
_homeServer = loginResponse.wellknown.homeServer.baseUrl;
_identityServer = loginResponse.wellknown.homeServer.baseUrl;

if (!_homeServer)
{
// Workaround: HS does not return the right URL in loginResponse.homeserver.
// Use the passed one instead
_homeServer = [defaultCredentials.homeServer copy];
}

if (!_identityServer)
{
_identityServer = [defaultCredentials.identityServer copy];
}
}
return self;
}

- (NSString *)homeServerName
{
return [NSURL URLWithString:_homeServer].host;
}

@end
2 changes: 1 addition & 1 deletion MatrixSDK/Data/Store/MXStore.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

#import "MXEnumConstants.h"
#import "MXJSONModels.h"
#import "MXCredentials.h"
#import "MXEvent.h"
#import "MXReceiptData.h"
#import "MXUser.h"
Expand Down
44 changes: 44 additions & 0 deletions MatrixSDK/JSONModels/AutoDiscovery/MXWellKnown.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2019 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with 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/Foundation.h>

#import "MXJSONModel.h"
#import "MXWellKnownBaseConfig.h"

NS_ASSUME_NONNULL_BEGIN

/**
* https://matrix.org/docs/spec/client_server/r0.4.0.html#server-discovery
*
* {
* "m.homeserver": {
* "base_url": "https://matrix.org"
* },
* "m.identity_server": {
* "base_url": "https://vector.im"
* }
* }
*/
@interface MXWellKnown : MXJSONModel

@property (nonatomic) MXWellKnownBaseConfig *homeServer;

@property (nonatomic, nullable) MXWellKnownBaseConfig *identityServer;

@end

NS_ASSUME_NONNULL_END
38 changes: 38 additions & 0 deletions MatrixSDK/JSONModels/AutoDiscovery/MXWellKnown.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2019 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with 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 "MXWellKnown.h"

@implementation MXWellKnown

+ (instancetype)modelFromJSON:(NSDictionary *)JSONDictionary
{
MXWellKnown *wellknown;

MXWellKnownBaseConfig *homeServerBaseConfig;
MXJSONModelSetMXJSONModel(homeServerBaseConfig, MXWellKnownBaseConfig, JSONDictionary[@"m.homeserver"]);
if (homeServerBaseConfig)
{
wellknown = [MXWellKnown new];
wellknown.homeServer = homeServerBaseConfig;

MXJSONModelSetMXJSONModel(wellknown.identityServer, MXWellKnownBaseConfig, JSONDictionary[@"m.identity_server"]);
}

return wellknown;
}

@end
Loading