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

Add ability to disable all Identity Server features #705

Merged
merged 12 commits into from
Aug 14, 2019
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changes in Matrix iOS SDK in 0.13.2 (2019-08-)

Improvements:
* MXServiceTerms: A class to support MSC2140 (Terms of Service API) (vector-im/riot-ios#2600).
* MXRestClient: Remove Identity Server URL fallback to homeserver one's when there is no Identity Server configured.

Changes in Matrix iOS SDK in 0.13.1 (2019-08-08)
===============================================
Expand Down
1 change: 0 additions & 1 deletion MatrixSDK/Data/MXCredentials.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ - (instancetype)initWithLoginResponse:(MXLoginResponse*)loginResponse

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

if (!_homeServer)
{
Expand Down
4 changes: 0 additions & 4 deletions MatrixSDK/MXError.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ FOUNDATION_EXPORT NSString *const kMXErrCodeStringTermsNotSigned;

FOUNDATION_EXPORT NSString *const kMXErrorStringInvalidToken;

/**
Error codes generated by the Matrix SDK.
*/
FOUNDATION_EXPORT NSString *const kMXSDKErrCodeStringMissingParameters;

/**
Keys and values that can be found in a Matrix error JSON dictionary.
Expand Down
2 changes: 0 additions & 2 deletions MatrixSDK/MXError.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@

NSString *const kMXErrorStringInvalidToken = @"Invalid token";

NSString *const kMXSDKErrCodeStringMissingParameters = @"org.matrix.sdk.missing_parameters";

NSString *const kMXErrorCodeKey = @"errcode";
NSString *const kMXErrorMessageKey = @"error";
NSString *const kMXErrorConsentNotGivenConsentURIKey = @"consent_uri";
Expand Down
12 changes: 12 additions & 0 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ FOUNDATION_EXPORT NSString *const kMXAccountDataKeyIgnoredUser;
*/
FOUNDATION_EXPORT NSString *const kMXRestClientErrorDomain;

/**
MXRestClient errors
*/

NS_ERROR_ENUM(kMXRestClientErrorDomain)
{
MXRestClientErrorUnknown,
MXRestClientErrorInvalidParameters,
MXRestClientErrorInvalidContentURI,
MXRestClientErrorMissingIdentityServer
};

/**
Parameters that can be used in [MXRestClient membersOfRoom:withParameters:...].
*/
Expand Down
107 changes: 79 additions & 28 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,8 @@ -(id)initWithCredentials:(MXCredentials*)inCredentials andOnUnrecognizedCertific
}
}];
}


if (self.credentials.identityServer)
{
self.identityServer = self.credentials.identityServer;
}
else if (self.credentials.homeServer)
{
// By default, use the same address for the identity server
self.identityServer = self.credentials.homeServer;
}

self.identityServer = self.credentials.identityServer;

completionQueue = dispatch_get_main_queue();

Expand Down Expand Up @@ -618,7 +609,7 @@ - (MXHTTPOperation*)resetPasswordWithParameters:(NSDictionary*)parameters
// sanity check
if (!parameters)
{
NSError* error = [NSError errorWithDomain:@"Invalid params" code:500 userInfo:nil];
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorInvalidParameters userInfo:nil];

[self dispatchFailure:error inBlock:failure];
return nil;
Expand All @@ -645,7 +636,7 @@ - (MXHTTPOperation*)changePassword:(NSString*)oldPassword with:(NSString*)newPas
// sanity check
if (!oldPassword || !newPassword)
{
NSError* error = [NSError errorWithDomain:@"Invalid params" code:500 userInfo:nil];
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorInvalidParameters userInfo:nil];

[self dispatchFailure:error inBlock:failure];
return nil;
Expand Down Expand Up @@ -777,7 +768,7 @@ - (MXHTTPOperation*)deactivateAccountWithAuthParameters:(NSDictionary*)authParam
// authParameters are required
if (!authParameters)
{
NSError* error = [NSError errorWithDomain:@"Invalid params" code:500 userInfo:nil];
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorInvalidParameters userInfo:nil];

