Search in sources :

Example 1 with AssociationException

use of org.openid4java.association.AssociationException 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 AssociationException

use of org.openid4java.association.AssociationException in project spring-security by spring-projects.

the class OpenID4JavaConsumerTests method verificationExceptionsRaiseOpenIDException.

@Test
public void verificationExceptionsRaiseOpenIDException() throws Exception {
    ConsumerManager mgr = mock(ConsumerManager.class);
    OpenID4JavaConsumer consumer = new OpenID4JavaConsumer(mgr, new NullAxFetchListFactory());
    when(mgr.verify(anyString(), any(ParameterList.class), any(DiscoveryInformation.class))).thenThrow(new MessageException("")).thenThrow(new AssociationException("")).thenThrow(new DiscoveryException(""));
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setQueryString("x=5");
    try {
        consumer.endConsumption(request);
        fail("OpenIDConsumerException was not thrown");
    } catch (OpenIDConsumerException expected) {
    }
    try {
        consumer.endConsumption(request);
        fail("OpenIDConsumerException was not thrown");
    } catch (OpenIDConsumerException expected) {
    }
    try {
        consumer.endConsumption(request);
        fail("OpenIDConsumerException was not thrown");
    } catch (OpenIDConsumerException expected) {
    }
}
Also used : ConsumerManager(org.openid4java.consumer.ConsumerManager) MessageException(org.openid4java.message.MessageException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DiscoveryInformation(org.openid4java.discovery.DiscoveryInformation) ParameterList(org.openid4java.message.ParameterList) AssociationException(org.openid4java.association.AssociationException) DiscoveryException(org.openid4java.discovery.DiscoveryException)

Example 3 with AssociationException

use of org.openid4java.association.AssociationException in project oxTrust by GluuFederation.

the class OxChooserWebService method responseHandler.

@Path("/Response")
@GET
@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response responseHandler(@Context HttpServletRequest httpReq, @Context HttpServletResponse httpRes, ForwardedRequest frequest) throws ConsumerException {
    try {
        log.debug("instantiating a ParameterList ");
        ParameterList response = new ParameterList(frequest.getParameterMap());
        log.debug("getting DiscoveryInformation ");
        DiscoveryInformation discovered = (DiscoveryInformation) httpReq.getSession().getAttribute("openid-disc");
        log.debug("getting StringBuffer ");
        StringBuffer receivingURL = frequest.getRequestURL();
        log.debug("getting QueryString ");
        String queryString = frequest.getQueryString();
        if (queryString != null && queryString.length() > 0)
            log.debug("getting receivingURL ");
        receivingURL.append("?").append(frequest.getQueryString());
        log.debug("getting VerificationResult ");
        VerificationResult verification = manager.verify(receivingURL.toString(), response, discovered);
        log.debug("getting VerificationResult ");
        Identifier verified = verification.getVerifiedId();
        log.debug(" VerificationResult retrieved ");
        if (verified != null) {
            log.debug("verified != null");
            AuthSuccess authSuccess = (AuthSuccess) verification.getAuthResponse();
            if (authSuccess.hasExtension(AxMessage.OPENID_NS_AX)) {
                log.debug("getting FetchResponse");
                FetchResponse fetchResp = (FetchResponse) authSuccess.getExtension(AxMessage.OPENID_NS_AX);
                log.debug("getting emails");
                List emails = fetchResp.getAttributeValues("email");
                log.debug("getting FirstName");
                String firstName = fetchResp.getAttributeValue("firstname");
                log.debug("getting LastName");
                String lastName = fetchResp.getAttributeValue("lastname");
                log.debug("getting one Email");
                String email = (String) emails.get(0);
                log.debug("email : ", email);
                String nickName = fetchResp.getAttributeValue("nickname");
                String Image = fetchResp.getAttributeValue("image");
                String Language = fetchResp.getAttributeValue("language");
                String Country = fetchResp.getAttributeValue("country");
                String Timezone = fetchResp.getAttributeValue("timezone");
                String Gender = fetchResp.getAttributeValue("gender");
                String Fullname = fetchResp.getAttributeValue("fullname");
                IdentityResponse idResponse = new IdentityResponse();
                idResponse.setFirstname(firstName);
                idResponse.setLastname(lastName);
                idResponse.setEmail(email);
                idResponse.setNickname(nickName);
                idResponse.setImage(Image);
                idResponse.setLanguage(Language);
                idResponse.setCountry(Country);
                idResponse.setTimezone(Timezone);
                idResponse.setGender(Gender);
                idResponse.setFullname(Fullname);
                return Response.ok(idResponse).build();
            }
            return errorResponse("Could not get fetched attributes");
        }
    } catch (AssociationException e) {
        return errorResponse("An AssociationException occured , please check your request.");
    } catch (MessageException e) {
        return errorResponse("An MessageException occured , please check your request.");
    } catch (DiscoveryException e) {
        return errorResponse("An DiscoveryException occured , please check your request.");
    } finally {
        identity.logout();
    }
    return errorResponse("An Error occured , please check your request.");
}
Also used : IdentityResponse(org.gluu.oxtrust.model.oxchooser.IdentityResponse) FetchResponse(org.openid4java.message.ax.FetchResponse) Identifier(org.openid4java.discovery.Identifier) VerificationResult(org.openid4java.consumer.VerificationResult) MessageException(org.openid4java.message.MessageException) DiscoveryInformation(org.openid4java.discovery.DiscoveryInformation) AuthSuccess(org.openid4java.message.AuthSuccess) ParameterList(org.openid4java.message.ParameterList) AssociationException(org.openid4java.association.AssociationException) List(java.util.List) ParameterList(org.openid4java.message.ParameterList) DiscoveryException(org.openid4java.discovery.DiscoveryException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

AssociationException (org.openid4java.association.AssociationException)3 DiscoveryException (org.openid4java.discovery.DiscoveryException)3 DiscoveryInformation (org.openid4java.discovery.DiscoveryInformation)3 MessageException (org.openid4java.message.MessageException)3 ParameterList (org.openid4java.message.ParameterList)3 List (java.util.List)2 VerificationResult (org.openid4java.consumer.VerificationResult)2 Identifier (org.openid4java.discovery.Identifier)2 ArrayList (java.util.ArrayList)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 Produces (javax.ws.rs.Produces)1 IdentityResponse (org.gluu.oxtrust.model.oxchooser.IdentityResponse)1 ConsumerManager (org.openid4java.consumer.ConsumerManager)1 AuthSuccess (org.openid4java.message.AuthSuccess)1 FetchResponse (org.openid4java.message.ax.FetchResponse)1 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1