forked from GJWT/javaOIDCMsg
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
194 lines (161 loc) · 5.88 KB
/
State.java
File metadata and controls
194 lines (161 loc) · 5.88 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
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
package oiccli;
import com.google.common.base.Strings;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import oiccli.exceptions.ExpiredToken;
import oiccli.exceptions.UnknownState;
import sun.swing.plaf.synth.DefaultSynthStyle;
public class State {
private String clientId;
private Database db;
private int lifetime;
public State(String clientId, Database db, String dbName, int lifetime) {
this.clientId = clientId;
if (db == null) {
if (!Strings.isNullOrEmpty(dbName)) {
this.db = shelve.open(dbName, true);
} else {
this.db = new Database();
}
} else {
this.db = db;
}
this.lifetime = lifetime;
}
public State(String clientId, Database db, String dbName) {
this(clientId, db, dbName, 600);
}
public String createState(final String receiver, Object request) {
String state = StringUtil.generateRandomString(24);
final long now = System.currentTimeMillis();
Map<String, Object> info = new HashMap<String, Object>() {{
put("clientId", clientId);
put("as", receiver);
put("iat", now);
}};
switch (state) {
case "1":
this.db.set1(info);
break;
case "2":
this.db.set2(info);
break;
case "3":
this.db.set3(info);
break;
}
return state;
}
public Map<String, Map<String, Object>> updateTokenInfo(Map<String, Map<String, Object>> info, AuthorizationResponse authorizationResponse) {
Map<String, Object> hMap = info.get("token");
if (hMap == null) {
hMap = new HashMap<>();
}
String token = authorizationResponse.getAccessToken();
hMap.put("accessToken", token);
Integer expiresAtInteger = authorizationResponse.getExpiresIn();
int expiresAt;
if (expiresAtInteger != null) {
expiresAt = expiresAtInteger.intValue();
hMap.put("exp", System.currentTimeMillis() + expiresAt);
hMap.put("expiresIn", expiresAt);
} else {
hMap.put("exp", System.currentTimeMillis() + ((Long) hMap.get("expiresIn")).longValue());
}
hMap.put("tokenType", authorizationResponse.getTokenType());
hMap.put("scope", authorizationResponse.getScope());
info.put("token", hMap);
return info;
}
public Map<String, Map<String, Object>> addMessageInfo(AuthorizationResponse authorizationResponse, String state) {
if (state == null) {
state = authorizationResponse.getState();
}
//_info = self[state] what are all the available types so i can create a switch/case?
info.put("code", authorizationResponse.getCode());
this.updateTokenInfo(info, authorizationResponse);
info.put("idToken", authorizationResponse.getIdToken());
info.put("refreshToken", authorizationResponse.getRefreshToken());
switch (state) {
case "1":
this.db.set1(info);
break;
case "2":
this.db.set2(info);
break;
case "3":
this.db.set3(info);
break;
}
return info;
}
public Map<String, String> addInfo(String state, Map<String, String> args) {
Map<String, String> info = this.get(state);
info.update(args);
switch (state) {
case "1":
this.db.set1(info);
break;
case "2":
this.db.set2(info);
break;
case "3":
this.db.set3(info);
break;
}
return info;
}
public Map<String, Object> getTokenInfo(String state, long now) throws ExpiredToken {
//_tinfo = self[state]['token']
//_exp = _tinfo['exp']
if (now == 0) {
now = System.currentTimeMillis();
}
if (now > _exp) {
throw new ExpiredToken("Passed best before");
}
return _tinfo;
}
public Map<String, Object> getTokenInfo(String state) throws ExpiredToken {
return getTokenInfo(state, 0);
}
public Map<String, Object> getResponseArgs(String state, AccessTokenRequest request, int now) throws ExpiredToken {
Map<String, String> info = state.getState(state);
Map<String, Object> responseArgs = new HashMap<>();
for (String claim : request.c_param) {
if (claim.equals("accessToken")) {
Map<String, Object> tInfo = this.getTokenInfo(state, now);
if (tInfo == null) {
continue;
}
responseArgs.put(claim, tInfo.get("accessToken"));
} else {
responseArgs.put(claim, info.get(claim));
}
}
return responseArgs;
}
public DefaultSynthStyle.StateInfo addResponse(AuthorizationResponse authorizationResponse, String state) throws UnknownState {
if (Strings.isNullOrEmpty(state)) {
state = authorizationResponse.getState();
}
DefaultSynthStyle.StateInfo stateInfo = this.getState(state);
if (stateInfo == null) {
throw new UnknownState(state);
}
stateInfo.setCode(authorizationResponse.getCode());
this.updateTokenInfo(stateInfo, authorizationResponse);
for (String claim : Arrays.asList("idToken", "refreshToken")) {
stateInfo.setClaim(authorizationResponse.getClaim());
}
this.setState(stateInfo);
return stateInfo;
}
public String getIdToken(String state) {
return this.get(state).get("idToken");
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
}