/*
 * Copyright 2016 Crown Copyright
 *
 * 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.
 */

package stroom.security.client;

import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import stroom.alert.client.event.AlertEvent;
import stroom.core.client.LocationManager;
import stroom.dispatch.client.ClientDispatchAsync;
import stroom.security.client.api.event.LogoutEvent;
import stroom.security.shared.FetchUserAndPermissionsAction;
import stroom.security.shared.LogoutAction;
import stroom.ui.config.client.UiConfigCache;

public class LoginManager implements HasHandlers {
    private final EventBus eventBus;
    private final CurrentUser currentUser;
    private final ClientDispatchAsync dispatcher;
    private final LocationManager locationManager;
    private final UiConfigCache clientPropertyCache;

    @Inject
    public LoginManager(
            final EventBus eventBus,
            final CurrentUser currentUser,
            final ClientDispatchAsync dispatcher,
            final LocationManager locationManager,
            final UiConfigCache clientPropertyCache) {
        this.eventBus = eventBus;
        this.currentUser = currentUser;
        this.dispatcher = dispatcher;
        this.locationManager = locationManager;
        this.clientPropertyCache = clientPropertyCache;

        // Listen for logout events.
        eventBus.addHandler(LogoutEvent.getType(), event -> logout());
    }

    public void fetchUserAndPermissions() {
        // When we start the application we will try and auto login using a client certificates.
        dispatcher.exec(new FetchUserAndPermissionsAction(), "Loading. Please wait...").onSuccess(userAndPermissions -> {
            if (userAndPermissions != null) {
                currentUser.setUserAndPermissions(userAndPermissions);
            } else {
                logout();
            }
        }).onFailure(caught -> AlertEvent.fireInfo(LoginManager.this, caught.getMessage(), this::logout));
    }

    private void logout() {
        // Perform logout on the server
        dispatcher.exec(new LogoutAction(), null)
                .onSuccess(r -> {
                    // Redirect the page to logout.
                    clientPropertyCache.get()
                            .onSuccess(result -> {
                                final String authServiceUrl = result.getUrlConfig().getAuthenticationService();
                                // Send the user's browser to the remote Authentication Service's logout endpoint.
                                // By adding 'prompt=login' we ask the Identity Provider to prompt the user for a login,
                                // bypassing certificate checks. We need this to enable username/password
                                // logins in an environment where the user's browser always presents a certificate.
                                String redirectUrl = URL.encode(result.getUrlConfig().getUi() + "?prompt=login");
                                Window.Location.replace(authServiceUrl + "/logout?redirect_url=" + redirectUrl);
                            });
                })
                .onFailure(t -> AlertEvent.fireErrorFromException(LoginManager.this, t, null));
    }

    @Override
    public void fireEvent(final GwtEvent<?> event) {
        eventBus.fireEvent(event);
    }
}

@@ -2588,24 +2588,36 @@
 re(caught -%3E
+%0A           
  AlertEvent.
@@ -2766,32 +2766,45 @@
       dispatcher
+%0A            
 .exec(new Logout
@@ -2823,36 +2823,32 @@
 ll)%0A            
-    
 .onSuccess(r -%3E 
@@ -2845,28 +2845,24 @@
 cess(r -%3E %7B%0A
-    
             
@@ -2913,20 +2913,16 @@
         
-    
 clientPr
@@ -2936,14 +2936,8 @@
 ache
-.get()
 %0A   
@@ -2933,32 +2933,51 @@
 tyCache%0A        
+            .get()%0A
                 
@@ -2999,32 +2999,24 @@
 result -%3E %7B%0A
-        
             
@@ -3131,24 +3131,16 @@
         
-     
-   
 // Send 
@@ -3214,24 +3214,16 @@
 dpoint.%0A
-        
         
@@ -3327,24 +3327,16 @@
  login,%0A
-        
         
@@ -3449,24 +3449,16 @@
         
-        
 // login
@@ -3557,24 +3557,16 @@
         
-        
 String r
@@ -3663,24 +3663,16 @@
         
-   
-     
 Window.L
@@ -3764,24 +3764,16 @@
         
-        
 %7D);%0A    
@@ -3784,19 +3784,11 @@
     
-    %7D)%0A    
+%7D)%0A
     
@@ -3810,16 +3810,32 @@
 ure(t -%3E
+%0A               
  AlertEv

Format code