Skip to content

Commit

Permalink
Authentication: .well-known support (#2117).
Browse files Browse the repository at this point in the history
  • Loading branch information
manuroe committed Mar 6, 2019
1 parent f134ccf commit 4b571ac
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Improvements:
* Use SwiftLint to enforce Swift style and conventions (PR #2300).
* Fix SWIFT_VERSION configuration in post install hook of Podfile (PR #2302).
* Authentication: support SSO by using the fallback URL (#2307).
* Authentication: .well-known support (#2117).

Bug fix:
* Reskin: status bar text is no more readable on iPad (#2276).
Expand Down
3 changes: 2 additions & 1 deletion Riot/Assets/en.lproj/Vector.strings
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
"auth_reset_password_success_message" = "Your password has been reset.\n\nYou have been logged out of all devices and will no longer receive push notifications. To re-enable notifications, re-log in on each device.";
"auth_add_email_and_phone_warning" = "Registration with email and phone number at once is not supported yet until the api exists. Only the phone number will be taken into account. You may add your email to your profile in settings.";
"auth_accept_policies" = "Please review and accept the policies of this homeserver:";
"auth_autodiscover_invalid_response" = "Invalid homeserver discovery response";

// Chat creation
"room_creation_title" = "New Chat";
Expand Down Expand Up @@ -313,7 +314,7 @@
// Room Preview
"room_preview_invitation_format" = "You have been invited to join this room by %@";
"room_preview_subtitle" = "This is a preview of this room. Room interactions have been disabled.";
"room_preview_unlinked_email_warning" = "This invitation was sent to %@, which is not associated with this account. You may wish to login with a different account, or add this email to your this account.";
"room_preview_unlinked_email_warning" = "This invitation was sent to %@, which is not associated with this account. You may wish to login with a different account, or add this email to your account.";
"room_preview_try_join_an_unknown_room" = "You are trying to access %@. Would you like to join in order to participate in the discussion?";
"room_preview_try_join_an_unknown_room_default" = "a room";

Expand Down
6 changes: 5 additions & 1 deletion Riot/Generated/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ internal enum VectorL10n {
internal static var authAddPhoneMessage: String {
return VectorL10n.tr("Vector", "auth_add_phone_message")
}
/// Invalid homeserver discovery response
internal static var authAutodiscoverInvalidResponse: String {
return VectorL10n.tr("Vector", "auth_autodiscover_invalid_response")
}
/// This email address is already in use
internal static var authEmailInUse: String {
return VectorL10n.tr("Vector", "auth_email_in_use")
Expand Down Expand Up @@ -1654,7 +1658,7 @@ internal enum VectorL10n {
internal static var roomPreviewTryJoinAnUnknownRoomDefault: String {
return VectorL10n.tr("Vector", "room_preview_try_join_an_unknown_room_default")
}
/// This invitation was sent to %@, which is not associated with this account. You may wish to login with a different account, or add this email to your this account.
/// This invitation was sent to %@, which is not associated with this account. You may wish to login with a different account, or add this email to your account.
internal static func roomPreviewUnlinkedEmailWarning(_ p1: String) -> String {
return VectorL10n.tr("Vector", "room_preview_unlinked_email_warning", p1)
}
Expand Down
110 changes: 110 additions & 0 deletions Riot/Modules/Authentication/AuthenticationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -869,4 +869,114 @@ - (void)authInputsViewDidCancelOperation:(MXKAuthInputsView *)authInputsView
[self cancel];
}

- (void)authInputsView:(MXKAuthInputsView *)authInputsView autoDiscoverServerWithDomain:(NSString *)domain
{
[self tryServerDiscoveryOnDomain:domain];
}

#pragma mark - Server discovery

- (void)tryServerDiscoveryOnDomain:(NSString *)domain
{
MXAutoDiscovery *autoDiscovery = [[MXAutoDiscovery alloc] initWithDomain:domain];

MXWeakify(self);
[autoDiscovery findClientConfig:^(MXDiscoveredClientConfig * _Nonnull discoveredClientConfig) {
MXStrongifyAndReturnIfNil(self);

switch (discoveredClientConfig.action)
{
case MXDiscoveredClientConfigActionPrompt:
[self customiseServersWithWellKnown:discoveredClientConfig.wellKnown];
break;

case MXDiscoveredClientConfigActionFailPrompt:
case MXDiscoveredClientConfigActionFailError:
{
// Alert user
if (self->alert)
{
[self->alert dismissViewControllerAnimated:NO completion:nil];
}

self->alert = [UIAlertController alertControllerWithTitle:NSLocalizedStringFromTable(@"auth_autodiscover_invalid_response", @"Vector", nil)
message:nil
preferredStyle:UIAlertControllerStyleAlert];

[self->alert addAction:[UIAlertAction actionWithTitle:[NSBundle mxk_localizedStringForKey:@"ok"]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {

self->alert = nil;
}]];

[self presentViewController:self->alert animated:YES completion:nil];

break;
}

default:
// Fail silently
break;
}

} failure:^(NSError * _Nonnull error) {
// Fail silently
}];
}

- (void)customiseServersWithWellKnown:(MXWellKnown*)wellKnown
{
if (self.customServersContainer.hidden)
{
// Check wellKnown data with application default servers
// If different, use custom servers
if (![self.defaultHomeServerUrl isEqualToString:wellKnown.homeServer.baseUrl]
|| ![self.defaultIdentityServerUrl isEqualToString:wellKnown.identityServer.baseUrl])
{
[self showCustomHomeserver:wellKnown.homeServer.baseUrl andIdentityServer:wellKnown.identityServer.baseUrl];
}
}
else
{
if ([self.defaultHomeServerUrl isEqualToString:wellKnown.homeServer.baseUrl]
&& [self.defaultIdentityServerUrl isEqualToString:wellKnown.identityServer.baseUrl])
{
// wellKnown matches with application default servers
// Hide custom servers
[self hideCustomServers:YES];
}
else
{
NSString *customHomeServerURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"customHomeServerURL"];
NSString *customIdentityServerURL = [[NSUserDefaults standardUserDefaults] objectForKey:@"customIdentityServerURL"];

if (![customHomeServerURL isEqualToString:wellKnown.homeServer.baseUrl]
|| ![customIdentityServerURL isEqualToString:wellKnown.identityServer.baseUrl])
{
// Update custom servers
[self showCustomHomeserver:wellKnown.homeServer.baseUrl andIdentityServer:wellKnown.identityServer.baseUrl];
}
}
}
}

- (void)showCustomHomeserver:(NSString*)homeserver andIdentityServer:(NSString*)identityServer
{
// Store the wellknown data into NSUserDefaults before displaying them
[[NSUserDefaults standardUserDefaults] setObject:homeserver forKey:@"customHomeServerURL"];

if (identityServer)
{
[[NSUserDefaults standardUserDefaults] setObject:identityServer forKey:@"customIdentityServerURL"];
}
else
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"customIdentityServerURL"];
}

// And show custom servers
[self hideCustomServers:NO];
}

@end
15 changes: 15 additions & 0 deletions Riot/Modules/Authentication/Views/AuthInputsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,21 @@ - (void)countryPickerViewController:(MXKCountryPickerViewController *)countryPic

#pragma mark - UITextField delegate

- (void)textFieldDidEndEditing:(UITextField*)textField
{
if (textField == self.userLoginTextField && type == MXKAuthenticationTypeLogin)
{
if ([MXTools isMatrixUserIdentifier:self.userLoginTextField.text])
{
if (self.delegate && [self.delegate respondsToSelector:@selector(authInputsView:autoDiscoverServerWithDomain:)])
{
NSString *domain = [self.userLoginTextField.text componentsSeparatedByString:@":"][1];
[self.delegate authInputsView:self autoDiscoverServerWithDomain:domain];
}
}
}
}

- (BOOL)textFieldShouldReturn:(UITextField*)textField
{
if (textField.returnKeyType == UIReturnKeyDone)
Expand Down

0 comments on commit 4b571ac

Please sign in to comment.