Skip to content

Commit dabd189

Browse files
authored
Merge pull request #108 from bunq/oauth-bunq/sdk_java#107
Oauth #107
2 parents e43acde + 1cd5bda commit dabd189

37 files changed

+441
-4127
lines changed

src/main/java/com/bunq/sdk/context/SessionContext.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import com.bunq.sdk.exception.BunqException;
44
import com.bunq.sdk.model.core.BunqModel;
55
import com.bunq.sdk.model.core.SessionServer;
6+
import com.bunq.sdk.model.generated.endpoint.UserApiKey;
67
import com.bunq.sdk.model.generated.endpoint.UserCompany;
78
import com.bunq.sdk.model.generated.endpoint.UserPerson;
89
import com.google.gson.annotations.Expose;
910
import com.google.gson.annotations.SerializedName;
11+
1012
import java.util.Date;
1113

1214
/**
@@ -17,12 +19,7 @@ public class SessionContext implements java.io.Serializable {
1719
/**
1820
* Error constants.
1921
*/
20-
private static final String ERROR_UNEXPECTED_USER_TYPE = "Unexpected user type.";
21-
22-
/**
23-
* Default assumed value for session timeout.
24-
*/
25-
private static final int SESSION_TIMEOUT_DEFAULT = 0;
22+
private static final String ERROR_UNEXPECTED_USER_TYPE = "'%s' is an unexpected user type.";
2623

