This commit is contained in:
Bill Burke 2018-03-19 16:47:13 -04:00
parent 4bba11cd94
commit 8926837a3e
5 changed files with 36 additions and 39 deletions

View file

@ -84,38 +84,19 @@ public class KcinitDriver {
public void mainCmd(String[] args) throws Exception { public void mainCmd(String[] args) throws Exception {
this.args = Arrays.copyOf(args, args.length);
for (String arg : args) {
if (!arg.startsWith("-")) break;
if (arg.equals("-f") || arg.equals("-force")) {
forceLogin = true;
this.args = Arrays.copyOfRange(this.args, 1, this.args.length);
} else if (arg.equals("-browser") || arg.equals("-b")) {
browserLogin = true;
this.args = Arrays.copyOfRange(this.args, 1, this.args.length);
} else {
System.err.println("Illegal argument: " + arg);
printHelp();
System.exit(1);
}
}
this.args = args; this.args = args;
if (args.length == 0) { if (args.length == 0) {
login(); printHelp();
return;
}
if (args[0].startsWith("-")) {
login();
return; return;
} }
if (args[0].equalsIgnoreCase("token")) { if (args[0].equalsIgnoreCase("token")) {
//System.err.println("executing token"); //System.err.println("executing token");
token(); token();
} else if (args[0].equalsIgnoreCase("login")) {
login();
} else if (args[0].equalsIgnoreCase("logout")) { } else if (args[0].equalsIgnoreCase("logout")) {
logout(); logout();
} else if (args[0].equalsIgnoreCase("env")) { } else if (args[0].equalsIgnoreCase("env")) {
@ -402,8 +383,7 @@ public class KcinitDriver {
public void printHelp() { public void printHelp() {
KeycloakInstalled.console().writer().println("Commands:"); KeycloakInstalled.console().writer().println("Commands:");
KeycloakInstalled.console().writer().println(" no arguments is a login"); KeycloakInstalled.console().writer().println(" login [-f] -f forces login");
KeycloakInstalled.console().writer().println(" no argument with -f forces login");
KeycloakInstalled.console().writer().println(" logout"); KeycloakInstalled.console().writer().println(" logout");
KeycloakInstalled.console().writer().println(" token [client] - print access token of desired client. Defaults to default master client. Will print either 'error', 'not-allowed', or 'login-required' on error."); KeycloakInstalled.console().writer().println(" token [client] - print access token of desired client. Defaults to default master client. Will print either 'error', 'not-allowed', or 'login-required' on error.");
KeycloakInstalled.console().writer().println(" install - Install this utility. Will store in $HOME/.keycloak/kcinit unless " + KC_LOGIN_CONFIG_PATH + " env var is set"); KeycloakInstalled.console().writer().println(" install - Install this utility. Will store in $HOME/.keycloak/kcinit unless " + KC_LOGIN_CONFIG_PATH + " env var is set");
@ -647,8 +627,23 @@ public class KcinitDriver {
public void login() throws Exception { public void login() throws Exception {
checkEnv(); checkEnv();
this.args = Arrays.copyOfRange(this.args, 1, this.args.length);
for (String arg : args) {
if (arg.equals("-f") || arg.equals("-force")) {
forceLogin = true;
this.args = Arrays.copyOfRange(this.args, 1, this.args.length);
} else if (arg.equals("-browser") || arg.equals("-b")) {
browserLogin = true;
this.args = Arrays.copyOfRange(this.args, 1, this.args.length);
} else {
System.err.println("Illegal argument: " + arg);
printHelp();
System.exit(1);
}
}
String masterClient = getMasterClient(); String masterClient = getMasterClient();
if (!forceLogin() && readToken(masterClient) != null) { if (!forceLogin && readToken(masterClient) != null) {
KeycloakInstalled.console().writer().println("Already logged in. `kcinit -f` to force relogin"); KeycloakInstalled.console().writer().println("Already logged in. `kcinit -f` to force relogin");
return; return;
} }

View file

@ -462,8 +462,6 @@ public class KeycloakInstalled {
//System.err.println("initial request"); //System.err.println("initial request");
Response response = client.target(authUrl).request().get(); Response response = client.target(authUrl).request().get();
while (true) { while (true) {
System.err.println("looping");
if (response.getStatus() == 403) { if (response.getStatus() == 403) {
if (response.getMediaType() != null) { if (response.getMediaType() != null) {
String splash = response.readEntity(String.class); String splash = response.readEntity(String.class);
@ -471,6 +469,7 @@ public class KeycloakInstalled {
} else { } else {
System.err.println("Forbidden to login"); System.err.println("Forbidden to login");
} }
return false;
} else if (response.getStatus() == 401) { } else if (response.getStatus() == 401) {
String authenticationHeader = response.getHeaderString(HttpHeaders.WWW_AUTHENTICATE); String authenticationHeader = response.getHeaderString(HttpHeaders.WWW_AUTHENTICATE);
if (authenticationHeader == null) { if (authenticationHeader == null) {

View file

@ -164,12 +164,11 @@ public class LogoutEndpoint {
* *
* returns 204 if successful, 400 if not with a json error response. * returns 204 if successful, 400 if not with a json error response.
* *
* @param authorizationHeader
* @return * @return
*/ */
@POST @POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response logoutToken(final @HeaderParam(HttpHeaders.AUTHORIZATION) String authorizationHeader) { public Response logoutToken() {
MultivaluedMap<String, String> form = request.getDecodedFormParameters(); MultivaluedMap<String, String> form = request.getDecodedFormParameters();
checkSsl(); checkSsl();

View file

@ -125,7 +125,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
UserModel user = session.users().addUser(realm, "bburke"); UserModel user = session.users().addUser(realm, "bburke");
session.userCredentialManager().updateCredential(realm, user, UserCredentialModel.password("password")); session.userCredentialManager().updateCredential(realm, user, UserCredentialModel.password("password"));
user.setEnabled(true); user.setEnabled(true);
user.setEmail("patriot1burke@gmail.com"); user.setEmail("p@p.com");
user.addRequiredAction(UserModel.RequiredAction.UPDATE_PASSWORD); user.addRequiredAction(UserModel.RequiredAction.UPDATE_PASSWORD);
user.addRequiredAction(UserModel.RequiredAction.CONFIGURE_TOTP); user.addRequiredAction(UserModel.RequiredAction.CONFIGURE_TOTP);
user.addRequiredAction(UserModel.RequiredAction.VERIFY_EMAIL); user.addRequiredAction(UserModel.RequiredAction.VERIFY_EMAIL);
@ -151,7 +151,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
smtp.put("replyTo", "reply-to@keycloak.org"); smtp.put("replyTo", "reply-to@keycloak.org");
smtp.put("ssl", "true"); smtp.put("ssl", "true");
smtp.put("auth", "true"); smtp.put("auth", "true");
smtp.put("user", "****"); smtp.put("user", "*****");
smtp.put("password", "****"); smtp.put("password", "****");
realm.setSmtpConfig(smtp); realm.setSmtpConfig(smtp);
@ -194,7 +194,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
testInstall(); testInstall();
// login // login
KcinitExec exe = KcinitExec.newBuilder() KcinitExec exe = KcinitExec.newBuilder()
.argsLine("") .argsLine("login")
.executeAsync(); .executeAsync();
exe.waitForStderr("Username:"); exe.waitForStderr("Username:");
exe.sendLine("wburke"); exe.sendLine("wburke");
@ -258,7 +258,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
testInstall(); testInstall();
KcinitExec exe = KcinitExec.newBuilder() KcinitExec exe = KcinitExec.newBuilder()
.argsLine("") .argsLine("login")
.executeAsync(); .executeAsync();
exe.waitForStderr("Username:"); exe.waitForStderr("Username:");
exe.sendLine("wburke"); exe.sendLine("wburke");
@ -288,7 +288,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
//Thread.sleep(100000000); //Thread.sleep(100000000);
KcinitExec exe = KcinitExec.newBuilder() KcinitExec exe = KcinitExec.newBuilder()
.argsLine("") .argsLine("login")
.executeAsync(); .executeAsync();
try { try {
exe.waitForStderr("Username:"); exe.waitForStderr("Username:");
@ -327,7 +327,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
testInstall(); testInstall();
KcinitExec exe = KcinitExec.newBuilder() KcinitExec exe = KcinitExec.newBuilder()
.argsLine("") .argsLine("login")
.executeAsync(); .executeAsync();
exe.waitForStderr("Username:"); exe.waitForStderr("Username:");
exe.sendLine("wburke"); exe.sendLine("wburke");
@ -343,7 +343,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
Assert.assertEquals(0, exe.stdoutLines().size()); Assert.assertEquals(0, exe.stdoutLines().size());
exe = KcinitExec.newBuilder() exe = KcinitExec.newBuilder()
.argsLine("-f") .argsLine("login -f")
.executeAsync(); .executeAsync();
exe.waitForStderr("Username:"); exe.waitForStderr("Username:");
exe.sendLine("wburke"); exe.sendLine("wburke");
@ -383,7 +383,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
testInstall(); testInstall();
KcinitExec exe = KcinitExec.newBuilder() KcinitExec exe = KcinitExec.newBuilder()
.argsLine("") .argsLine("login")
.executeAsync(); .executeAsync();
exe.waitForStderr("Username:"); exe.waitForStderr("Username:");
exe.sendLine("wburke"); exe.sendLine("wburke");
@ -427,7 +427,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
Assert.assertEquals(0, exe.exitCode()); Assert.assertEquals(0, exe.exitCode());
exe = KcinitExec.newBuilder() exe = KcinitExec.newBuilder()
.argsLine("") .argsLine("login")
.executeAsync(); .executeAsync();
exe.waitForStderr("Username:"); exe.waitForStderr("Username:");
exe.sendLine("wburke"); exe.sendLine("wburke");
@ -465,7 +465,7 @@ public class KcinitTest extends AbstractTestRealmKeycloakTest {
testInstall(); testInstall();
KcinitExec exe = KcinitExec.newBuilder() KcinitExec exe = KcinitExec.newBuilder()
.argsLine("") .argsLine("login")
.executeAsync(); .executeAsync();
exe.waitForStderr("Username:"); exe.waitForStderr("Username:");
exe.sendLine("test-user@localhost"); exe.sendLine("test-user@localhost");

View file

@ -25,3 +25,7 @@ ${msg("loginTotpManualStep3")}
</#if> </#if>
Enter in your one time password so we can verify you have installed it correctly.