Commit f3bc1bcb authored by Patrik Dufresne's avatar Patrik Dufresne

Convert project to pom. Add license information. Add license header to

java file. Fix test cases.
parent 865e63d8
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
/target
*~
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>license</name>
<name>com.patrikdufresne.license</name>
<comment></comment>
<projects>
</projects>
......@@ -10,8 +10,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.6
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
Copyright (c) 2012 David Stites, Patrik Dufresne and others.
Copyright (c) 2013 David Stites, Patrik Dufresne and others.
LICENSING
=========
This program is free software; you can redistribute it and/or modify
it under the terms of either:
a) The MIT License (MIT)
b) Apache License 2.0
c) Simplified BSD License
it under the terms of Apache License 2.0
You should have received a copy of the MIT License (MIT) with this
software, in the file named "MIT". If not, I'll be glad to provide one.
You should have received a copy of the Apache License 2.0
along with this program in the file named "Apache".
You should have received a copy of the Simplified BSD License
along with this program in the file named "BSD".
along with this program in the file named "LICENSE".
This package is based on the following article:
http://blog.afewguyscoding.com/2012/02/licensing-module-java/
......
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path="bin"/>
</classpath>
#Tue Apr 03 18:41:31 EDT 2012
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
\ No newline at end of file
The MIT License (MIT)
Copyright (c) 2012
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project default="package" name="Build script" basedir=".">
<property name="root.dir" value="." />
<property name="target.dir" value="${root.dir}/ant-target" />
<property name="bin.dir" value="${target.dir}/bin" />
<property name="src.dir" value="${root.dir}/src" />
<property name="libs.dir" value="${root.dir}/lib" />
<target name="init">
<property file="${root.dir}/version.properties" />
<tstamp />
<property name="jar.filename" value="${package.name}_${version}.${DSTAMP}.jar" />
<property name="src.filename" value="${package.name}.source_${version}.${DSTAMP}.zip" />
<mkdir dir="${target.dir}" />
<mkdir dir="${bin.dir}" />
</target>
<target name="compile" depends="init">
<copy includeemptydirs="false" todir="${bin.dir}" filtering="true">
<fileset dir="${src.dir}" excludes="**/*.launch, **/*.testsuite, **/*.deploy, **/*.location, **/*.execution, **/*.datapool, **/*.artifact, **/*.html, **/*.svg, **/*.java" />
</copy>
<javac srcdir="${src.dir}" destdir="${bin.dir}" nowarn="yes" target="1.5" includeAntRuntime="no" debug="true" debuglevel="lines,vars,source">
</javac>
</target>
<!-- Main entry point to produce binaries for all supported platform -->
<target name="package" depends="package-src, jar" />
<target name="package-src" depends="init">
<zip destfile="${target.dir}/${src.filename}">
<zipfileset dir="${src.dir}" prefix="src" />
<zipfileset file="${root.dir}/README"/>
<zipfileset file="${root.dir}/build.xml"/>
<zipfileset file="${root.dir}/version.properties" />
</zip>
</target>
<target name="jar" depends="compile">
<jar destfile="${target.dir}/${jar.filename}" basedir="${bin.dir}" encoding="utf-8">
</jar>
</target>
<target name="clean" description="Use to clean the project">
<delete dir="${target.dir}" />
<delete dir="${bin.dir}" />
</target>
</project>
/*
* Copyright (c) 2012 David Stites, Patrik Dufresne and others.
*
* You may distribute under the terms of either the MIT License, the Apache
* License 2.0 or the Simplified BSD License, as specified in the README file.
*
* Contributors:
* David Stites - initial API and implementation
* Patrik Dufresne - refactoring
*/
package com.patrikdufresne.license;
import java.io.Serializable;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Abstract implementation of the license interface use by the license manager.
*
* @author Patrik Dufresne
*
*/
public abstract class AbstractLicense implements Serializable, ILicense {
private static final long serialVersionUID = -144058457108187374L;
/**
* License type for lifetime version. Always valid.
*/
public static final String TYPE_LIFETIME = "lifetime";
/**
* License type for single version. This type is valid for the given
* version.
*/
public static final String TYPE_SINGLE_VERSION = "single-version";
/**
* License type for trial version. This type is valid until the expiration
* date.
*/
public static final String TYPE_TRIAL = "trial";
private String email;
private Date expiration;
private String licenseNumber;
private String licenseType;
private String name;
private String version;
/**
* Create a new license with default property value.
*/
public AbstractLicense() {
name = "";
email = "";
licenseNumber = "";
expiration = new Date();
version = "";
licenseType = TYPE_TRIAL;
}
/**
* Return the associated email value.
*
* @return the email or null
*/
public String getEmail() {
return email;
}
/**
* @return the expiration
*/
public Date getExpiration() {
return expiration;
}
/**
* @return the licenseNumber
*/
public String getLicenseNumber() {
return licenseNumber;
}
/**
* Return the license type.
*
* @return the licenseType
*/
public String getLicenseType() {
return licenseType;
}
/**
* Return the name associated with the license.
*
* @return the name or null
*/
public String getName() {
return name;
}
/**
* Return the license version
*
* @return the version or null
*/
public String getVersion() {
return version;
}
/**
* Sets the associated email
*
* @param email
* the email or null.
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Set the license expiration date. Required with TYPE_TRIAL
*
* @param expiration
* the expiration date or null.
*/
public void setExpiration(Date expiration) {
this.expiration = expiration;
}
/**
* Sets the license number.
*
* @param licenseNumber
* the licenseNumber
*/
public void setLicenseNumber(String licenseNumber) {
this.licenseNumber = licenseNumber;
}
/**
* Sets the license type.
*
* @param licenseType
* the licenseType, one of the TYPE_* constants (can't be null).
*/
public void setLicenseType(String licenseType) {
if (licenseType == null) {
throw new NullPointerException();
}
this.licenseType = licenseType;
}
/**
* Sets the name associated with the license
*
* @param name
* the name or null
*/
public void setName(String name) {
this.name = name;
}
/**
* Sets the license version. Required with SINGLE_VERSION.
*
* @param version
* the version or null
*/
public void setVersion(String version) {
this.version = version;
}
/**
* Check if the given license object is valid.
*
* @param license
* the license object
*/
public void validate(Date currentDate, String currentVersion)
throws LicenseException {
validateExpiration(new Date());
validateVersion(currentVersion);
}
/**
* Used to validate the expiration date according to the license type.
*
* @param currentDate
* the current date.
* @throws LicenseExpiredException
*/
protected void validateExpiration(Date currentDate)
throws LicenseExpiredException {
if (getLicenseType().equals(TYPE_TRIAL)) {
if (getExpiration() == null || currentDate.after(getExpiration())) {
throw new LicenseExpiredException();
}
}
// The expiration date doesn't matter for a single version license or a
// lifetime version.
}
/**
* Used to validate the version according to the license type.
*
* @param currentVersion
* @throws LicenseVersionExpiredException
*/
protected void validateVersion(String currentVersion)
throws LicenseVersionExpiredException {
if (getLicenseType().equals(TYPE_SINGLE_VERSION)) {
if (getVersion() == null) {
throw new LicenseVersionExpiredException();
}
Pattern pattern = Pattern.compile(getVersion());
Matcher matcher = pattern.matcher(currentVersion);
if (!matcher.matches()) {
throw new LicenseVersionExpiredException();
}
}
}
}
/*
* Copyright (c) 2012 David Stites, Patrik Dufresne and others.
*
* You may distribute under the terms of either the MIT License, the Apache
* License 2.0 or the Simplified BSD License, as specified in the README file.
*
* Contributors:
* David Stites - initial API and implementation
* Patrik Dufresne - refactoring
*/
package com.patrikdufresne.license;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
/**
* This class is used to manage the encryption of the license. It's used to
* encrypt, sign and validate using a public or private key.
*
* @author Patrik Dufresne
*
*/
public class EncryptionManager {
private static final int SIZE = 2048;
/**
* Single instance of the utility class.
*/
// private static EncryptionManager instance;
// private static final String PUBLIC_KEY_FILE = "/License/public_key.der";
// private static final String PRIVATE_KEY_FILE =
// "/path/to/your/private_key.der";
private PublicKey publicKey;
/**
* Our private key.
*/
private PrivateKey privateKey;
/**
* Create a new encryption manager.
*
* @param publicKey
* the public key (can't be null).
* @param privateKey
* the private key (null if not available).
* @throws NoSuchAlgorithmException
* if no Provider supports RSA
* @throws InvalidKeySpecException
* if the given key specification is inappropriate for this key
* factory to produce a public key.
*/
public EncryptionManager(byte[] publicKey, byte[] privateKey)
throws NoSuchAlgorithmException, InvalidKeySpecException {
if (publicKey == null) {
throw new NullPointerException("publicKey");
}
X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
KeyFactory kf = KeyFactory.getInstance("RSA");
this.publicKey = kf.generatePublic(spec);
if (privateKey != null) {
PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(
privateKey);
KeyFactory privateKeyFactory = KeyFactory.getInstance("RSA");
this.privateKey = privateKeyFactory.generatePrivate(privateSpec);
}
}
/**
* This function is used to read a stream.
*
* @param input
* the input stream
* @return the data read from the stream
* @throws IOException
*/
public static byte[] readAll(InputStream input) throws IOException {
// Read the content of the file and store it in a byte array.
ByteArrayOutputStream out = new ByteArrayOutputStream(SIZE);
byte[] buf = new byte[SIZE];
int size;
while ((size = input.read(buf)) != -1) {
out.write(buf, 0, size);
}
return out.toByteArray();
}
/**
* This function maybe used to read the public and/or private key from a
* file.
*
* @param file
* the file to read
* @return the file data
*
* @throws IOException
* if the file does not exist, or if the first byte cannot be
* read for any reason
*/
public static byte[] readAll(File file) throws IOException {
InputStream input = new FileInputStream(file);
try {
return readAll(input);
} finally {
input.close();
}
}
/**
* Use to check if the given data matches the given signature.
*
* @param data
* the data
* @param sig
* the signature associated with the data.
*
* @throws NoSuchAlgorithmException
* if the algorithm SHA1withRSA is not supported.
* @throws NoSuchProviderException
* @throws InvalidKeyException
* if the key is invalid.
* @throws SignatureException
* if this signature algorithm is unable to process the input
* data
*/
public boolean verify(byte[] data, byte[] sig)
throws NoSuchAlgorithmException, InvalidKeyException,
SignatureException {
// Initialize the signing algorithm with our public key
Signature rsaSignature = Signature.getInstance("SHA1withRSA");
rsaSignature.initVerify(publicKey);
// Update the signature algorithm with the data.
rsaSignature.update(data);
// Validate the signature
return rsaSignature.verify(sig);
}
/**
* Sign the given input stream data. The signature is append to the output
* stream.
*
* @param data
* the the data to be signed.
* @return the signature for the given data.
* @throws NoSuchAlgorithmException
* if no Provider supports a Signature implementation for
* SHA1withRSA.
* @throws InvalidKeyException
* if the private key is invalid.
* @throws SignatureException
* if this signature algorithm is unable to process the input
* data provided.
* @throws UnsupportedOperationException
* if the private key was not providedin the constructor.
*/
public byte[] sign(byte[] data) throws NoSuchAlgorithmException,
InvalidKeyException, SignatureException {
if (privateKey == null) {
throw new UnsupportedOperationException(
"Can't sign when the private key is not available.");
}
// Initialize the signing algorithm with our private key
Signature rsaSignature = Signature.getInstance("SHA1withRSA");
rsaSignature.initSign(privateKey);
rsaSignature.update(data);
// Generate the signature.
return rsaSignature.sign();
}
}
/*
* Copyright (c) 2012 David Stites, Patrik Dufresne and others.
*
* You may distribute under the terms of either the MIT License, the Apache
* License 2.0 or the Simplified BSD License, as specified in the README file.
*
* Contributors:
* David Stites - initial API and implementation
* Patrik Dufresne - refactoring
*/
package com.patrikdufresne.license;
import java.io.Serializable;
/**
* This interface is used to represent a license data.
*
* @author ikus060
*
*/
public interface ILicense extends Serializable {
}
/*
* Copyright (c) 2012 David Stites, Patrik Dufresne and others.
*
* You may distribute under the terms of either the MIT License, the Apache
* License 2.0 or the Simplified BSD License, as specified in the README file.
*
* Contributors:
* David Stites - initial API and implementation
* Patrik Dufresne - refactoring
*/
package com.patrikdufresne.license;
/**
* This exception is throw by the key manager when the key is determined to be
* black listed.
*
* @author Patrik Dufresne
*
*/
public class KeyBlackListedException extends LicenseException {
private static final long serialVersionUID = 4833729281645719038L;
public KeyBlackListedException() {
super("black listed key");
}