Re-organize the project into a better hierarchy.
This commit is contained in:
14
platform-independent/cli-java/build.gradle
Normal file
14
platform-independent/cli-java/build.gradle
Normal file
@@ -0,0 +1,14 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'application'
|
||||
id 'com.github.johnrengelman.shadow' version '1.2.4'
|
||||
}
|
||||
|
||||
description = 'Master Password CLI'
|
||||
mainClassName = 'com.lyndir.masterpassword.CLI'
|
||||
|
||||
dependencies {
|
||||
compile project(':masterpassword-algorithm')
|
||||
|
||||
compile group: 'ch.qos.logback', name: 'logback-classic', version:'1.1.2'
|
||||
}
|
98
platform-independent/cli-java/pom.xml
Normal file
98
platform-independent/cli-java/pom.xml
Normal file
@@ -0,0 +1,98 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<!-- PROJECT METADATA -->
|
||||
<parent>
|
||||
<groupId>com.lyndir.masterpassword</groupId>
|
||||
<artifactId>masterpassword</artifactId>
|
||||
<version>GIT-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<name>Master Password CLI</name>
|
||||
<description>A CLI interface to the Master Password algorithm</description>
|
||||
|
||||
<artifactId>masterpassword-cli</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<!-- BUILD CONFIGURATION -->
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/scripts</directory>
|
||||
<filtering>true</filtering>
|
||||
<targetPath>${project.build.directory}</targetPath>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-antrun-plugin</artifactId>
|
||||
<version>1.7</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>prepare-package</id>
|
||||
<phase>prepare-package</phase>
|
||||
<configuration>
|
||||
<target>
|
||||
<chmod file="${project.build.directory}/install" perm="755" />
|
||||
</target>
|
||||
</configuration>
|
||||
<goals>
|
||||
<goal>run</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>com.lyndir.masterpassword.CLI</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/*.SF</exclude>
|
||||
<exclude>META-INF/*.DSA</exclude>
|
||||
<exclude>META-INF/*.RSA</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<!-- DEPENDENCY MANAGEMENT -->
|
||||
<dependencies>
|
||||
|
||||
<!-- PROJECT REFERENCES -->
|
||||
<dependency>
|
||||
<groupId>com.lyndir.masterpassword</groupId>
|
||||
<artifactId>masterpassword-algorithm</artifactId>
|
||||
<version>GIT-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* Copyright 2008, Maarten Billemont
|
||||
*
|
||||
* 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 com.lyndir.masterpassword;
|
||||
|
||||
import static com.lyndir.lhunath.opal.system.util.ObjectUtils.ifNotNullElse;
|
||||
import static com.lyndir.lhunath.opal.system.util.StringUtils.strf;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.io.LineReader;
|
||||
import com.google.common.primitives.UnsignedInteger;
|
||||
import com.lyndir.lhunath.opal.system.util.ConversionUtils;
|
||||
import com.lyndir.lhunath.opal.system.util.StringUtils;
|
||||
import java.io.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* <p> <i>Jun 10, 2008</i> </p>
|
||||
*
|
||||
* @author mbillemo
|
||||
*/
|
||||
public class CLI {
|
||||
|
||||
public static void main(final String[] args)
|
||||
throws IOException {
|
||||
|
||||
// Read information from the environment.
|
||||
char[] masterPassword;
|
||||
String siteName = null, context = null;
|
||||
String userName = System.getenv( MPConstant.env_userName );
|
||||
String siteTypeName = ifNotNullElse( System.getenv( MPConstant.env_siteType ), "" );
|
||||
MPSiteType siteType = siteTypeName.isEmpty()? MPSiteType.GeneratedLong: MPSiteType.forOption( siteTypeName );
|
||||
MPSiteVariant variant = MPSiteVariant.Password;
|
||||
String siteCounterName = ifNotNullElse( System.getenv( MPConstant.env_siteCounter ), "" );
|
||||
UnsignedInteger siteCounter = siteCounterName.isEmpty()? UnsignedInteger.valueOf( 1 ): UnsignedInteger.valueOf( siteCounterName );
|
||||
|
||||
// Parse information from option arguments.
|
||||
boolean userNameArg = false, typeArg = false, counterArg = false, variantArg = false, contextArg = false;
|
||||
for (final String arg : Arrays.asList( args ))
|
||||
// Full Name
|
||||
if ("-u".equals( arg ) || "--username".equals( arg ))
|
||||
userNameArg = true;
|
||||
else if (userNameArg) {
|
||||
userName = arg;
|
||||
userNameArg = false;
|
||||
}
|
||||
|
||||
// Type
|
||||
else if ("-t".equals( arg ) || "--type".equals( arg ))
|
||||
typeArg = true;
|
||||
else if (typeArg) {
|
||||
siteType = MPSiteType.forOption( arg );
|
||||
typeArg = false;
|
||||
}
|
||||
|
||||
// Counter
|
||||
else if ("-c".equals( arg ) || "--counter".equals( arg ))
|
||||
counterArg = true;
|
||||
else if (counterArg) {
|
||||
siteCounter = UnsignedInteger.valueOf( arg );
|
||||
counterArg = false;
|
||||
}
|
||||
|
||||
// Variant
|
||||
else if ("-v".equals( arg ) || "--variant".equals( arg ))
|
||||
variantArg = true;
|
||||
else if (variantArg) {
|
||||
variant = MPSiteVariant.forOption( arg );
|
||||
variantArg = false;
|
||||
}
|
||||
|
||||
// Context
|
||||
else if ("-C".equals( arg ) || "--context".equals( arg ))
|
||||
contextArg = true;
|
||||
else if (contextArg) {
|
||||
context = arg;
|
||||
contextArg = false;
|
||||
}
|
||||
|
||||
// Help
|
||||
else if ("-h".equals( arg ) || "--help".equals( arg )) {
|
||||
System.out.println();
|
||||
System.out.format( "Usage: mpw [-u name] [-t type] [-c counter] site\n\n" );
|
||||
System.out.format( " -u name Specify the full name of the user.\n" );
|
||||
System.out.format( " Defaults to %s in env.\n\n", MPConstant.env_userName );
|
||||
System.out.format( " -t type Specify the password's template.\n" );
|
||||
System.out.format( " Defaults to %s in env or 'long' for password, 'name' for login.\n", MPConstant.env_siteType );
|
||||
|
||||
int optionsLength = 0;
|
||||
Map<String, MPSiteType> typeMap = Maps.newLinkedHashMap();
|
||||
for (MPSiteType elementType : MPSiteType.values()) {
|
||||
String options = Joiner.on( ", " ).join( elementType.getOptions() );
|
||||
typeMap.put( options, elementType );
|
||||
optionsLength = Math.max( optionsLength, options.length() );
|
||||
}
|
||||
for (Map.Entry<String, MPSiteType> entry : typeMap.entrySet()) {
|
||||
String infoString = strf( " -v %" + optionsLength + "s | ", entry.getKey() );
|
||||
String infoNewline = "\n" + StringUtils.repeat( " ", infoString.length() - 3 ) + " | ";
|
||||
infoString += entry.getValue().getDescription().replaceAll( "\n", infoNewline );
|
||||
System.out.println( infoString );
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
System.out.format( " -c counter The value of the counter.\n" );
|
||||
System.out.format( " Defaults to %s in env or '1'.\n\n", MPConstant.env_siteCounter );
|
||||
System.out.format( " -v variant The kind of content to generate.\n" );
|
||||
System.out.format( " Defaults to 'password'.\n" );
|
||||
|
||||
optionsLength = 0;
|
||||
Map<String, MPSiteVariant> variantMap = Maps.newLinkedHashMap();
|
||||
for (MPSiteVariant elementVariant : MPSiteVariant.values()) {
|
||||
String options = Joiner.on( ", " ).join( elementVariant.getOptions() );
|
||||
variantMap.put( options, elementVariant );
|
||||
optionsLength = Math.max( optionsLength, options.length() );
|
||||
}
|
||||
for (Map.Entry<String, MPSiteVariant> entry : variantMap.entrySet()) {
|
||||
String infoString = strf( " -v %" + optionsLength + "s | ", entry.getKey() );
|
||||
String infoNewline = "\n" + StringUtils.repeat( " ", infoString.length() - 3 ) + " | ";
|
||||
infoString += entry.getValue().getDescription().replaceAll( "\n", infoNewline );
|
||||
System.out.println( infoString );
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
System.out.format( " -C context A variant-specific context.\n" );
|
||||
System.out.format( " Defaults to empty.\n" );
|
||||
for (Map.Entry<String, MPSiteVariant> entry : variantMap.entrySet()) {
|
||||
String infoString = strf( " -v %" + optionsLength + "s | ", entry.getKey() );
|
||||
String infoNewline = "\n" + StringUtils.repeat( " ", infoString.length() - 3 ) + " | ";
|
||||
infoString += entry.getValue().getContextDescription().replaceAll( "\n", infoNewline );
|
||||
System.out.println( infoString );
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
System.out.format( " ENVIRONMENT\n\n" );
|
||||
System.out.format( " MP_USERNAME | The full name of the user.\n" );
|
||||
System.out.format( " MP_SITETYPE | The default password template.\n" );
|
||||
System.out.format( " MP_SITECOUNTER | The default counter value.\n\n" );
|
||||
return;
|
||||
} else
|
||||
siteName = arg;
|
||||
|
||||
// Read missing information from the console.
|
||||
Console console = System.console();
|
||||
try (InputStreamReader inReader = new InputStreamReader( System.in )) {
|
||||
LineReader lineReader = new LineReader( inReader );
|
||||
|
||||
if (siteName == null) {
|
||||
System.err.format( "Site name: " );
|
||||
siteName = lineReader.readLine();
|
||||
}
|
||||
|
||||
if (userName == null) {
|
||||
System.err.format( "User's name: " );
|
||||
userName = lineReader.readLine();
|
||||
}
|
||||
|
||||
if (console != null)
|
||||
masterPassword = console.readPassword( "%s's master password: ", userName );
|
||||
|
||||
else {
|
||||
System.err.format( "%s's master password: ", userName );
|
||||
masterPassword = lineReader.readLine().toCharArray();
|
||||
}
|
||||
}
|
||||
|
||||
// Encode and write out the site password.
|
||||
System.out.println( MasterKey.create( userName, masterPassword ).encode( siteName, siteType, siteCounter, variant, context ) );
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
*
|
||||
* @author lhunath, 15-02-04
|
||||
*/
|
||||
|
||||
|
||||
@ParametersAreNonnullByDefault
|
||||
package com.lyndir.masterpassword;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
15
platform-independent/cli-java/src/main/resources/logback.xml
Normal file
15
platform-independent/cli-java/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,15 @@
|
||||
<configuration scan="false">
|
||||
|
||||
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%-8relative %22c{0} [%-5level] %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="com.lyndir" level="${mp.log.level:-INFO}" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="stdout" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
1958
platform-independent/cli-java/src/main/scripts/bashlib
Executable file
1958
platform-independent/cli-java/src/main/scripts/bashlib
Executable file
File diff suppressed because it is too large
Load Diff
56
platform-independent/cli-java/src/main/scripts/install
Executable file
56
platform-independent/cli-java/src/main/scripts/install
Executable file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Install the Master Password CLI tool.
|
||||
set -e
|
||||
cd "${BASH_SOURCE%/*}"
|
||||
source bashlib
|
||||
|
||||
inf "This will install the mpw tool."
|
||||
|
||||
# Try to guess then ask for the bin dir to install to.
|
||||
IFS=: read -a paths <<< "$PATH"
|
||||
if inArray ~/bin "${paths[@]}"; then
|
||||
bindir=~/bin
|
||||
elif inArray ~/.bin "${paths[@]}"; then
|
||||
bindir=~/.bin
|
||||
elif inArray /usr/local/bin "${paths[@]}"; then
|
||||
bindir=/usr/local/bin
|
||||
else
|
||||
bindir=~/bin
|
||||
fi
|
||||
bindir=$(ask -d "$bindir" "What bin directory should I install to?")
|
||||
[[ -d "$bindir" ]] || mkdir "$bindir" || ftl 'Cannot create missing bin directory: %s' "$bindir" || exit
|
||||
[[ -w "$bindir" ]] || ftl 'Cannot write to bin directory: %s' "$bindir" || exit
|
||||
|
||||
# Try to guess then ask for the share dir to install to.
|
||||
sharedir=$(cd -P "$bindir/.."; [[ $bindir = */.bin ]] && printf '%s/.share' "$PWD" || printf '%s/share' "$PWD")
|
||||
sharedir=$(ask -d "$sharedir" "What share directory should I install to?")
|
||||
[[ -d "$sharedir" ]] || mkdir "$sharedir" || ftl 'Cannot create missing share directory: %s' "$sharedir" || exit
|
||||
[[ -w "$sharedir" ]] || ftl 'Cannot write to share directory: %s' "$sharedir" || exit
|
||||
|
||||
# Install Master Password.
|
||||
sharepath=$sharedir/masterpassword
|
||||
mkdir -p "$sharepath"
|
||||
cp -a "masterpassword-cli-"*".jar" bashlib mpw "$sharepath"
|
||||
ex -c "%s~%SHAREPATH%~$sharepath~g|x" "$sharepath/mpw"
|
||||
ln -sf "$sharepath/mpw" "$bindir/mpw"
|
||||
chmod +x "$sharepath/mpw"
|
||||
[[ ! -e "$bindir/bashlib" ]] && ln -s "$sharepath/bashlib" "$bindir/bashlib" ||:
|
||||
|
||||
# Convenience bash function.
|
||||
inf "Installation successful!"
|
||||
echo
|
||||
inf "To improve usability, you can install an mpw function in your bash shell."
|
||||
inf "This function adds the following features:"
|
||||
inf " - Ask the Master Password once, remember in the shell."
|
||||
inf " - Automatically put the password in the clipboard (some platforms)."
|
||||
echo
|
||||
inf "To do this you need the following function in ~/.bashrc:\n%s" "$(<mpw.bashrc)"
|
||||
echo
|
||||
inf "We can do this for you automatically now."
|
||||
if ask -c Y!n "Append the mpw function to your .bashrc?"; then
|
||||
cat mpw.bashrc >> ~/.bashrc
|
||||
inf "Done! Don't forget to run '%s' to apply the changes!" "source ~/.bashrc"
|
||||
fi
|
||||
echo
|
||||
inf "To begin using Master Password, type: mpw [site name]"
|
9
platform-independent/cli-java/src/main/scripts/mpw
Executable file
9
platform-independent/cli-java/src/main/scripts/mpw
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# Uncomment this to set your user name. Master Password will no longer ask you for a user name.
|
||||
# export MP_USERNAME="Robert Lee Mitchell"
|
||||
# Uncomment this to hardcode your master password. Make sure this file's permissions are tight. Master Password will not ask you for your master password anymore. This is probably not a good idea.
|
||||
# export MP_PASSWORD="banana colored duckling"
|
||||
|
||||
cd "%SHAREPATH%"
|
||||
exec java -jar masterpassword-cli-GIT-SNAPSHOT.jar "$@"
|
15
platform-independent/cli-java/src/main/scripts/mpw.bashrc
Normal file
15
platform-independent/cli-java/src/main/scripts/mpw.bashrc
Normal file
@@ -0,0 +1,15 @@
|
||||
source bashlib
|
||||
mpw() {
|
||||
_nocopy() { echo >&2 "$(cat)"; }
|
||||
_copy() { "$(type -P pbcopy || type -P xclip || echo _nocopy)"; }
|
||||
|
||||
# Empty the clipboard
|
||||
:| _copy 2>/dev/null
|
||||
|
||||
# Ask for the user's name and password if not yet known.
|
||||
MP_USERNAME=${MP_USERNAME:-$(ask -s 'Your Full Name:')}
|
||||
MP_PASSWORD=${MP_PASSWORD:-$(ask -s 'Master Password:')}
|
||||
|
||||
# Start Master Password and copy the output.
|
||||
printf %s "$(MP_USERNAME=$MP_USERNAME MP_PASSWORD=$MP_PASSWORD command mpw "$@")" | _copy
|
||||
}
|
Reference in New Issue
Block a user