UserEditor.java

/***************************************************************************
   Copyright 2012 Emily Estes

   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 net.metanotion.sqlauthident;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import javax.sql.DataSource;

import net.metanotion.authident.UserToken;
import net.metanotion.json.JsonArray;
import net.metanotion.json.JsonObject;
import net.metanotion.sql.DbUtil;
import net.metanotion.sql.SchemaPoolSwitcher;
import net.metanotion.util.ArgsToStruct;
import net.metanotion.util.SecureString;

/** This is a command line application to be used for editing users in a database, usually for testing/mock purposes.
To run this program:<br />
<code>java -class net.metanotion.sqlauthident.UserEditor <i>jbdc_url</i> <i>jdbc_username</i> <i>jdbc_password</i>
[<i>postgresql_schema</i>] -a <i>username_to_add</i> <i>password_to_add</i></code><br />
So far, only the <i>-a</i> commandline switch is supported for adding users.
*/
public final class UserEditor {
	/** This class represents the command line options used by UserEditor. */
	public static final class Args {
		public String dbUrl;
		public String dbUser;
		public String dbPass;
		public String schema;
		public boolean addUser;
		public String user;
		public String pass;
		public File file;
	}

	/** The main function of the user editor.
		@param args The paarameters to this program.
	*/
	public static void main(String[] args) {
		try {
			final Args arg = (new ArgsToStruct<>(Args.class)).getInstance(args);
			DataSource ds = DbUtil.startDBConnectionPool(arg.dbUrl, arg.dbUser, arg.dbPass);
			if(arg.schema != null) { ds = new SchemaPoolSwitcher(ds, arg.schema); }
			final SQLRealm realm = new SQLRealm(ds);

			if(arg.addUser) {
				realm.createAuthentication(arg.user, new SecureString(arg.pass));
			} else if(arg.file != null) {
				try (final Reader json = new InputStreamReader(new FileInputStream(arg.file), StandardCharsets.UTF_8)) {
					final JsonObject obj = JsonObject.read(json);
					final JsonArray users = (JsonArray) obj.get("addUsers");
					for(final Object element: users) {
						final JsonObject user = (JsonObject) element;
						final Object u = user.get("user");
						final SecureString pass = new SecureString(user.get("pass").toString());
						if(u instanceof JsonArray) {
							UserToken theUser = null;
							for(Object name: ((JsonArray) u)) {
								if(theUser == null) {
									theUser = realm.createAuthentication(name.toString(), pass);
								} else {
									realm.addAuthentication(theUser, name.toString());
								}
							}
						} else {
							realm.createAuthentication(u.toString(), pass);
						}
					}
				}
			} else {
				System.out.println("ERROR: you did not specify a user to add or a file to process.");
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}
}