Search in sources :

Example 6 with ApplianceBootstrap

use of org.keycloak.services.managers.ApplianceBootstrap in project keycloak by keycloak.

the class WelcomeResource method createUser.

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML_UTF_8)
public Response createUser(final MultivaluedMap<String, String> formData) {
    checkBootstrap();
    if (!shouldBootstrap()) {
        return createWelcomePage(null, null);
    } else {
        if (!isLocal()) {
            ServicesLogger.LOGGER.rejectedNonLocalAttemptToCreateInitialUser(session.getContext().getConnection().getRemoteAddr());
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }
        csrfCheck(formData);
        String username = formData.getFirst("username");
        String password = formData.getFirst("password");
        String passwordConfirmation = formData.getFirst("passwordConfirmation");
        if (username != null) {
            username = username.trim();
        }
        if (username == null || username.length() == 0) {
            return createWelcomePage(null, "Username is missing");
        }
        if (password == null || password.length() == 0) {
            return createWelcomePage(null, "Password is missing");
        }
        if (!password.equals(passwordConfirmation)) {
            return createWelcomePage(null, "Password and confirmation doesn't match");
        }
        expireCsrfCookie();
        ApplianceBootstrap applianceBootstrap = new ApplianceBootstrap(session);
        if (applianceBootstrap.isNoMasterUser()) {
            setBootstrap(false);
            applianceBootstrap.createMasterRealmUser(username, password);
            ServicesLogger.LOGGER.createdInitialAdminUser(username);
            return createWelcomePage("User created", null);
        } else {
            ServicesLogger.LOGGER.initialUserAlreadyCreated();
            return createWelcomePage(null, "Users already exists");
        }
    }
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 7 with ApplianceBootstrap

use of org.keycloak.services.managers.ApplianceBootstrap in project keycloak by keycloak.

the class KeycloakServer method setupDevConfig.

protected void setupDevConfig() {
    if (System.getProperty("keycloak.createAdminUser", "true").equals("true")) {
        KeycloakSession session = sessionFactory.create();
        try {
            session.getTransactionManager().begin();
            if (new ApplianceBootstrap(session).isNoMasterUser()) {
                new ApplianceBootstrap(session).createMasterRealmUser("admin", "admin");
                log.info("Created master user with credentials admin:admin");
            }
            session.getTransactionManager().commit();
        } finally {
            session.close();
        }
    }
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) KeycloakSession(org.keycloak.models.KeycloakSession)

Example 8 with ApplianceBootstrap

use of org.keycloak.services.managers.ApplianceBootstrap in project keycloak by keycloak.

the class KeycloakOnUndertow method setupDevConfig.

protected void setupDevConfig() {
    KeycloakSession session = sessionFactory.create();
    try {
        session.getTransactionManager().begin();
        if (new ApplianceBootstrap(session).isNoMasterUser()) {
            new ApplianceBootstrap(session).createMasterRealmUser("admin", "admin");
        }
        session.getTransactionManager().commit();
    } finally {
        session.close();
    }
}
Also used : ApplianceBootstrap(org.keycloak.services.managers.ApplianceBootstrap) KeycloakSession(org.keycloak.models.KeycloakSession)

Aggregations

ApplianceBootstrap (org.keycloak.services.managers.ApplianceBootstrap)8 KeycloakSession (org.keycloak.models.KeycloakSession)5 SystemException (javax.transaction.SystemException)2 Transaction (javax.transaction.Transaction)2 Consumes (javax.ws.rs.Consumes)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ExportImportManager (org.keycloak.exportimport.ExportImportManager)2 KeycloakSessionTask (org.keycloak.models.KeycloakSessionTask)2 JtaTransactionManagerLookup (org.keycloak.transaction.JtaTransactionManagerLookup)2 KeycloakSessionFactory (org.keycloak.models.KeycloakSessionFactory)1 KeycloakTransactionManager (org.keycloak.models.KeycloakTransactionManager)1 DBLockManager (org.keycloak.models.dblock.DBLockManager)1 DBLockProvider (org.keycloak.models.dblock.DBLockProvider)1 KeycloakModelUtils.runJobInTransaction (org.keycloak.models.utils.KeycloakModelUtils.runJobInTransaction)1 PostMigrationEvent (org.keycloak.models.utils.PostMigrationEvent)1 QuarkusKeycloakSessionFactory (org.keycloak.quarkus.runtime.integration.QuarkusKeycloakSessionFactory)1