use of org.apereo.portal.soffit.model.v1_0.Bearer in project uPortal by Jasig.
the class AuthorizationHeaderProvider method createHeader.
@Override
public Header createHeader(RenderRequest renderRequest, RenderResponse renderResponse) {
// Username
final String username = getUsername(renderRequest);
// Attributes
final Map<String, List<String>> attributes = new HashMap<>();
final IPersonAttributes person = personAttributeDao.getPerson(username);
if (person != null) {
for (Entry<String, List<Object>> y : person.getAttributes().entrySet()) {
final List<String> values = new ArrayList<>();
for (Object value : y.getValue()) {
if (value instanceof String) {
values.add((String) value);
}
}
attributes.put(y.getKey(), values);
}
}
logger.debug("Found the following user attributes for username='{}': {}", username, attributes);
// Groups
final List<String> groups = new ArrayList<>();
final IGroupMember groupMember = GroupService.getGroupMember(username, IPerson.class);
if (groupMember != null) {
Set<IEntityGroup> ancestors = groupMember.getAncestorGroups();
for (IEntityGroup g : ancestors) {
groups.add(g.getName());
}
}
logger.debug("Found the following group affiliations for username='{}': {}", username, groups);
// Expiration of the Bearer token
final PortletSession portletSession = renderRequest.getPortletSession();
final Date expires = new Date(portletSession.getLastAccessedTime() + ((long) portletSession.getMaxInactiveInterval() * 1000L));
// Authorization header
final Bearer bearer = bearerService.createBearer(username, attributes, groups, expires);
final Header rslt = new BasicHeader(Headers.AUTHORIZATION.getName(), Headers.BEARER_TOKEN_PREFIX + bearer.getEncryptedToken());
logger.debug("Produced the following Authorization header for username='{}': {}", username, rslt);
return rslt;
}
use of org.apereo.portal.soffit.model.v1_0.Bearer in project uPortal by Jasig.
the class SoffitRendererController method render.
@RequestMapping(value = "/{module}", method = RequestMethod.GET)
public ModelAndView render(final HttpServletRequest req, final HttpServletResponse res, @PathVariable final String module) {
logger.debug("Rendering for request URI '{}'", req.getRequestURI());
// Soffit Object Model
final PortalRequest portalRequest = getPortalRequest(req);
final Bearer bearer = getBearer(req);
final Preferences preferences = getPreferences(req);
final Definition definition = getDefinition(req);
// Select a view
final String viewName = selectView(req, module, portalRequest);
final Map<String, Object> model = modelAttributeService.gatherModelAttributes(viewName, req, res, portalRequest, bearer, preferences, definition);
model.put(PORTAL_REQUEST_MODEL_NAME, portalRequest);
model.put(BEARER_MODEL_NAME, bearer);
model.put(PREFERENCES_MODEL_NAME, preferences);
model.put(DEFINITION_MODEL_NAME, definition);
// Set up cache headers appropriately
configureCacheHeaders(res, module);
return new ModelAndView(viewName, model);
}
use of org.apereo.portal.soffit.model.v1_0.Bearer in project uPortal by Jasig.
the class ModelAttributeServiceTest method testGetModelAttributeFromMethod.
@Test
public void testGetModelAttributeFromMethod() {
final ModelAttributeService modelAttributeService = new ModelAttributeService();
final Class[] parameterClasses = new Class[] { HttpServletRequest.class, PortalRequest.class, Bearer.class };
final Method method;
try {
method = getClass().getMethod("soffitModelAttributeMethod", parameterClasses);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
// Object Model
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
final HttpServletResponse res = Mockito.mock(HttpServletResponse.class);
final PortalRequest portalRequest = Mockito.mock(PortalRequest.class);
final Bearer bearer = Mockito.mock(Bearer.class);
final Preferences preferences = Mockito.mock(Preferences.class);
final Definition definition = Mockito.mock(Definition.class);
final Object modelAttribute = modelAttributeService.getModelAttributeFromMethod(this, method, req, res, portalRequest, bearer, preferences, definition);
assertEquals("Incorrect modelAttribute", modelAttribute, returnValue);
final Method brokenMethod;
try {
brokenMethod = getClass().getMethod("brokenSoffitModelAttributeMethod", parameterClasses);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
try {
modelAttributeService.getModelAttributeFromMethod(this, brokenMethod, req, res, portalRequest, bearer, preferences, definition);
fail("Expected IllegalStateException for void-declaring method");
} catch (IllegalStateException e) {
// Expected; fall through...
}
}
use of org.apereo.portal.soffit.model.v1_0.Bearer in project uPortal by Jasig.
the class ModelAttributeServiceTest method testPrepareMethodParameters.
@Test
public void testPrepareMethodParameters() {
final ModelAttributeService modelAttributeService = new ModelAttributeService();
final Class[] parameterClasses = new Class[] { HttpServletRequest.class, PortalRequest.class, Bearer.class };
final Method method;
try {
method = getClass().getMethod("soffitModelAttributeMethod", parameterClasses);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
// Object Model
final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
final HttpServletResponse res = Mockito.mock(HttpServletResponse.class);
final PortalRequest portalRequest = Mockito.mock(PortalRequest.class);
final Bearer bearer = Mockito.mock(Bearer.class);
final Preferences preferences = Mockito.mock(Preferences.class);
final Definition definition = Mockito.mock(Definition.class);
final Object[] parameters = modelAttributeService.prepareMethodParameters(method, req, res, portalRequest, bearer, preferences, definition);
assertEquals("parameterClasses and parameters arrays must be the same length", parameterClasses.length, parameters.length);
for (int i = 0; i < parameters.length; i++) {
assertTrue("Mismatched parameter type", parameterClasses[i].isInstance(parameters[i]));
}
}
use of org.apereo.portal.soffit.model.v1_0.Bearer in project uPortal by Jasig.
the class BearerService method createBearer.
public Bearer createBearer(String username, Map<String, List<String>> attributes, List<String> groups, Date expires) {
final Claims claims = createClaims(Bearer.class, username, expires);
/*
* User attributes; attribute names that match registered attributes
* (https://www.iana.org/assignments/jwt/jwt.xhtml) will be
* automatically portable.
*/
for (Map.Entry<String, List<String>> y : attributes.entrySet()) {
final String name = y.getKey();
switch(y.getValue().size()) {
case 0:
// Do nothing...
break;
case 1:
// Model as a single value (in this a good idea?)
claims.put(name, y.getValue().get(0));
break;
default:
// Retain the collection
claims.put(name, y.getValue());
break;
}
}
// Groups
claims.put(JwtClaims.GROUPS.getName(), groups);
return new Bearer(generateEncryptedToken(claims), username, attributes, groups);
}
Aggregations