-
Notifications
You must be signed in to change notification settings - Fork 150
/
UmaGatherContext.java
204 lines (165 loc) · 6.36 KB
/
UmaGatherContext.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
/*
* oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
*
* Copyright (c) 2015, Gluu
*/
package org.xdi.oxauth.uma.authorization;
import org.gluu.jsf2.service.FacesService;
import org.xdi.model.SimpleCustomProperty;
import org.xdi.oxauth.model.common.SessionId;
import org.xdi.oxauth.model.common.User;
import org.xdi.oxauth.model.configuration.AppConfiguration;
import org.xdi.oxauth.model.exception.InvalidJwtException;
import org.xdi.oxauth.model.jwt.JwtClaims;
import org.xdi.oxauth.model.registration.Client;
import org.xdi.oxauth.model.uma.persistence.UmaPermission;
import org.xdi.oxauth.service.UserService;
import org.xdi.oxauth.service.external.context.ExternalScriptContext;
import org.xdi.oxauth.uma.service.RedirectParameters;
import org.xdi.oxauth.uma.service.UmaPctService;
import org.xdi.oxauth.uma.service.UmaPermissionService;
import org.xdi.oxauth.uma.service.UmaSessionService;
import org.xdi.oxauth.uma.ws.rs.UmaMetadataWS;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author yuriyz
* @version August 9, 2017
*/
public class UmaGatherContext extends ExternalScriptContext {
private final UmaSessionService sessionService;
private final UmaPermissionService permissionService;
private final UmaPctService pctService;
private final UserService userService;
private final FacesService facesService;
private final Map<String, SimpleCustomProperty> configurationAttributes;
private final AppConfiguration appConfiguration;
private final SessionId session;
private final RedirectParameters redirectUserParameters = new RedirectParameters();
private final UmaPCT pct;
private final JwtClaims claims;
private final Map<String, String> pageClaims;
private String redirectToExternalUrl = null;
public UmaGatherContext(Map<String, SimpleCustomProperty> configurationAttributes, HttpServletRequest httpRequest, SessionId session, UmaSessionService sessionService,
UmaPermissionService permissionService, UmaPctService pctService, Map<String, String> pageClaims,
UserService userService, FacesService facesService, AppConfiguration appConfiguration) {
super(httpRequest);
this.configurationAttributes = configurationAttributes;
this.session = session;
this.sessionService = sessionService;
this.permissionService = permissionService;
this.userService = userService;
this.pctService = pctService;
this.facesService = facesService;
this.pct = pctService.getByCode(sessionService.getPct(session));
this.claims = pct.getClaims();
this.pageClaims = pageClaims;
this.appConfiguration = appConfiguration;
}
public Map<String, SimpleCustomProperty> getConfigurationAttributes() {
return configurationAttributes;
}
public User getUser(String... returnAttributes) {
return sessionService.getUser(httpRequest, returnAttributes);
}
public String getUserDn() {
return sessionService.getUserDn(httpRequest);
}
public Client getClient() {
return sessionService.getClient(session);
}
public Map<String, String> getConnectSessionAttributes() {
SessionId connectSession = sessionService.getConnectSession(httpRequest);
if (connectSession != null) {
return new HashMap<String, String>(connectSession.getSessionAttributes());
}
return new HashMap<String, String>();
}
public boolean isAuthenticated() {
return getUser() != null;
}
public Map<String, String> getPageClaims() {
return pageClaims;
}
public Map<String, String[]> getRequestParameters() {
return httpRequest.getParameterMap();
}
public int getStep() {
return sessionService.getStep(session);
}
public void setStep(int step) {
sessionService.setStep(step, session);
}
public void addSessionAttribute(String key, String value) {
session.getSessionAttributes().put(key, value);
}
public void removeSessionAttribute(String key) {
session.getSessionAttributes().remove(key);
}
public Map<String, String> getSessionAttributes() {
return session.getSessionAttributes();
}
public void addRedirectUserParam(String paramName, String paramValue) {
redirectUserParameters.add(paramName, paramValue);
}
public void removeRedirectUserParameter(String paramName) {
redirectUserParameters.remove(paramName);
}
public RedirectParameters getRedirectUserParameters() {
return redirectUserParameters;
}
public Map<String, Set<String>> getRedirectUserParametersMap() {
return redirectUserParameters.map();
}
public List<UmaPermission> getPermissions() {
return permissionService.getPermissionsByTicket(sessionService.getTicket(session));
}
public JwtClaims getClaims() {
return claims;
}
public Object getClaim(String claimName) {
return claims.getClaim(claimName);
}
public void putClaim(String claimName, Object claimValue) {
claims.setClaimObject(claimName, claimValue, true);
}
public void removeClaim(String claimName) {
claims.removeClaim(claimName);
}
public boolean hasClaim(String claimName) {
return getClaim(claimName) != null;
}
/**
* Must not take any parameters
*/
public void persist() {
try {
pct.setClaims(claims);
} catch (InvalidJwtException e) {
getLog().error("Failed to persist claims", e);
}
sessionService.persist(session);
pctService.merge(pct);
}
public void redirectToExternalUrl(String url) {
redirectToExternalUrl = url;
}
public String getRedirectToExternalUrl() {
return redirectToExternalUrl;
}
public String getAuthorizationEndpoint() {
return appConfiguration.getAuthorizationEndpoint();
}
public String getIssuer() {
return appConfiguration.getIssuer();
}
public String getBaseEndpoint() {
return appConfiguration.getBaseEndpoint();
}
public String getClaimsGatheringEndpoint() {
return getBaseEndpoint() + UmaMetadataWS.UMA_CLAIMS_GATHERING_PATH;
}
}