[self dispatchFailure:error inBlock:failure];
return nil;
Expand Down Expand Up @@ -1057,7 +1048,7 @@ - (MXHTTPOperation*)setPusherWithPushkey:(NSString *)pushkey
// sanity check
if (!pushkey || !kind || !appDisplayName || !deviceDisplayName || !profileTag || !lang || !data)
{
NSError* error = [NSError errorWithDomain:@"Invalid params" code:500 userInfo:nil];
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorInvalidParameters userInfo:nil];

NSLog(@"[MXRestClient] setPusherWithPushkey: Error: Invalid params: ");

Expand Down Expand Up @@ -1312,7 +1303,8 @@ - (MXHTTPOperation *)addPushRule:(NSString*)ruleId
}
else
{
[self dispatchFailure:[NSError errorWithDomain:kMXRestClientErrorDomain code:0 userInfo:@{@"error": @"Invalid argument"}] inBlock:failure];
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorInvalidParameters userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}
}
Expand Down Expand Up @@ -1974,8 +1966,8 @@ - (MXHTTPOperation*)inviteByThreePid:(NSString*)medium
// The identity server must be defined
if (!self.credentials.identityServer)
{
MXError *error = [[MXError alloc] initWithErrorCode:kMXSDKErrCodeStringMissingParameters error:@"No supplied identity server URL"];
[self dispatchFailure:[error createNSError] inBlock:failure];
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

Expand Down Expand Up @@ -2863,6 +2855,9 @@ - (MXHTTPOperation*)add3PID:(NSString*)sid
if (!self.credentials.identityServer)
{
NSLog(@"[MXRestClient] add3PID: Error: Missing identityServer");
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

NSURL *identityServerURL = [NSURL URLWithString:self.credentials.identityServer];
Expand Down Expand Up @@ -3363,12 +3358,20 @@ - (MXHTTPOperation*) uploadContent:(NSData *)data
#pragma mark - Identity server API
- (void)setIdentityServer:(NSString *)identityServer
{
self.credentials.identityServer = [identityServer copy];
identityHttpClient = [[MXHTTPClient alloc] initWithBaseURL:[NSString stringWithFormat:@"%@/%@", identityServer, kMXIdentityAPIPrefixPathV1]
andOnUnrecognizedCertificateBlock:nil];

// The identity server accepts parameters in form data form not in JSON
identityHttpClient.requestParametersInJSON = NO;
if (identityServer.length)
{
self.credentials.identityServer = [identityServer copy];
identityHttpClient = [[MXHTTPClient alloc] initWithBaseURL:[NSString stringWithFormat:@"%@/%@", identityServer, kMXIdentityAPIPrefixPathV1]
andOnUnrecognizedCertificateBlock:nil];

// The identity server accepts parameters in form data form not in JSON
identityHttpClient.requestParametersInJSON = NO;
}
else
{
self.credentials.identityServer = nil;
identityHttpClient = nil;
}
}

- (NSString *)identityServer
Expand All @@ -3379,6 +3382,13 @@ - (NSString *)identityServer

- (MXHTTPOperation *)pingIdentityServer:(void (^)(void))success failure:(void (^)(NSError *))failure
{
if (!identityHttpClient)
{
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

// We cannot use "" as the HTTP client (AFNetworking) will request for "/v1/"
NSString *path = @"../v1";

Expand All @@ -3404,6 +3414,13 @@ - (MXHTTPOperation*)lookup3pid:(NSString*)address
success:(void (^)(NSString *userId))success
failure:(void (^)(NSError *error))failure
{
if (!identityHttpClient)
{
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

return [identityHttpClient requestWithMethod:@"GET"
path:@"lookup"
parameters:@{
Expand All @@ -3430,6 +3447,13 @@ - (MXHTTPOperation*)lookup3pids:(NSArray*)threepids
success:(void (^)(NSArray *discoveredUsers))success
failure:(void (^)(NSError *error))failure
{
if (!identityHttpClient)
{
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

NSData *payloadData = nil;
if (threepids)
{
Expand Down Expand Up @@ -3469,6 +3493,13 @@ - (MXHTTPOperation*)requestEmailValidation:(NSString*)email
success:(void (^)(NSString *sid))success
failure:(void (^)(NSError *error))failure
{
if (!identityHttpClient)
{
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{
@"email": email,
@"client_secret": clientSecret,
Expand Down Expand Up @@ -3507,6 +3538,13 @@ - (MXHTTPOperation*)requestPhoneNumberValidation:(NSString*)phoneNumber
success:(void (^)(NSString *sid, NSString *msisdn))success
failure:(void (^)(NSError *error))failure
{
if (!identityHttpClient)
{
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{
@"phone_number": phoneNumber,
@"country": (countryCode ? countryCode : @""),
Expand Down Expand Up @@ -3547,6 +3585,13 @@ - (MXHTTPOperation *)submit3PIDValidationToken:(NSString *)token
success:(void (^)(void))success
failure:(void (^)(NSError *))failure
{
if (!identityHttpClient)
{
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

// Sanity check
if (!medium.length)
{
Expand Down Expand Up @@ -3591,6 +3636,13 @@ - (MXHTTPOperation*)signUrl:(NSString*)signUrl
success:(void (^)(NSDictionary *thirdPartySigned))success
failure:(void (^)(NSError *error))failure
{
if (!identityHttpClient)
{
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorMissingIdentityServer userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

NSString *path = [NSString stringWithFormat:@"%@&mxid=%@", signUrl, credentials.userId];

return [identityHttpClient requestWithMethod:@"POST"
Expand Down Expand Up @@ -3659,8 +3711,8 @@ - (MXHTTPOperation*)scanUnencryptedContent:(NSString*)mxcContentURI
if (![mxcContentURI hasPrefix:kMXContentUriScheme])
{
// do not scan non-mxc content URLs
NSError* error = [NSError errorWithDomain:@"Invalid content URI" code:500 userInfo:nil];
failure(error);
NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorInvalidContentURI userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}

Expand Down Expand Up @@ -4918,8 +4970,7 @@ - (MXHTTPOperation*)getPublicisedGroupsForUsers:(NSArray<NSString*>*)userIds
// sanity check
if (!userIds || !userIds.count)
{
NSError* error = [NSError errorWithDomain:@"Invalid params" code:500 userInfo:nil];

NSError *error = [NSError errorWithDomain:kMXRestClientErrorDomain code:MXRestClientErrorInvalidParameters userInfo:nil];
[self dispatchFailure:error inBlock:failure];
return nil;
}
Expand Down
13 changes: 13 additions & 0 deletions MatrixSDK/NotificationCenter/ServiceTerms/MXServiceTerms.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ typedef enum : NSUInteger
MXServiceTypeIntegrationManager
} MXServiceType;

/**
MXServiceTerms error domain
*/
FOUNDATION_EXPORT NSString * _Nonnull const MXServiceTermsErrorDomain;

/**
MXServiceTerms error codes
*/
NS_ERROR_ENUM(MXServiceTermsErrorDomain)
{
MXServiceTermsErrorUnknown,
MXServiceTermsErrorMissingParameters
};

NS_ASSUME_NONNULL_BEGIN

Expand Down
9 changes: 7 additions & 2 deletions MatrixSDK/NotificationCenter/ServiceTerms/MXServiceTerms.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

NSString *const kMXIntegrationManagerAPIPrefixPathV1 = @"_matrix/integrations/v1";

/**
MXServiceTerms error domain
*/
NSString *const MXServiceTermsErrorDomain = @"org.matrix.sdk.MXServiceTermsErrorDomain";

@interface MXServiceTerms()

@property (nonatomic, strong) MXServiceTermsRestClient *restClient;
Expand Down Expand Up @@ -64,8 +69,8 @@ - (MXHTTPOperation *)agreeToTerms:(NSArray<NSString *> *)termsUrls
{
if (failure)
{
MXError *error = [[MXError alloc] initWithErrorCode:kMXSDKErrCodeStringMissingParameters error:@"No Matrix session or no access token"];
failure([error createNSError]);
NSError *error = [NSError errorWithDomain:MXServiceTermsErrorDomain code:MXServiceTermsErrorMissingParameters userInfo:@{ NSLocalizedDescriptionKey : @"No Matrix session or no access token"}];
failure(error);
}
return nil;
}
Expand Down