Search in sources :

Example 1 with ParameterList

use of org.openid4java.message.ParameterList in project spring-security by spring-projects.

the class OpenID4JavaConsumer method endConsumption.

public OpenIDAuthenticationToken endConsumption(HttpServletRequest request) throws OpenIDConsumerException {
    // extract the parameters from the authentication response
    // (which comes in as a HTTP request from the OpenID provider)
    ParameterList openidResp = new ParameterList(request.getParameterMap());
    // retrieve the previously stored discovery information
    DiscoveryInformation discovered = (DiscoveryInformation) request.getSession().getAttribute(DISCOVERY_INFO_KEY);
    if (discovered == null) {
        throw new OpenIDConsumerException("DiscoveryInformation is not available. Possible causes are lost session or replay attack");
    }
    List<OpenIDAttribute> attributesToFetch = (List<OpenIDAttribute>) request.getSession().getAttribute(ATTRIBUTE_LIST_KEY);
    request.getSession().removeAttribute(DISCOVERY_INFO_KEY);
    request.getSession().removeAttribute(ATTRIBUTE_LIST_KEY);
    // extract the receiving URL from the HTTP request
    StringBuffer receivingURL = request.getRequestURL();
    String queryString = request.getQueryString();
    if (StringUtils.hasLength(queryString)) {
        receivingURL.append("?").append(request.getQueryString());
    }
    // verify the response
    VerificationResult verification;
    try {
        verification = consumerManager.verify(receivingURL.toString(), openidResp, discovered);
    } catch (MessageException e) {
        throw new OpenIDConsumerException("Error verifying openid response", e);
    } catch (DiscoveryException e) {
        throw new OpenIDConsumerException("Error verifying openid response", e);
    } catch (AssociationException e) {
        throw new OpenIDConsumerException("Error verifying openid response", e);
    }
    // examine the verification result and extract the verified identifier
    Identifier verified = verification.getVerifiedId();
    if (verified == null) {
        Identifier id = discovered.getClaimedIdentifier();
        return new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.FAILURE, id == null ? "Unknown" : id.getIdentifier(), "Verification status message: [" + verification.getStatusMsg() + "]", Collections.<OpenIDAttribute>emptyList());
    }
    List<OpenIDAttribute> attributes = fetchAxAttributes(verification.getAuthResponse(), attributesToFetch);
    return new OpenIDAuthenticationToken(OpenIDAuthenticationStatus.SUCCESS, verified.getIdentifier(), "some message", attributes);
}
Also used : Identifier(org.openid4java.discovery.Identifier) VerificationResult(org.openid4java.consumer.VerificationResult) MessageException(org.openid4java.message.MessageException) DiscoveryInformation(org.openid4java.discovery.DiscoveryInformation) ParameterList(org.openid4java.message.ParameterList) AssociationException(org.openid4java.association.AssociationException) ParameterList(org.openid4java.message.ParameterList) ArrayList(java.util.ArrayList) List(java.util.List) DiscoveryException(org.openid4java.discovery.DiscoveryException)

Example 2 with ParameterList

use of org.openid4java.message.ParameterList in project cas by apereo.

the class OpenIdValidateController method handleRequestInternal.

@Override
protected ModelAndView handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final String openIdMode = request.getParameter(OpenIdProtocolConstants.OPENID_MODE);
    if (StringUtils.equals(openIdMode, OpenIdProtocolConstants.CHECK_AUTHENTICATION)) {
        final VerifyResponse message = (VerifyResponse) this.serverManager.verify(new ParameterList(request.getParameterMap()));
        final Map<String, String> parameters = new HashMap<>();
        parameters.putAll(message.getParameterMap());
        if (message.isSignatureVerified()) {
            LOGGER.debug("Signature verification request successful.");
            return new ModelAndView(getSuccessView(), VIEW_MODEL_KEY_PARAMETERS, parameters);
        } else {
            LOGGER.debug("Signature verification request unsuccessful.");
            return new ModelAndView(getFailureView(), VIEW_MODEL_KEY_PARAMETERS, parameters);
        }
    } else {
        // since we only deal OpenId signature verification
        return super.handleRequestInternal(request, response);
    }
}
Also used : VerifyResponse(org.openid4java.message.VerifyResponse) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) ParameterList(org.openid4java.message.ParameterList)

Example 3 with ParameterList

use of org.openid4java.message.ParameterList in project cas by apereo.

the class SmartOpenIdController method getAssociationResponse.

/**
     * Gets the association response. Determines the mode first.
     * If mode is set to associate, will set the response. Then
     * builds the response parameters next and returns.
     *
     * @param request the request
     * @return the association response
     */
public Map<String, String> getAssociationResponse(final HttpServletRequest request) {
    final ParameterList parameters = new ParameterList(request.getParameterMap());
    final String mode = parameters.hasParameter(OpenIdProtocolConstants.OPENID_MODE) ? parameters.getParameterValue(OpenIdProtocolConstants.OPENID_MODE) : null;
    Message response = null;
    if (StringUtils.equals(mode, OpenIdProtocolConstants.ASSOCIATE)) {
        response = this.serverManager.associationResponse(parameters);
    }
    final Map<String, String> responseParams = new HashMap<>();
    if (response != null) {
        responseParams.putAll(response.getParameterMap());
    }
    return responseParams;
}
Also used : Message(org.openid4java.message.Message) HashMap(java.util.HashMap) ParameterList(org.openid4java.message.ParameterList)

