Skip to content

Commit

Permalink
Merge pull request #50 from jeffreybakker/feature/extension_3
Browse files Browse the repository at this point in the history
implement extension 3
  • Loading branch information
jeffreybakker authored Aug 13, 2017
2 parents 5d0c224 + 77fb908 commit 659ae0d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/main/java/honours/ing/banq/auth/AuthServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public Card getAuthorizedCard(String token, String iBan, String pinCard) throws
throw new NotAuthorizedError();
}

if (card.isInvalidated()) {
throw new InvalidPINError();
}

return card;
}

Expand Down Expand Up @@ -157,6 +161,10 @@ public BankAccount getAuthorizedBankAccount(String iBAN, String pinCard, String
"There have been " + card.getFailedAttempts() + " consecutive failed attempts.");
}

if (card.isInvalidated()) {
throw new InvalidPINError();
}

if (!card.getPin().equals(pinCode)) {
card.addFailedAttempt();
cardRepository.save(card);
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/honours/ing/banq/card/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public class Card {

@Column(unique = true, nullable = false)
private String cardNumber;
private String pin; // TODO: add hashing
private Date expirationDate;
private String pin;

private Date expirationDate;
private boolean invalidated;
private int failedAttempts;

/**
Expand All @@ -44,17 +45,21 @@ public class Card {
public Card() { }

public Card(Customer holder, BankAccount account, String cardNumber) {
this(holder, account, cardNumber, generatePin());
}

public Card(Customer holder, BankAccount account, String cardNumber, String pin) {
this.holder = holder;
this.account = account;
this.cardNumber = cardNumber;

pin = generatePin();
this.pin = pin;

Calendar expiration = Calendar.getInstance();
expiration.setTimeInMillis(TimeServiceImpl.currentTimeMillis() + DURABILITY);
expirationDate = expiration.getTime();

failedAttempts = 0;
invalidated = false;
}

public Integer getId() {
Expand All @@ -77,6 +82,14 @@ public String getPin() {
return pin;
}

public boolean isInvalidated() {
return invalidated;
}

public void setInvalidated(boolean invalidated) {
this.invalidated = invalidated;
}

public boolean hasExpired() {
return expirationDate.getTime() >= TimeServiceImpl.getCurDate().getTime();
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/honours/ing/banq/card/CardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.googlecode.jsonrpc4j.JsonRpcService;
import honours.ing.banq.InvalidParamValueError;
import honours.ing.banq.access.NoEffectError;
import honours.ing.banq.access.bean.NewCardBean;
import honours.ing.banq.auth.InvalidPINError;
import honours.ing.banq.auth.NotAuthorizedError;

Expand All @@ -28,4 +29,19 @@ Object unblockCard(@JsonRpcParam("authToken") String authToken, @JsonRpcParam("i
@JsonRpcParam("pinCard") String pinCard)
throws InvalidParamValueError, NotAuthorizedError, NoEffectError, InvalidPINError;

/**
* A PIN card can be invalidated if it is lost and replaced with a new card by using this method.
* @param authToken the authentication token
* @param iBAN the number of the bank account
* @param pinCard the number of the pin card
* @param newPin {@code true} if the user wishes to get a new PIN code
* @return an dictionary containing the new information
* @throws InvalidParamValueError if one or more parameters are invalid
* @throws NotAuthorizedError if the user is not authorized to do this
* @throws InvalidPINError if the card is not valid
*/
NewCardBean invalidateCard(@JsonRpcParam("authToken") String authToken, @JsonRpcParam("iBAN") String iBAN,
@JsonRpcParam("pinCard") String pinCard, @JsonRpcParam("newPin") boolean newPin)
throws InvalidParamValueError, NotAuthorizedError, InvalidPINError, NoEffectError;

}
26 changes: 26 additions & 0 deletions src/main/java/honours/ing/banq/card/CardServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import honours.ing.banq.InvalidParamValueError;
import honours.ing.banq.access.NoEffectError;
import honours.ing.banq.access.bean.NewCardBean;
import honours.ing.banq.account.BankAccount;
import honours.ing.banq.account.BankAccountRepository;
import honours.ing.banq.auth.AuthService;
Expand Down Expand Up @@ -42,4 +43,29 @@ public Object unblockCard(String authToken, String iBAN, String pinCard)

return new Object();
}

@Override
public NewCardBean invalidateCard(String authToken, String iBAN, String pinCard, boolean newPin)
throws InvalidParamValueError, NotAuthorizedError, InvalidPINError, NoEffectError {
Card old = auth.getAuthorizedCard(authToken, iBAN, pinCard);

if (old.isInvalidated()) {
throw new NoEffectError();
}

old.setInvalidated(true);

Card res;

if (newPin) {
res = new Card(old.getHolder(), old.getAccount(), CardUtil.generateCardNumber(repository));
} else {
res = new Card(old.getHolder(), old.getAccount(), CardUtil.generateCardNumber(repository), old.getPin());
}

repository.save(old);
repository.save(res);

return new NewCardBean(res);
}
}
8 changes: 8 additions & 0 deletions src/test/java/honours/ing/banq/card/CardServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package honours.ing.banq.card;

import honours.ing.banq.BoilerplateTest;
import honours.ing.banq.access.bean.NewCardBean;
import honours.ing.banq.auth.CardBlockedError;
import honours.ing.banq.auth.InvalidPINError;
import honours.ing.banq.transaction.TransactionService;
Expand Down Expand Up @@ -44,4 +45,11 @@ public void unblockCard() throws Exception {
transactionService.depositIntoAccount(account1.iBan, account1.cardNumber, account1.pin, 10.0);
}

@Test
public void invalidateCard() throws Exception {
NewCardBean res = service.invalidateCard(account1.token, account1.iBan, account1.cardNumber, false);
account1.cardNumber = res.getPinCard();
account1.pin = res.getPinCode();
}

}

0 comments on commit 659ae0d

Please sign in to comment.