-
Notifications
You must be signed in to change notification settings - Fork 173
/
OKQ8.java
236 lines (211 loc) · 8.84 KB
/
OKQ8.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (C) 2010 Nullbyte <http://nullbyte.eu>
* Contributors: COLA
*
* 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.liato.bankdroid.banking.banks;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;
import android.content.Context;
import android.text.Html;
import android.text.InputType;
import android.util.Log;
import com.liato.bankdroid.Helpers;
import com.liato.bankdroid.R;
import com.liato.bankdroid.banking.Account;
import com.liato.bankdroid.banking.Bank;
import com.liato.bankdroid.banking.Transaction;
import com.liato.bankdroid.banking.exceptions.BankChoiceException;
import com.liato.bankdroid.banking.exceptions.BankException;
import com.liato.bankdroid.banking.exceptions.LoginException;
import com.liato.bankdroid.provider.IBankTypes;
import eu.nullbyte.android.urllib.Urllib;
public class OKQ8 extends Bank {
private static final String TAG = "OKQ8";
private static final String NAME = "OKQ8 VISA";
private static final String NAME_SHORT = "okq8";
private static final String URL = "https://nettbank.edb.com/Logon/index.jsp?domain=0066&from_page=http://www.okq8.se&to_page=https://nettbank.edb.com/cardpayment/transigo/logon/done/okq8";
private static final int BANKTYPE_ID = IBankTypes.OKQ8;
private static final int INPUT_TYPE_USERNAME = InputType.TYPE_CLASS_PHONE;
private static final String INPUT_HINT_USERNAME = "ÅÅMMDDXXXX";
private static final boolean STATIC_BALANCE = true;
private Pattern reLoginRedir = Pattern.compile("value=\"([^\"]*)\"", Pattern.CASE_INSENSITIVE);
private Pattern reBalance = Pattern.compile("<div class=\"numberpositive\">([^<]*)</div>", Pattern.CASE_INSENSITIVE);
private Pattern reTransactions = Pattern.compile("style=\"white-space: nowrap\">([^<]*)</td>\\s*<td[^>]*>[^<]*</td>\\s*<td[^>]*>[^<]*</td>\\s*<td[^>]*>([^<]*)</td>\\s*<td[^>]*><div[^>]*>([^<]*)</div></td>", Pattern.CASE_INSENSITIVE);
private String response = null;
public OKQ8(Context context) {
super(context);
super.TAG = TAG;
super.NAME = NAME;
super.NAME_SHORT = NAME_SHORT;
super.BANKTYPE_ID = BANKTYPE_ID;
super.URL = URL;
super.INPUT_TYPE_USERNAME = INPUT_TYPE_USERNAME;
super.INPUT_HINT_USERNAME = INPUT_HINT_USERNAME;
super.STATIC_BALANCE = STATIC_BALANCE;
}
public OKQ8(String username, String password, Context context) throws BankException, LoginException, BankChoiceException {
this(context);
this.update(username, password);
}
@Override
protected LoginPackage preLogin() throws BankException,
ClientProtocolException, IOException {
urlopen = new Urllib(true);
Date d = new Date();
List <NameValuePair> postData = new ArrayList <NameValuePair>();
response = urlopen.open("https://nettbank.edb.com/Logon/index.jsp?domain=0066&from_page=http://www.okq8.se&to_page=https://nettbank.edb.com/cardpayment/transigo/logon/done/okq8");
//p_tranid is the epoch time in milliseconds
postData.add(new BasicNameValuePair("p_tranid", Long.toString(d.getTime())));
postData.add(new BasicNameValuePair("p_errorScreen", "LOGON_REPOST_ERROR"));
postData.add(new BasicNameValuePair("n_bank", ""));
postData.add(new BasicNameValuePair("empty_pwd", ""));
postData.add(new BasicNameValuePair("user_id", username.toUpperCase()));
postData.add(new BasicNameValuePair("password", password));
return new LoginPackage(urlopen, postData, response, "https://nettbank.edb.com/Logon/logon/step1");
}
public Urllib login() throws LoginException, BankException {
Matcher matcher;
String value = null;
try {
LoginPackage lp = preLogin();
List <NameValuePair> postData = lp.getPostData();
response = urlopen.open(lp.getLoginTarget(), postData);
if (!response.contains("LOGON_OK")) {
throw new LoginException(res.getText(R.string.invalid_username_password).toString());
}
/*
* After login ok we end up at an intermediate login page with a submit
* form that contains information that must be passed to the next page:
* <input type="hidden" name="so" value="xxx"/>
* <input type="hidden" name="last_logon_time" value="xxx"/>
* <input type="hidden" name="failed_logon_attempts" value="xxx"/>
* <input type="hidden" name="login_service_url" value="xxx"/>
*/
matcher = reLoginRedir.matcher(response);
postData.clear();
if(!matcher.find())
{
throw new LoginException("Could not find value for 'so'.");
}
value = matcher.group(1);
postData.add(new BasicNameValuePair("so", value));
if(!matcher.find())
{
throw new LoginException("Could not find value for 'last_logon_time'.");
}
value = matcher.group(1);
postData.add(new BasicNameValuePair("last_logon_time", value));
if(!matcher.find())
{
throw new LoginException("Could not find value for 'failed_logon_attempts'.");
}
value = matcher.group(1);
postData.add(new BasicNameValuePair("failed_logon_attempts", value));
if(!matcher.find())
{
throw new LoginException("Could not find value for 'login_service_url'.");
}
value = matcher.group(1);
postData.add(new BasicNameValuePair("login_service_url", value));
response = urlopen.open("https://nettbank.edb.com/cardpayment/transigo/logon/done/okq8", postData);
if(response.contains("HTML REDIRECT"))
{
throw new LoginException("Login failed.");
}
}
catch (ClientProtocolException e) {
throw new BankException(e.getMessage());
}
catch (IOException e) {
throw new BankException(e.getMessage());
}
return urlopen;
}
@Override
public void update() throws BankException, LoginException, BankChoiceException {
super.update();
if (username == null || password == null || username.length() == 0 || password.length() == 0) {
throw new LoginException(res.getText(R.string.invalid_username_password).toString());
}
if (response == null) {
urlopen = login();
}
try {
/*
* The start page contains the balance of the account ("Kvar att utnytta") so read it.
* The balance is the first value (of three) that are matched by reBalance expression.
*/
Matcher matcher;
matcher = reBalance.matcher(response);
/*
* The start page contains the balance of the account ("Kvar att utnytta") so read it.
* The balance is the first value (of three) that are matched by reBalance expression.
*/
matcher = reBalance.matcher(response);
if(matcher.find())
{
accounts.add(new Account("Kvar att utnyttja" , Helpers.parseBalance(matcher.group(1)), "1"));
balance = balance.add(Helpers.parseBalance(matcher.group(1)));
}
/*
* Find the next value that is "Saldo". Add a new account but don't add to the balance.
*/
if(matcher.find())
{
accounts.add(new Account("Saldo" , Helpers.parseBalance(matcher.group(1)), "2"));
accounts.add(new Account("Saldo" , Helpers.parseBalance(matcher.group(1)).negate(), "4"));
}
/*
* Find the next value that is "Köpgräns". Add a new account but don't add to the balance.
*/
if(matcher.find())
{
accounts.add(new Account("Köpgräns" , Helpers.parseBalance(matcher.group(1)), "3"));
}
if (accounts.isEmpty()) {
throw new BankException(res.getText(R.string.no_accounts_found).toString());
}
response = urlopen.open("https://nettbank.edb.com/cardpayment/transigo/card/overview/lastTransactionsAccount");
matcher = reTransactions.matcher(response);
ArrayList<Transaction> transactions = new ArrayList<Transaction>();
while (matcher.find()) {
/*
* Capture group 1 = date
* Capture group 2 = text
* Capture group 3 = amount
* Negate the amount since buys are reported as positive.
*/
transactions.add(new Transaction(matcher.group(1).trim(), Html.fromHtml(matcher.group(2)).toString().trim(), Helpers.parseBalance(matcher.group(3)).negate()));
}
accounts.get(0).setTransactions(transactions);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
super.updateComplete();
}
}
}