Skip to content

Commit

Permalink
MXRestClient: Make sure that forgetPasswordForEmail, requestTokenForE…
Browse files Browse the repository at this point in the history
…mail and requestTokenForPhoneNumber can work with no IS.

element-hq/element-ios#2658
element-hq/element-ios#2659
  • Loading branch information
manuroe committed Sep 3, 2019
1 parent 44c9a3d commit 78a2ce6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 88 deletions.
28 changes: 16 additions & 12 deletions MatrixSDK/MXRestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,8 @@ FOUNDATION_EXPORT NSString *const kMXMembersOfRoomParametersNotMembership;
Check that the given email address is associated with any account
and then request the validation of an email address.
The identity server will send an email to this address. The end user
will have to click on the link it contains to validate the address.
Check MXMatrixVersion.doesServerRequireIdentityServerParam to determine if
the homeserver requires the id_server parameter to be provided.
@param email the email address to validate.
@param clientSecret a secret key generated by the client. ([MXTools generateSecret] creates such key)
Expand Down Expand Up @@ -512,11 +512,16 @@ FOUNDATION_EXPORT NSString *const kMXMembersOfRoomParametersNotMembership;

#pragma mark - 3pid token request
/**
Request the validation of an email address (like `requestEmailValidation` in identity server API),
but first checks that the given email address is not already associated with an account on this Home Server.
The identity server will send an email to this address. The end user
will have to click on the link it contains to validate the address.
Requests an email verification token for the purposes of adding a
third party identifier to an account.
Check MXMatrixVersion.doesServerRequireIdentityServerParam to determine if
the homeserver requires the id_server parameter to be provided.
If an account with the given email address already exists and is
associated with an account other than the one the user is authed as,
it will either send an email to the address informing them of this
or return M_THREEPID_IN_USE (which one is up to the homeserver).
Use the returned sid to complete operations that require authenticated email
like [MXRestClient add3PID:].
Expand Down Expand Up @@ -544,11 +549,10 @@ FOUNDATION_EXPORT NSString *const kMXMembersOfRoomParametersNotMembership;
failure:(void (^)(NSError *error))failure NS_REFINED_FOR_SWIFT;

/**
Request the validation of a phone number (like `requestPhoneNumberValidation` in identity server API),
but first checks that the given email address is not already associated with an account on this Home Server.
The identity server will send a validation token by sms. The end user
will have to send this token by using [MXRestClient submit3PIDValidationToken].
Requests a text message verification token for the purposes of registration.
Check MXMatrixVersion.doesServerRequireIdentityServerParam to determine if
the homeserver requires the id_server parameter to be provided.
Use the returned sid to complete operations that require authenticated phone number
like [MXRestClient add3PID:].
Expand Down
137 changes: 61 additions & 76 deletions MatrixSDK/MXRestClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -458,40 +458,28 @@ - (MXHTTPOperation *)forgetPasswordForEmail:(NSString *)email
success:(void (^)(NSString *sid))success
failure:(void (^)(NSError *error))failure
{
NSString *identityServer = self.credentials.identityServer;
if ([identityServer hasPrefix:@"http://"] || [identityServer hasPrefix:@"https://"])
{
identityServer = [identityServer substringFromIndex:[identityServer rangeOfString:@"://"].location + 3];
}

NSString *path = [NSString stringWithFormat:@"%@/account/password/email/requestToken", apiPathPrefix];

NSDictionary *parameters = @{
@"email" : email,
@"client_secret" : clientSecret,
@"send_attempt" : @(sendAttempt),
@"id_server" : identityServer
};

MXWeakify(self);
return [httpClient requestWithMethod:@"POST"
path:[NSString stringWithFormat:@"%@/account/password/email/requestToken", apiPathPrefix]
parameters:parameters
success:^(NSDictionary *JSONResponse) {
MXStrongifyAndReturnIfNil(self);
return [self requestTokenFromEndpoint:path parameters:parameters success:^(NSDictionary *JSONResponse) {
MXStrongifyAndReturnIfNil(self);

if (success)
{
__block NSString *sid;
[self dispatchProcessing:^{
MXJSONModelSetString(sid, JSONResponse[@"sid"]);
} andCompletion:^{
success(sid);
}];
}
}
failure:^(NSError *error) {
MXStrongifyAndReturnIfNil(self);
[self dispatchFailure:error inBlock:failure];
}];
if (success)
{
__block NSString *sid;
[self dispatchProcessing:^{
MXJSONModelSetString(sid, JSONResponse[@"sid"]);
} andCompletion:^{
success(sid);
}];
}
} failure:failure];
}

#pragma mark - Login operations
Expand Down Expand Up @@ -909,17 +897,10 @@ - (MXHTTPOperation*)requestTokenForEmail:(NSString*)email
success:(void (^)(NSString *sid))success
failure:(void (^)(NSError *error))failure
{
NSString *identityServer = self.credentials.identityServer;
if ([identityServer hasPrefix:@"http://"] || [identityServer hasPrefix:@"https://"])
{
identityServer = [identityServer substringFromIndex:[identityServer rangeOfString:@"://"].location + 3];
}

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{
@"email": email,
@"client_secret": clientSecret,
@"send_attempt" : @(sendAttempt),
@"id_server" : identityServer
@"send_attempt" : @(sendAttempt)
}];

if (nextLink)
Expand All @@ -938,26 +919,19 @@ - (MXHTTPOperation*)requestTokenForEmail:(NSString*)email
}

MXWeakify(self);
return [httpClient requestWithMethod:@"POST"
path:path
parameters:parameters
success:^(NSDictionary *JSONResponse) {
MXStrongifyAndReturnIfNil(self);
return [self requestTokenFromEndpoint:path parameters:parameters success:^(NSDictionary *JSONResponse) {
MXStrongifyAndReturnIfNil(self);

if (success)
{
__block NSString *sid;
[self dispatchProcessing:^{
MXJSONModelSetString(sid, JSONResponse[@"sid"]);
} andCompletion:^{
success(sid);
}];
}
}
failure:^(NSError *error) {
MXStrongifyAndReturnIfNil(self);
[self dispatchFailure:error inBlock:failure];
}];
if (success)
{
__block NSString *sid;
[self dispatchProcessing:^{
MXJSONModelSetString(sid, JSONResponse[@"sid"]);
} andCompletion:^{
success(sid);
}];
}
} failure:failure];
}

- (MXHTTPOperation*)requestTokenForPhoneNumber:(NSString*)phoneNumber
Expand All @@ -969,18 +943,11 @@ - (MXHTTPOperation*)requestTokenForPhoneNumber:(NSString*)phoneNumber
success:(void (^)(NSString *sid, NSString *msisdn))success
failure:(void (^)(NSError *error))failure
{
NSString *identityServer = self.credentials.identityServer;
if ([identityServer hasPrefix:@"http://"] || [identityServer hasPrefix:@"https://"])
{
identityServer = [identityServer substringFromIndex:[identityServer rangeOfString:@"://"].location + 3];
}

NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:@{
@"phone_number": phoneNumber,
@"country": (countryCode ? countryCode : @""),
@"client_secret": clientSecret,
@"send_attempt" : @(sendAttempt),
@"id_server" : identityServer
@"send_attempt" : @(sendAttempt)
}];
if (nextLink)
{
Expand All @@ -997,24 +964,42 @@ - (MXHTTPOperation*)requestTokenForPhoneNumber:(NSString*)phoneNumber
path = [NSString stringWithFormat:@"%@/account/3pid/msisdn/requestToken", apiPathPrefix];
}

MXWeakify(self);
return [self requestTokenFromEndpoint:path parameters:parameters success:^(NSDictionary *JSONResponse) {
MXStrongifyAndReturnIfNil(self);

if (success)
{
__block NSString *sid, *msisdn;
[self dispatchProcessing:^{
MXJSONModelSetString(sid, JSONResponse[@"sid"]);
MXJSONModelSetString(msisdn, JSONResponse[@"msisdn"]);
} andCompletion:^{
success(sid, msisdn);
}];
}
} failure:failure];
}


- (MXHTTPOperation*)requestTokenFromEndpoint:(NSString *)path
parameters:(NSDictionary*)parameters
success:(void (^)(NSDictionary *JSONResponse))success
failure:(void (^)(NSError *error))failure
{

NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:parameters];
if (self.credentials.identityServer)
{
NSURL *identityServerURL = [NSURL URLWithString:self.credentials.identityServer];
params[@"id_server"] = identityServerURL.host;
}

MXWeakify(self);
return [httpClient requestWithMethod:@"POST"
path:path
parameters:parameters
success:^(NSDictionary *JSONResponse) {
MXStrongifyAndReturnIfNil(self);

if (success)
{
__block NSString *sid, *msisdn;
[self dispatchProcessing:^{
MXJSONModelSetString(sid, JSONResponse[@"sid"]);
MXJSONModelSetString(msisdn, JSONResponse[@"msisdn"]);
} andCompletion:^{
success(sid, msisdn);
}];
}
}
parameters:params
success:success
failure:^(NSError *error) {
MXStrongifyAndReturnIfNil(self);
[self dispatchFailure:error inBlock:failure];
Expand Down

0 comments on commit 78a2ce6

Please sign in to comment.