forked from bunq/sdk_java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSessionContext.java
More file actions
139 lines (115 loc) · 4.67 KB
/
Copy pathSessionContext.java
File metadata and controls
139 lines (115 loc) · 4.67 KB
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
package com.bunq.sdk.context;
import com.bunq.sdk.exception.BunqException;
import com.bunq.sdk.model.core.BunqModel;
import com.bunq.sdk.model.core.SessionServer;
import com.bunq.sdk.model.generated.endpoint.UserApiKeyApiObject;
import com.bunq.sdk.model.generated.endpoint.UserCompanyApiObject;
import com.bunq.sdk.model.generated.endpoint.UserPaymentServiceProviderApiObject;
import com.bunq.sdk.model.generated.endpoint.UserPersonApiObject;
import com.bunq.sdk.util.BunqUtil;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.Date;
/**
* Context of your current bunq Public API session.
*/
public class SessionContext implements java.io.Serializable {
/**
* Error constants.
*/
private static final String ERROR_UNEXPECTED_USER_TYPE = "'%s' is an unexpected user type.";
/**
* Constant for converting milliseconds to seconds.
*/
private static final int MILLISECONDS_IN_SECOND = 1000;
/**
* Session token returned as a response to POST /session-server.
*/
@Expose
@SerializedName("token")
private final String token;
/**
* The time the session will expire.
*/
@Expose
@SerializedName("expiry_time")
private final Date expiryTime;
@Expose
@SerializedName("user_id")
private final Long userId;
@Expose
@SerializedName("user_person")
private final UserPersonApiObject userPerson;
@Expose
@SerializedName("user_company")
private final UserCompanyApiObject userCompany;
@Expose
@SerializedName("user_api_key")
private final UserApiKeyApiObject userApiKey;
@Expose
@SerializedName("user_payment_service_provider")
private final UserPaymentServiceProviderApiObject userPaymentServiceProvider;
/**
* @param sessionServer Object containing the session info.
*/
SessionContext(SessionServer sessionServer) {
this.token = sessionServer.getSessionToken().getToken();
this.expiryTime = calculateExpiryTime(sessionServer);
this.userId = getUserId(sessionServer.getReferencedUser());
this.userPerson = sessionServer.getUserPersonOrNull();
this.userCompany = sessionServer.getUserCompanyOrNull();
this.userApiKey = sessionServer.getUserApiKeyOrNull();
this.userPaymentServiceProvider = sessionServer.getUserPaymentServiceProviderOrNull();
}
private long getUserId(BunqModel user) {
if (user instanceof UserPersonApiObject) {
return ((UserPersonApiObject) user).getId();
} else if (user instanceof UserCompanyApiObject) {
return ((UserCompanyApiObject) user).getId();
} else if (user instanceof UserApiKeyApiObject) {
return ((UserApiKeyApiObject) user).getId();
} else if (user instanceof UserPaymentServiceProviderApiObject) {
return ((UserPaymentServiceProviderApiObject) user).getId();
} else {
throw new BunqException(String.format(ERROR_UNEXPECTED_USER_TYPE, user.getClass().toString()));
}
}
private static Date calculateExpiryTime(SessionServer sessionServer) {
Date expiryTime = new Date();
long sessionTimeoutMilliseconds = getSessionTimeout(sessionServer) * MILLISECONDS_IN_SECOND;
expiryTime.setTime(expiryTime.getTime() + sessionTimeoutMilliseconds);
return expiryTime;
}
private static long getSessionTimeout(SessionServer sessionServer) {
BunqModel user = sessionServer.getReferencedUser();
if (user instanceof UserApiKeyApiObject) {
BunqModel referencedUser = ((UserApiKeyApiObject) user).getRequestedByUser().getReferencedObject();
return getSessionTimeOutFromUser(referencedUser);
} else {
return getSessionTimeOutFromUser(user);
}
}
private static long getSessionTimeOutFromUser(BunqModel user) {
if (user instanceof UserCompanyApiObject) {
return ((UserCompanyApiObject) user).getSessionTimeout();
} else if (user instanceof UserPersonApiObject) {
return ((UserPersonApiObject) user).getSessionTimeout();
} else if (user instanceof UserPaymentServiceProviderApiObject) {
return ((UserPaymentServiceProviderApiObject) user).getSessionTimeout();
} else {
throw new BunqException(ERROR_UNEXPECTED_USER_TYPE);
}
}
String getToken() {
return token;
}
Date getExpiryTime() {
return expiryTime;
}
public Long getUserId() {
return userId;
}
public BunqModel getUserReference() {
return BunqUtil.getReferencedUser(userPerson, userCompany, userApiKey, userPaymentServiceProvider);
}
}