package io.liveoak.keycloak.server;

import java.io.FileNotFoundException;

import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;

import io.liveoak.keycloak.theme.LiveOakLoginThemeProviderFactory;
import org.jboss.resteasy.core.Dispatcher;
import org.keycloak.Config;
import org.keycloak.enums.SslRequired;
import org.keycloak.models.ApplicationModel;
import org.keycloak.models.ClaimMask;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.models.utils.KeycloakModelUtils;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.services.managers.ApplicationManager;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resources.KeycloakApplication;

public class KeycloakServerApplication extends KeycloakApplication {

    public KeycloakServerApplication(@Context ServletContext context, @Context Dispatcher dispatcher) throws FileNotFoundException {
        super(context, dispatcher);
    }

    @Override
    protected void setupDefaultRealm(String contextPath) {
        super.setupDefaultRealm(contextPath);

        KeycloakSession session = getSessionFactory().create();
        session.getTransaction().begin();
        try {
            configureLiveOakConsole(session);
            session.getTransaction().commit();
        } finally {
            session.close();
        }
    }

    protected void configureLiveOakConsole(KeycloakSession session) {
        RealmManager manager = new RealmManager(session);
        RealmModel adminRealm = manager.getRealm(Config.getAdminRealm());

        ApplicationModel consoleApp = adminRealm.getApplicationByName("console");
        if (consoleApp == null) {
            consoleApp = new ApplicationManager(manager).createApplication(adminRealm, "console");
            consoleApp.setPublicClient(true);

            consoleApp.setAllowedClaimsMask(ClaimMask.ALL);
            consoleApp.addRedirectUri("/admin");
            consoleApp.addRedirectUri("/admin/");
            consoleApp.setBaseUrl("/admin/");

            consoleApp.addScopeMapping(adminRealm.getRole("admin"));
        }

        RealmModel appsRealm = manager.getRealm("liveoak-apps");
        if (appsRealm == null) {
            RealmManager realmManager = new RealmManager(session);
            RealmModel realm = realmManager.createRealm("liveoak-apps");

            realm.setEnabled(true);
            realm.setRegistrationAllowed(true);
            realm.setSslRequired(SslRequired.EXTERNAL);
            realm.addRequiredCredential(CredentialRepresentation.PASSWORD);

            realm.setSsoSessionMaxLifespan(36000);
            realm.setAccessTokenLifespan(300);
            realm.setSsoSessionIdleTimeout(6000);
            realm.setAccessCodeLifespan(60);
            realm.setAccessCodeLifespanUserAction(300);
            KeycloakModelUtils.generateRealmKeys(realm);

            realm.setLoginTheme(LiveOakLoginThemeProviderFactory.ID);
            realm.setAccountTheme(LiveOakLoginThemeProviderFactory.ID);
            adminRealm.setLoginTheme(LiveOakLoginThemeProviderFactory.ID);
            adminRealm.setAccountTheme(LiveOakLoginThemeProviderFactory.ID);
            adminRealm.setSsoSessionIdleTimeout(6000);
        }
    }

}

@@ -215,16 +215,50 @@
 ctory;%0D%0A
+import org.jboss.logging.Logger;%0D%0A
 import o
@@ -289,24 +289,24 @@
 ispatcher;%0D%0A
-
 import org.k
@@ -524,32 +524,81 @@
 ls.RealmModel;%0D%0A
+import org.keycloak.models.UserCredentialModel;%0D%0A
 import org.keycl
@@ -945,24 +945,216 @@
 cation %7B%0D%0A%0D%0A
+    private static final String LIVEOAK_INITIAL_PASSWORD_PARAMETER = %22liveoak.initial.password%22;%0D%0A%0D%0A    private static final Logger log = Logger.getLogger(KeycloakServerApplication.class);%0D%0A%0D%0A
     public K
@@ -2366,24 +2366,24 @@
 /admin/%22);%0D%0A
-
             
@@ -2415,24 +2415,909 @@
 dmin/%22);%0D%0A%0D%0A
+            //check if we should create a customized default password for LiveOak's KeyCloak instance%0D%0A            String initialPassword = System.getProperty(LIVEOAK_INITIAL_PASSWORD_PARAMETER);%0D%0A            if (initialPassword != null) %7B%0D%0A                //Create a new, initial password for the admin user%0D%0A                UserCredentialModel password = new UserCredentialModel();%0D%0A                password.setType(UserCredentialModel.PASSWORD);%0D%0A                password.setValue(initialPassword);%0D%0A%0D%0A                session.users().getUserByUsername(%22admin%22, adminRealm).updateCredential(password);%0D%0A%0D%0A                log.info(%22############################################################%22);%0D%0A                log.info(%22INITIAL LIVEOAK PASSWORD IS : %22 + initialPassword);%0D%0A                log.info(%22############################################################%22);%0D%0A            %7D%0D%0A%0D%0A
             

Add a system parameter to set the initial password for LiveOak. Allows us to start the server with a password other than the hardcoded 'admin'.