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

Authentication: .well-known support #2313

Merged
merged 2 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
123 changes: 123 additions & 0 deletions Riot/Modules/Authentication/AuthenticationViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ @interface AuthenticationViewController ()
Observe kThemeServiceDidChangeThemeNotification to handle user interface theme change.
*/
id kThemeServiceDidChangeThemeNotificationObserver;

/**
Server discovery.
*/
MXAutoDiscovery *autoDiscovery;
}

@end
Expand Down Expand Up @@ -235,6 +240,8 @@ - (void)destroy
[[NSNotificationCenter defaultCenter] removeObserver:kThemeServiceDidChangeThemeNotificationObserver];
kThemeServiceDidChangeThemeNotificationObserver = nil;
}

autoDiscovery = nil;
}

- (void)setAuthType:(MXKAuthenticationType)authType
Expand Down Expand Up @@ -869,4 +876,120 @@ - (void)authInputsViewDidCancelOperation:(MXKAuthInputsView *)authInputsView
[self cancel];
}

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

#pragma mark - Server discovery

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

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

self->autoDiscovery = nil;

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) {
MXStrongifyAndReturnIfNil(self);

self->autoDiscovery = nil;

// 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