2724
/**
2825
* Constant for converting milliseconds to seconds.
@@ -53,16 +50,22 @@ public class SessionContext implements java.io.Serializable {
5350
SessionContext(SessionServer sessionServer) {
5451
this.token = sessionServer.getSessionToken().getToken();
5552
this.expiryTime = calculateExpiryTime(sessionServer);
56-
this.userId = getUserId(sessionServer.getReferencedObject());
53+
this.userId = getUserId(sessionServer.getReferencedUser());
5754
}
5855

59-
private Integer getUserId(BunqModel user) {
56+
private int getUserId(BunqModel user) {
6057
if (user instanceof UserPerson) {
6158
return ((UserPerson) user).getId();
6259
} else if (user instanceof UserCompany) {
63-
return ((UserCompany) user).getId();
60+
return ((UserCompany) user).getId();
61+
} else if (user instanceof UserApiKey) {
62+
return ((UserApiKey) user).getId();
6463
} else {
65-
throw new BunqException(ERROR_UNEXPECTED_USER_TYPE);
64+
throw new BunqException(
65+
String.format(
66+
ERROR_UNEXPECTED_USER_TYPE, user.getClass().toString()
67+
)
68+
);
6669
}
6770
}
6871

@@ -75,19 +78,25 @@ private static Date calculateExpiryTime(SessionServer sessionServer) {
7578
}
7679

7780
private static int getSessionTimeout(SessionServer sessionServer) {
78-
UserCompany userCompany = sessionServer.getUserCompany();
79-
80-
if (userCompany != null) {
81-
return userCompany.getSessionTimeout();
82-
}
81+
BunqModel user = sessionServer.getReferencedUser();
8382

84-
UserPerson userPerson = sessionServer.getUserPerson();
83+
if (user instanceof UserApiKey) {
84+
BunqModel referencedUser = ((UserApiKey) user).getRequestedByUser().getReferencedObject();
8585

86-
if (userPerson != null) {
87-
return userPerson.getSessionTimeout();
86+
return getSessionTimeOutFromUser(referencedUser);
87+
} else {
88+
return getSessionTimeOutFromUser(user);
8889
}
90+
}
8991

90-
return SESSION_TIMEOUT_DEFAULT;
92+
private static int getSessionTimeOutFromUser(BunqModel user) {
93+
if (user instanceof UserCompany) {
94+
return ((UserCompany) user).getSessionTimeout();
95+
} else if (user instanceof UserPerson) {
96+
return ((UserPerson) user).getSessionTimeout();
97+
} else {
98+
throw new BunqException(ERROR_UNEXPECTED_USER_TYPE);
99+
}
91100
}
92101

93102
String getToken() {

src/main/java/com/bunq/sdk/context/UserContext.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.bunq.sdk.context;
22

33
import com.bunq.sdk.exception.BunqException;
4+
import com.bunq.sdk.model.core.BunqModel;
45
import com.bunq.sdk.model.core.UserContextHelper;
56
import com.bunq.sdk.model.generated.endpoint.MonetaryAccountBank;
67
import com.bunq.sdk.model.generated.endpoint.User;
8+
import com.bunq.sdk.model.generated.endpoint.UserApiKey;
79
import com.bunq.sdk.model.generated.endpoint.UserCompany;
810
import com.bunq.sdk.model.generated.endpoint.UserPerson;
911

@@ -18,20 +20,23 @@ public class UserContext {
1820
private final ApiContext apiContext;
1921
private UserCompany userCompany;
2022
private UserPerson userPerson;
23+
private UserApiKey userApiKey;
2124
private MonetaryAccountBank primaryMonetaryAccountBank;
2225

2326
public UserContext(ApiContext apiContext) {
2427
this.apiContext = apiContext;
2528
refreshContext();
2629
}
2730

28-
private void initUser(User user) {
29-
if (user.getUserPerson() != null) {
30-
this.userPerson = user.getUserPerson();
31-
} else if (user.getUserCompany() != null) {
32-
this.userCompany = user.getUserCompany();
31+
private void initUser(BunqModel user) {
32+
if (user instanceof UserPerson) {
33+
this.userPerson = (UserPerson) user;
34+
} else if (user instanceof UserCompany) {
35+
this.userCompany = (UserCompany) user;
36+
} else if (user instanceof UserApiKey) {
37+
this.userApiKey = (UserApiKey) user;
3338
} else {
34-
throw new BunqException(ERROR_UNEXPECTED_USER_INSTANCE);
39+
throw new BunqException(String.format(ERROR_UNEXPECTED_USER_INSTANCE, user.getClass().toString()));
3540
}
3641
}
3742

@@ -41,7 +46,7 @@ private void initMainMonetaryAccount(MonetaryAccountBank monetaryAccountBank) {
4146

4247
public void refreshContext() {
4348
UserContextHelper helper = new UserContextHelper(this.apiContext);
44-
this.initUser(helper.getFirstUser());
49+
this.initUser(helper.getFirstUser().getReferencedObject());
4550
this.initMainMonetaryAccount(helper.getFirstActiveMonetaryAccountBankByUserId(getUserId()));
4651
}
4752

@@ -50,15 +55,19 @@ public Integer getUserId() {
5055
}
5156

5257
public boolean isOnlyUserPersonSet() {
53-
return this.userCompany == null && this.userPerson != null;
58+
return this.userCompany == null && this.userApiKey == null &&this.userPerson != null;
5459
}
5560

5661
public boolean isOnlyUserCompanySet() {
57-
return this.userPerson == null && this.userCompany != null;
62+
return this.userPerson == null && this.userApiKey == null &&this.userCompany != null;
5863
}
5964

60-
public boolean isBothUserTypeSet() {
61-
return this.userPerson != null && this.userCompany != null;
65+
public boolean isOnlyUserApiKeySet() {
66+
return this.userPerson == null && this.userCompany == null && this.userApiKey != null;
67+
}
68+
69+
public boolean areAllUserSet() {
70+
return this.userPerson != null && this.userCompany != null && this.userApiKey != null;
6271
}
6372

6473
public Integer getMainMonetaryAccountId() {
@@ -77,6 +86,10 @@ public UserCompany getUserCompany() {
7786
return this.userCompany;
7887
}
7988

89+
public UserApiKey getUserApiKey() {
90+
return userApiKey;
91+
}
92+
8093
public MonetaryAccountBank getPrimaryMonetaryAccountBank() {
8194
return this.primaryMonetaryAccountBank;
8295
}

src/main/java/com/bunq/sdk/json/SessionServerAdapter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.bunq.sdk.json;
22

3+
import com.bunq.sdk.exception.BunqException;
34
import com.bunq.sdk.model.core.Id;
45
import com.bunq.sdk.model.core.SessionServer;
56
import com.bunq.sdk.model.core.SessionToken;
7+
import com.bunq.sdk.model.generated.endpoint.UserApiKey;
68
import com.bunq.sdk.model.generated.endpoint.UserCompany;
79
import com.bunq.sdk.model.generated.endpoint.UserPerson;
810
import com.google.gson.JsonArray;
@@ -18,6 +20,10 @@
1820
* SessionServer POST response.
1921
*/
2022
public class SessionServerAdapter implements JsonDeserializer<SessionServer> {
23+
/**
24+
* Error constatns.
25+
*/
26+
private static final String ERROR_COULD_NOT_DETERMINE_USER = "Could not determine user.";
2127

2228
private static final int INDEX_ID = 0;
2329
private static final String FIELD_ID = "Id";
@@ -28,6 +34,7 @@ public class SessionServerAdapter implements JsonDeserializer<SessionServer> {
2834
private static final int INDEX_USER = 2;
2935
private static final String FIELD_USER_COMPANY = "UserCompany";
3036
private static final String FIELD_USER_PERSON = "UserPerson";
37+
private static final String FIELD_USER_API_KEY = "UserApiKey";
3138

3239
@Override
3340
public SessionServer deserialize(JsonElement json, Type typeOfT,
@@ -51,13 +58,22 @@ public SessionServer deserialize(JsonElement json, Type typeOfT,
5158
);
5259

5360
return new SessionServer(id, token, userCompany);
54-
} else {
61+
} else if (userBody.has(FIELD_USER_PERSON)) {
5562
UserPerson userPerson = context.deserialize(
5663
userBody.get(FIELD_USER_PERSON),
5764
UserPerson.class
5865
);
5966

6067
return new SessionServer(id, token, userPerson);
68+
} else if (userBody.has(FIELD_USER_API_KEY)) {
69+
UserApiKey userApiKey = context.deserialize(
70+
userBody.get(FIELD_USER_API_KEY),
71+
UserApiKey.class
72+
);
73+
74+
return new SessionServer(id, token, userApiKey);
75+
} else {
76+
throw new BunqException(ERROR_COULD_NOT_DETERMINE_USER);
6177
}
6278
}
6379

src/main/java/com/bunq/sdk/model/core/SessionServer.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.bunq.sdk.http.ApiClient;
66
import com.bunq.sdk.http.BunqResponse;
77
import com.bunq.sdk.http.BunqResponseRaw;
8+
import com.bunq.sdk.model.generated.endpoint.UserApiKey;
89
import com.bunq.sdk.model.generated.endpoint.UserCompany;
910
import com.bunq.sdk.model.generated.endpoint.UserPerson;
1011
import java.util.HashMap;
@@ -30,6 +31,7 @@ public class SessionServer extends BunqModel {
3031
private SessionToken sessionToken;
3132
private UserCompany userCompany;
3233
private UserPerson userPerson;
34+
private UserApiKey userApiKey;
3335

3436
public SessionServer(Id id, SessionToken sessionToken, UserCompany userCompany) {
3537
this.id = id;
@@ -43,6 +45,12 @@ public SessionServer(Id id, SessionToken sessionToken, UserPerson userPerson) {
4345
this.userPerson = userPerson;
4446
}
4547

48+
public SessionServer(Id id, SessionToken sessionToken, UserApiKey userApiKey) {
49+
this.id = id;
50+
this.sessionToken = sessionToken;
51+
this.userApiKey = userApiKey;
52+
}
53+
4654
/**
4755
* Create a new session for a DeviceServer. Provide the Installation token
4856
* in the "X-Bunq-Client-Authentication" header. And don't forget to create
@@ -92,26 +100,32 @@ public boolean isAllFieldNull() {
92100
return false;
93101
}
94102

95-
if (this.sessionToken == null) {
103+
if (this.sessionToken != null) {
104+
return false;
105+
}
106+
107+
if (this.userCompany != null) {
96108
return false;
97109
}
98110

99-
if (this.userCompany == null) {
111+
if (this.userPerson != null) {
100112
return false;
101113
}
102114

103-
if (this.userPerson == null) {
115+
if (this.userApiKey != null) {
104116
return false;
105117
}
106118

107119
return true;
108120
}
109121

110-
public BunqModel getReferencedObject() {
111-
if (this.userCompany == null) {
122+
public BunqModel getReferencedUser() {
123+
if (this.userCompany == null && this.userApiKey == null && this.userPerson != null) {
112124
return this.userPerson;
113-
} else if (this.userPerson == null) {
125+
} else if (this.userPerson == null && this.userApiKey == null && this.userCompany != null) {
114126
return this.userCompany;
127+
} else if (this.userPerson == null && this.userCompany == null && this.userApiKey != null) {
128+
return this.userApiKey;
115129
} else {
116130
throw new BunqException(ERROR_ALL_FIELD_NULL);
117131
}

0 commit comments

Comments
 (0)