TemporaryAccount.java
/*
** Module : TemporaryAccount.java
** Abstract : a temporary account structure.
**
** Copyright (c) 2013-2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------------Description--------------------------
** 001 MAG 20131211 Created initial version.
** 002 MAG 20140128 Add timestamp field and check for expired.
** 003 MAG 20140131 Remove timestamp.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.main;
import java.util.*;
/**
* This structure holds the temporary account subject and the clear text password
* random generated. The structure is used as a pool item on temporary accounts pool
* implementation. This parameters are passed to the spawned process via environment variables.
* Because the values are random generated and could contains special characters in order to
* avoid any side effects the values are converted in hex strings before stored as environment
* variables. On the spawned process the values are get from the process environment and decoded
* from hex strings to their original values.
*
* @author mag
*
*/
public class TemporaryAccount
{
/** Account subject id environment variable */
public static final String P2J_SUBJECT = "P2J_SUBJECT";
/** Account password environment variable */
public static final String P2J_PASSWORD = "P2J_PASSWORD";
/** Subject Id */
private String subject;
/** Password */
private String password;
/**
* Convert a string into a hex format.
*
* @param text
* Input text.
*
* @return The converted string.
*/
public static String toHex(String text)
{
StringBuilder buffer = new StringBuilder();
for (byte b : text.getBytes())
{
buffer.append(String.format("%02X", b));
}
return buffer.toString();
}
/**
* Convert a string from a hex format in ASCII
*
* @param text
* Input text. The length of the input must be multiple of 2.
*
* @return The converted string.
*/
public static String fromHex(String text)
{
int length = text.length();
if (length % 2 != 0)
{
throw new IllegalArgumentException("The input length must be multiple of 2");
}
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < length-1; i += 2 )
{
String hexPairs = text.substring(i, (i + 2));
buffer.append((char)Integer.parseInt(hexPairs, 16));
}
return buffer.toString();
}
/**
* Constructor
*
* @param subject
* Subject id.
* @param password
* Password
*/
public TemporaryAccount(String subject, String password)
{
this.password = password;
this.subject = subject;
}
/**
* Create environment variables.
*
* @return A Map containing the random account credentials.
* The key is the name of the environment variable.
* The values is the account value as hex format.
*/
public synchronized Map<String, String> getEnvironmentMap()
{
Map<String, String> environment = new HashMap<>();
environment.put(TemporaryAccount.P2J_SUBJECT, toHex(subject));
environment.put(TemporaryAccount.P2J_PASSWORD, toHex(password));
return environment;
}
/**
* Get an item from pool
*
* @return fetch an item from the head of pool.
*/
public static TemporaryAccount open()
{
return TemporaryAccountPool.getInstance().poll();
}
/**
* Enqueued <code>this</code> item at the tail of the pool.
*
*/
public void close()
{
TemporaryAccountPool.getInstance().tail(this);
}
/**
* Get user account name.
*
* @return User account name.
*/
public synchronized String getSubject()
{
return subject;
}
/**
* Get account password.
*
* @return Account password.
*/
public synchronized String getPassword()
{
return password;
}
/**
* Set account password.
*
* @param password
* New password.
*/
public synchronized void setPassword(String password)
{
this.password = password;
}
}