/*
* AccountImpl.java
*
* Created on December 13, 2001, 9:25 AM
*/
package com.systinet.demos.bank;
// imports of WASP security
import org.idoox.security.AuthResult;
import org.idoox.security.Credentials;
import org.idoox.security.PrincipalAuthenticator;
import org.idoox.security.server.Current;
import org.idoox.webservice.server.Initializable;
import org.idoox.webservice.server.WebServiceContext;
/**
* Account implementation
*/
public class AccountImpl
implements Account, Initializable
{
private double balance = 0;
private String number = "";
public AccountImpl()
{
this.number = ""+System.currentTimeMillis();
}
public void init(WebServiceContext context)
{
authenticate();
}
public void destroy()
{
// do nothing here
}
/**
* Deposits to the account
* @param amount amount of many to deposit
* @throws AuthenticationException if authentication fails
*/
synchronized public void deposit(double amount)
throws AuthenticationException
{
checkAuth();
this.balance += amount;
}
/**
* Withdraw from the account
* @param amount amount to withdraw
* @throws UnsufficientFundsException thrown if account doesn't hava enough funds
* @throws AuthenticationException if authentication fails
*/
synchronized public void withdraw(double amount)
throws UnsufficientFundsException, AuthenticationException
{
checkAuth();
if(amount < this.balance) {
this.balance = this.balance - amount;
}
else {
throw new UnsufficientFundsException("The withdrawal of " + amount +
" was requested but the balance is
only " +
this.balance+" .");
}
}
/**
* Returns the account balance
* @return the actual balance of the account
* @throws AuthenticationException if authentication fails
*/
synchronized public double getBalance()
throws AuthenticationException
{
checkAuth();
return this.balance;
}
/**
* Sets the account balance
* @param amount the actual balance of the account
* @throws AuthenticationException if authentication fails
*/
synchronized public void setBalance(double amount)
throws AuthenticationException
{
checkAuth();
this.balance = amount;
}
/**
* Returns the account number
* @return account number
* @throws AuthenticationException if authentication fails
*/
public String getAccountNumber()
throws AuthenticationException
{
checkAuth();
return this.number;
}
/**
* Sets the account number
* @param accountNumber account number
* @throws AuthenticationException if authentication fails
*/
public void setAccountNumber(String accountNumber)
throws AuthenticationException
{
checkAuth();
this.number = accountNumber;
}
/**
* Close the account
* @throws AuthenticationException if authentication fails
*/
public void close()
throws AuthenticationException
{
checkAuth();
org.idoox.webservice.server.WebServiceContext context =
org.idoox.webservice.server.WebServiceContext.getInstance();
org.idoox.webservice.server.LifeCycleService lc =
context.getLifeCycleService();
lc.disposeServiceInstance(this);
}
/**
* Creates and sets the security identity credentials if they are
* not alread set
*/
private synchronized void authenticate()
{
Current current = Current.getInstance();
if (current.getCredentials() == null) {
PrincipalAuthenticator auth = current.getAuthenticator();
AuthResult result = auth.authenticate("bank-server",
"password".getBytes());
if (result.resultCode != AuthResult.AUTH_STATUS_SUCCESS) {
System.err.println("Unable to authenticate");
}
current.setCredentials(new Credentials[] { result.creds });
}
}
/**
* Performs very simple authorization based on the hardcoded
* identity name, which is able to manipulate the account.
*
* @throws AuthenticationException if the client is not authorized
*/
private void checkAuth()
throws AuthenticationException
{
Current current = Current.getInstance();
Credentials credentials = current.getReceivedCredentials();
if(credentials != null) {
String caller = credentials.getName();
if(caller == null || !caller.equals("john")) {
throw new AuthenticationException("Access denied.");
}
}
}
}
A send public_kay_a To B
B encrypt sym_key with public_kay_a , send the result to A
A decrypt what he received with private_key, get sym_key
A and B communicate with data encypted with sym_key