Example 4 with ParameterList

use of org.openid4java.message.ParameterList in project cas by apereo.

the class OpenIdServiceTests method verifyExpiredAssociationGetResponse.

@Test
public void verifyExpiredAssociationGetResponse() {
    try {
        request.removeParameter(OpenIdProtocolConstants.OPENID_ASSOCHANDLE);
        request.addParameter(OpenIdProtocolConstants.OPENID_ASSOCHANDLE, association.getHandle());
        openIdService = openIdServiceFactory.createService(request);
        final AuthenticationResult ctx = CoreAuthenticationTestUtils.getAuthenticationResult(getAuthenticationSystemSupport(), openIdService);
        final String tgt = centralAuthenticationService.createTicketGrantingTicket(ctx).getId();
        final String st = centralAuthenticationService.grantServiceTicket(tgt, openIdService, ctx).getId();
        centralAuthenticationService.validateServiceTicket(st, openIdService);
        synchronized (this) {
            try {
                this.wait(3000);
            } catch (final InterruptedException e) {
                fail("Could not wait long enough to check association expiry date");
            }
        }
        final ParameterList paramList = new ParameterList(request.getParameterMap());
        final Response response = new OpenIdServiceResponseBuilder(OPEN_ID_PREFIX_URL, serverManager, centralAuthenticationService).build(openIdService, st);
        assertNotNull(response);
        assertEquals(2, response.getAttributes().size());
        assertEquals("cancel", response.getAttributes().get(OpenIdProtocolConstants.OPENID_MODE));
    } catch (final Exception e) {
        LOGGER.debug("Exception during verification of service ticket", e);
    }
}
Also used : Response(org.apereo.cas.authentication.principal.Response) ParameterList(org.openid4java.message.ParameterList) IOException(java.io.IOException) AuthenticationResult(org.apereo.cas.authentication.AuthenticationResult) Test(org.junit.Test)

Example 5 with ParameterList

use of org.openid4java.message.ParameterList in project cas by apereo.

the class OpenIdServiceResponseBuilder method build.

/**
     * Generates an Openid response.
     * If no ticketId is found, response is negative.
     * If we have a ticket id, then we check if we have an association.
     * If so, we ask OpenId server manager to generate the answer according with the existing association.
     * If not, we send back an answer with the ticket id as association handle.
     * This will force the consumer to ask a verification, which will validate the service ticket.
     *
     * @param ticketId              the service ticket to provide to the service.
     * @param webApplicationService the service requesting an openid response
     * @return the generated authentication answer
     */
@Override
public Response build(final WebApplicationService webApplicationService, final String ticketId) {
    final OpenIdService service = (OpenIdService) webApplicationService;
    final ParameterList parameterList = new ParameterList(WebUtils.getHttpServletRequestFromRequestAttributes().getParameterMap());
    final Map<String, String> parameters = new HashMap<>();
    if (StringUtils.isBlank(ticketId)) {
        parameters.put(OpenIdProtocolConstants.OPENID_MODE, OpenIdProtocolConstants.CANCEL);
        return buildRedirect(service, parameters);
    }
    final Association association = getAssociation(serverManager, parameterList);
    final boolean associated = association != null;
    final boolean associationValid = isAssociationValid(association);
    boolean successFullAuthentication = true;
    Assertion assertion = null;
    try {
        if (associated && associationValid) {
            assertion = centralAuthenticationService.validateServiceTicket(ticketId, service);
            LOGGER.debug("Validated openid ticket [{}] for [{}]", ticketId, service);
        } else if (!associated) {
            LOGGER.debug("Responding to non-associated mode. Service ticket [{}] must be validated by the RP", ticketId);
        } else {
            LOGGER.warn("Association does not exist or is not valid");
            successFullAuthentication = false;
        }
    } catch (final AbstractTicketException e) {
        LOGGER.error("Could not validate ticket : [{}]", e.getMessage(), e);
        successFullAuthentication = false;
    }
    final String id = determineIdentity(service, assertion);
    return buildAuthenticationResponse(service, parameters, successFullAuthentication, id, parameterList);
}
Also used : Association(org.openid4java.association.Association) HashMap(java.util.HashMap) Assertion(org.apereo.cas.validation.Assertion) ParameterList(org.openid4java.message.ParameterList) AbstractTicketException(org.apereo.cas.ticket.AbstractTicketException)

Aggregations

ParameterList (org.openid4java.message.ParameterList)7 HashMap (java.util.HashMap)3 VerificationResult (org.openid4java.consumer.VerificationResult)3 MessageException (org.openid4java.message.MessageException)3 List (java.util.List)2 AssociationException (org.openid4java.association.AssociationException)2 DiscoveryException (org.openid4java.discovery.DiscoveryException)2 DiscoveryInformation (org.openid4java.discovery.DiscoveryInformation)2 Identifier (org.openid4java.discovery.Identifier)2 Message (org.openid4java.message.Message)2 FetchResponse (org.openid4java.message.ax.FetchResponse)2 AccountException (com.google.gerrit.server.account.AccountException)1 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Cookie (javax.servlet.http.Cookie)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1