Search in sources :

Example 1 with Bearer

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;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Date(java.util.Date) IEntityGroup(org.apereo.portal.groups.IEntityGroup) IGroupMember(org.apereo.portal.groups.IGroupMember) IPersonAttributes(org.jasig.services.persondir.IPersonAttributes) PortletSession(javax.portlet.PortletSession) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) ArrayList(java.util.ArrayList) List(java.util.List) Bearer(org.apereo.portal.soffit.model.v1_0.Bearer) BasicHeader(org.apache.http.message.BasicHeader)

Example 2 with Bearer

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);
}
Also used : Definition(org.apereo.portal.soffit.model.v1_0.Definition) ModelAndView(org.springframework.web.servlet.ModelAndView) Preferences(org.apereo.portal.soffit.model.v1_0.Preferences) Bearer(org.apereo.portal.soffit.model.v1_0.Bearer) PortalRequest(org.apereo.portal.soffit.model.v1_0.PortalRequest) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with Bearer

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...
    }
}
Also used : Definition(org.apereo.portal.soffit.model.v1_0.Definition) HttpServletResponse(javax.servlet.http.HttpServletResponse) Method(java.lang.reflect.Method) PortalRequest(org.apereo.portal.soffit.model.v1_0.PortalRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Preferences(org.apereo.portal.soffit.model.v1_0.Preferences) Bearer(org.apereo.portal.soffit.model.v1_0.Bearer) Test(org.junit.Test)

Example 4 with Bearer

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]));
    }
}
Also used : Definition(org.apereo.portal.soffit.model.v1_0.Definition) HttpServletResponse(javax.servlet.http.HttpServletResponse) Method(java.lang.reflect.Method) PortalRequest(org.apereo.portal.soffit.model.v1_0.PortalRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Preferences(org.apereo.portal.soffit.model.v1_0.Preferences) Bearer(org.apereo.portal.soffit.model.v1_0.Bearer) Test(org.junit.Test)

Example 5 with Bearer

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);
}
Also used : Claims(io.jsonwebtoken.Claims) Bearer(org.apereo.portal.soffit.model.v1_0.Bearer)

Aggregations

Bearer (org.apereo.portal.soffit.model.v1_0.Bearer)6 Definition (org.apereo.portal.soffit.model.v1_0.Definition)3 PortalRequest (org.apereo.portal.soffit.model.v1_0.PortalRequest)3 Preferences (org.apereo.portal.soffit.model.v1_0.Preferences)3 Claims (io.jsonwebtoken.Claims)2 Method (java.lang.reflect.Method)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 PortletSession (javax.portlet.PortletSession)1 Header (org.apache.http.Header)1 BasicHeader (org.apache.http.message.BasicHeader)1 IEntityGroup (org.apereo.portal.groups.IEntityGroup)1 IGroupMember (org.apereo.portal.groups.IGroupMember)1 IPersonAttributes (org.jasig.services.persondir.IPersonAttributes)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1