Kod:
public class NordeaManager implements BankManager {
private static final String TAG = "NordeaManager";
private static final String NAME = "Nordea";
public final static String KEY_PREFIX = "NOR_";
private static final String LOGIN_URL = "https://internetbanken.privat.nordea.se/nsp/engine";
private static final String ACCOUNTS_URL = "https://internetbanken.privat.nordea.se/nsp/engine?usecase=menu&command=openmenu&commandorigin=0.leftmenu_view&&menuid=160";
private static final String CREDITS_URL = "https://internetbanken.privat.nordea.se/nsp/engine?usecase=menu&command=openmenu&commandorigin=0.leftmenu_view&&menuid=147";
private static final String GUID_PARAM = "guid";
private static final String FPID_PARAM = "fpid";
private static final String HASH_PARAM = "hash";
private static final String COMMAND_PARAM = "commandorigin";
private static final String USER_PARAM = "userid";
private static final String PASS_PARAM = "pin";
private static final String ACCOUNTS_REGEX =
"KF00T', '(\\d+)', '([^']+)'[^\\n]*[^>]*>([0-9.,-]+)";
private static final String CREDITS_REGEX =
"Kontoutdrag[^>]*>([^<]+)<[^\\n]*[^>]*>([0-9.,-]+)";
private static final String FOND_REGEX =
"sendFund[^>]*>([^<]+)<[^\\n]*[^>]*>([0-9.,-]+)";
private static final String HASH_REGEX= "hash=([^\"\\&]+)";
private static final String GUID_REGEX= "guid=([^\"\\&]+)";
private static final String FPID_REGEX= "fpid=([^\"\\&]+)";
private BankLogin mBankLogin;
private Context mContext;
public NordeaManager(BankLogin bankLogin, Context context) {
this.mBankLogin = bankLogin;
this.mContext = context;
}
@Override
public String getName() {
return NAME;
}
@Override
public Map<AccountHashKey, RemoteAccount> getAccounts() throws BankException {
return getAccounts(new LinkedHashMap<AccountHashKey, RemoteAccount>());
}
@Override
public Map<AccountHashKey, RemoteAccount> getAccounts(Map<AccountHashKey, RemoteAccount> accounts) throws BankException {
Log.d(TAG, "-> getAccounts()");
HttpClient httpClient = new SaldoHttpClient(mContext);
try {
Log.d(TAG, "getting hash,guid,fpid...");
String res = HttpHelper.get(httpClient, LOGIN_URL);
Pattern pattern = Pattern.compile(HASH_REGEX);
Matcher matcher = pattern.matcher(res);
if (!matcher.find()) {
Log.e(TAG, "No hash match.");
throw new NordeaException("No hash match.");
}
String hash = matcher.group(1);
Log.d(TAG, "hash= " + hash);
pattern = Pattern.compile(GUID_REGEX);
matcher = pattern.matcher(res);
if (!matcher.find()) {
Log.e(TAG, "No guid match.");
throw new NordeaException("No guid match.");
}
String guid = matcher.group(1);
Log.d(TAG, "guid= " + guid);
pattern = Pattern.compile(FPID_REGEX);
matcher = pattern.matcher(res);
if (!matcher.find()) {
Log.e(TAG, "No fpid match.");
throw new NordeaException("No fpid match.");
}
String fpid = matcher.group(1);
Log.d(TAG, "fpid= " + fpid);
Log.d(TAG, "getting hash,guid,fpid...");
// Do login
List<NameValuePair> parameters = new ArrayList<NameValuePair>(3);
parameters.add(new BasicNameValuePair(USER_PARAM, mBankLogin.getUsername()));
parameters.add(new BasicNameValuePair(PASS_PARAM, mBankLogin.getPassword()));
parameters.add(new BasicNameValuePair(COMMAND_PARAM, "0.commonlogintabview_SE"));
parameters.add(new BasicNameValuePair(HASH_PARAM, hash));
parameters.add(new BasicNameValuePair(GUID_PARAM, guid));
parameters.add(new BasicNameValuePair(FPID_PARAM, fpid));
parameters.add(new BasicNameValuePair("usecase","base"));
parameters.add(new BasicNameValuePair("command","formcommand"));
parameters.add(new BasicNameValuePair("commonlogin","commonlogin"));
parameters.add(new BasicNameValuePair("commonlogin$loginLight","Logga in"));
parameters.add(new BasicNameValuePair("JAVASCRIPT_DETECTED","true"));
res = HttpHelper.post(httpClient, LOGIN_URL, parameters);
if (res.contains("felaktiga uppgifter")) {
//login failed.. bail
throw new AuthenticationException("auth fail");
}
pattern = Pattern.compile(GUID_REGEX);
matcher = pattern.matcher(res);
if (!matcher.find()) {
Log.e(TAG, "No guid match.");
throw new NordeaException("No guid match.");
}
guid = matcher.group(1);
Log.d(TAG, "guid= " + guid);
pattern = Pattern.compile(FPID_REGEX);
matcher = pattern.matcher(res);
if (!matcher.find()) {
Log.e(TAG, "No fpid match.");
throw new NordeaException("No fpid match.");
}
fpid = matcher.group(1);
Log.d(TAG, "fpid= " + fpid);
//ACCOUNTS
Log.d(TAG, "getting account info...");
res = HttpHelper.get(httpClient, ACCOUNTS_URL+"&guid="+guid+"&fpid="+fpid+"&hash="+hash);
//Log.d(TAG, "accounts html dump:");
//Log.d(TAG, res);
pattern = Pattern.compile(ACCOUNTS_REGEX);
matcher = pattern.matcher(res);
int ordinal=1;
while (matcher.find()) {
int remoteId = Integer.parseInt(matcher.group(1));
ordinal = Integer.parseInt(matcher.group(1));
String name = Html.fromHtml(matcher.group(2)).toString();
long balance = Long.parseLong(matcher.group(3).replaceAll("\\,|\\.", ""))/100;
accounts.put(new AccountHashKey(remoteId, bankLogin.getId()), new Account(remoteId, bankLogin.getId(), ordinal, name, balance));
}
//CREDIT CARDS
Log.d(TAG, "getting account info...");
String res2 = HttpHelper.get(httpClient, CREDITS_URL+"&guid="+guid+"&fpid="+fpid+"&hash="+hash);
Log.d(TAG, "accounts html dump:");
Log.d(TAG, res);
pattern = Pattern.compile(CREDITS_REGEX);
matcher = pattern.matcher(res2);
int i = ordinal;
while (matcher.find()) {
ordinal = ++i;
int remoteId = ordinal+100; // we need a unique remoteId per bank login
String name = Html.fromHtml(matcher.group(1)).toString();
long balance = Long.parseLong(matcher.group(2).replaceAll("\\,|\\.", ""))/100;
accounts.put(new AccountHashKey(remoteId, bankLogin.getId()), new Account(remoteId, bankLogin.getId(), ordinal, name, balance));
}
//FONDER
pattern = Pattern.compile(FOND_REGEX);
matcher = pattern.matcher(res);
i = ordinal;
while (matcher.find()) {
ordinal = ++i;
int remoteId = ordinal+200;
String name = Html.fromHtml(matcher.group(1)).toString();
long balance = Long.parseLong(matcher.group(2).replaceAll("\\,|\\.", ""))/100;
accounts.put(new AccountHashKey(remoteId, bankLogin.getId()), new Account(remoteId, bankLogin.getId(), ordinal, name, balance));
}
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
throw new NordeaException(e.getMessage(), e);
} catch (HttpException e) {
Log.e(TAG, e.getMessage(), e);
throw new NordeaException(e.getMessage(), e);
}
Log.d(TAG, "<- getAccounts()");
return accounts;
}