public void process(OpenIdTestService s, AuthRequest authReq, Message rep) throws MessageException { if (authReq.hasExtension(SRegMessage.OPENID_NS_SREG)) { MessageExtension ext = authReq.getExtension(SRegMessage.OPENID_NS_SREG); if (ext instanceof SRegRequest) { SRegRequest sregReq = (SRegRequest) ext; SRegResponse sregRes = SRegResponse.createFetchResponse(); List<String> attrs = (List<String>) sregReq.getAttributes(); if (attrs.contains("fullname") && s.props.containsKey(IdProperty.fullName)) { sregRes.addAttribute("fullname", s.props.get(IdProperty.fullName)); } if (attrs.contains("nickname") && s.props.containsKey(IdProperty.nick)) { sregRes.addAttribute("nickname", s.props.get(IdProperty.nick)); } if (attrs.contains("email") && s.props.containsKey(IdProperty.email)) { sregRes.addAttribute("email", s.props.get(IdProperty.email)); } rep.addExtension(sregRes); } } }
@SuppressWarnings("unchecked") private void storeRequestedAttributesToSession(IWContext iwc, AuthRequest authReq) { if (authReq.hasExtension(AxMessage.OPENID_NS_AX)) { MessageExtension ext; try { ext = authReq.getExtension(AxMessage.OPENID_NS_AX); if (ext instanceof FetchRequest) { FetchRequest fetchReq = (FetchRequest) ext; OpenIDServerBean serverBean = ELUtil.getInstance().getBean("openIDServerBean"); Map<String, String> allAttributes = (Map<String, String>) fetchReq.getAttributes(); Map<String, String> required = (Map<String, String>) fetchReq.getAttributes(true); List<AuthorizedAttribute> allAttributesList = new ArrayList<AuthorizedAttribute>(); List<AuthorizedAttribute> requiredAttributesList = new ArrayList<AuthorizedAttribute>(); List<AuthorizedAttribute> optionalAttributesList = new ArrayList<AuthorizedAttribute>(); String realm = serverBean.getRealm(); for (String alias : allAttributes.keySet()) { ExchangeAttribute attribute = getDAO().getExchangeAttribute(alias, allAttributes.get(alias)); if (attribute != null) { User user = iwc.getCurrentUser(); AuthorizedAttribute aattr = getDAO().getAuthorizedAttributes(user.getUniqueId(), realm, attribute); if (aattr == null) { aattr = new AuthorizedAttribute(); aattr.setExchangeAttribute(attribute); aattr.setRealm(realm); aattr.setUserUUID(user.getUniqueId()); aattr.setIsAllowed(true); } allAttributesList.add(aattr); if (required.containsKey(alias) && attribute.getType().equals(required.get(alias))) { requiredAttributesList.add(aattr); } else { optionalAttributesList.add(aattr); } } else { LOGGER.warning( "Requesting unknown exchange attribute: " + alias + " : " + allAttributes.get(alias)); // throw new UnsupportedOperationException("Requesting unknown exchange attribute."); } } serverBean.setRequestedAttributes(allAttributesList); serverBean.setRequiredAttributes(requiredAttributesList); serverBean.setOptionalAttributes(optionalAttributesList); return; } else /*if (ext instanceof StoreRequest)*/ { // TODO implement? throw new UnsupportedOperationException("TODO"); } } catch (MessageException e) { e.printStackTrace(); } } if (authReq.hasExtension(SRegMessage.OPENID_NS_SREG11)) { try { MessageExtension ext = authReq.getExtension(SRegMessage.OPENID_NS_SREG11); if (ext instanceof SRegRequest) { SRegRequest sregReq = (SRegRequest) ext; List<String> all = sregReq.getAttributes(); List<String> required = sregReq.getAttributes(true); List<String> optional = sregReq.getAttributes(false); List<AuthorizedAttribute> allAttributesList = new ArrayList<AuthorizedAttribute>(); List<AuthorizedAttribute> requiredAttributesList = new ArrayList<AuthorizedAttribute>(); List<AuthorizedAttribute> optionalAttributesList = new ArrayList<AuthorizedAttribute>(); OpenIDServerBean serverBean = ELUtil.getInstance().getBean("openIDServerBean"); String realm = serverBean.getRealm(); User user = iwc.getCurrentUser(); for (String alias : all) { ExchangeAttribute attribute = getDAO().getExchangeAttribute(alias); if (attribute != null) { AuthorizedAttribute aattr = getDAO().getAuthorizedAttributes(user.getUniqueId(), realm, attribute); if (aattr == null) { aattr = new AuthorizedAttribute(); aattr.setExchangeAttribute(attribute); aattr.setRealm(realm); aattr.setUserUUID(user.getUniqueId()); aattr.setIsAllowed(true); } allAttributesList.add(aattr); if (required.contains(alias)) { requiredAttributesList.add(aattr); } if (optional.contains(alias)) { optionalAttributesList.add(aattr); } } } serverBean.setRequestedAttributes(allAttributesList); serverBean.setRequiredAttributes(requiredAttributesList); serverBean.setOptionalAttributes(optionalAttributesList); return; } } catch (MessageException e) { e.printStackTrace(); } } }
protected String[] prepareResponse( OpenIDServerBean serverBean, Message response, IWContext iwc, User user, AuthRequest authReq) throws MessageException { List<String> extensionsToSign = new ArrayList<String>(); if (authReq.hasExtension(AxMessage.OPENID_NS_AX)) { try { MessageExtension ext = authReq.getExtension(AxMessage.OPENID_NS_AX); if (ext instanceof FetchRequest) { FetchRequest fetchReq = (FetchRequest) ext; Map<String, String> requestedAttributes = fetchReq.getAttributes(); Map userDataExt = new HashMap(); FetchResponse fetchResp = FetchResponse.createFetchResponse(fetchReq, userDataExt); List<AuthorizedAttribute> s = serverBean.getRequestedAttributes(); Set<String> keys = requestedAttributes.keySet(); Collection<String> types = requestedAttributes.values(); for (AuthorizedAttribute a : s) { ExchangeAttribute attr = a.getExchangeAttribute(); String alias = attr.getName(); String type = attr.getType(); if (keys.contains(alias) || types.contains(type)) { String value = getAttributeValue(iwc, user, alias, type); if (value == null) { value = ""; } fetchResp.addAttribute(alias, type, value); } else { // FetchRequest not asking for this attribute throw new UnsupportedOperationException( "Processed and requested attributes do not match."); } } response.addExtension(fetchResp); extensionsToSign.add(AxMessage.OPENID_NS_AX); } else /*if (ext instanceof StoreRequest)*/ { throw new UnsupportedOperationException("TODO"); } } catch (MessageException me) { System.err.println(me.getMessage()); } } if (authReq.hasExtension(SRegMessage.OPENID_NS_SREG11)) { MessageExtension ext = authReq.getExtension(SRegMessage.OPENID_NS_SREG11); if (ext instanceof SRegRequest) { SRegRequest sregReq = (SRegRequest) ext; List<String> required = sregReq.getAttributes(true); List<String> optional = sregReq.getAttributes(false); Map userData = new HashMap(); for (String alias : required) { String value = getAttributeValue(iwc, user, alias, null); if (alias.length() > 0 && value != null) { userData.put(alias, value); } else if (alias.length() > 0) { throw new UnsupportedOperationException("Required attribute not supported: " + alias); } } for (String alias : optional) { String value = getAttributeValue(iwc, user, alias, null); if (alias.length() > 0 && value != null) { userData.put(alias, value); } } SRegResponse sregResp = SRegResponse.createSRegResponse(sregReq, userData); response.addExtension(sregResp); extensionsToSign.add(SRegMessage.OPENID_NS_SREG11); } else if (ext instanceof SRegResponse) { response.addExtension(ext); } } return extensionsToSign.toArray(new String[extensionsToSign.size()]); }
/** * Authenticate an user with the provider * * @param adapter protocol adapter * @param providerInfo Information about a provider derived from discovery process * @return * @throws OpenIDDiscoveryException * @throws OpenIDConsumerException * @throws OpenIDMessageException * @throws OpenIDProtocolException */ @SuppressWarnings("unchecked") public boolean authenticate(OpenIDProtocolAdapter adapter, OpenIDProviderInformation providerInfo) throws OpenIDDiscoveryException, OpenIDConsumerException, OpenIDMessageException, OpenIDProtocolException { DiscoveryInformation discovered = providerInfo.get(); // obtain a AuthRequest message to be sent to the OpenID provider try { AuthRequest authReq = consumerManager.authenticate(discovered, adapter.getReturnURL()); // Attribute Exchange example: fetching the 'email' attribute FetchRequest fetch = FetchRequest.createFetchRequest(); SRegRequest sregReq = SRegRequest.createFetchRequest(); OpenIDAttributeMap amap = adapter.getAttributeMap(); if ("1".equals(amap.get("nickname"))) { // fetch.addAttribute("nickname", // "http://schema.openid.net/contact/nickname", false); sregReq.addAttribute("nickname", false); } if ("1".equals(amap.get("email"))) { fetch.addAttribute("email", OpenIDConstants.EMAIL.url(), false); sregReq.addAttribute("email", false); } if ("1".equals(amap.get("fullname"))) { fetch.addAttribute("fullname", OpenIDConstants.FULLNAME.url(), false); sregReq.addAttribute("fullname", false); } if ("1".equals(amap.get("dob"))) { fetch.addAttribute("dob", OpenIDConstants.DOB.url(), true); sregReq.addAttribute("dob", false); } if ("1".equals(amap.get("gender"))) { fetch.addAttribute("gender", OpenIDConstants.GENDER.url(), false); sregReq.addAttribute("gender", false); } if ("1".equals(amap.get("postcode"))) { fetch.addAttribute("postcode", OpenIDConstants.POSTCODE.url(), false); sregReq.addAttribute("postcode", false); } if ("1".equals(amap.get("country"))) { fetch.addAttribute("country", OpenIDConstants.COUNTRY.url(), false); sregReq.addAttribute("country", false); } if ("1".equals(amap.get("language"))) { fetch.addAttribute("language", OpenIDConstants.LANGUAGE.url(), false); sregReq.addAttribute("language", false); } if ("1".equals(amap.get("timezone"))) { fetch.addAttribute("timezone", OpenIDConstants.TIMEZONE.url(), false); sregReq.addAttribute("timezone", false); } // attach the extension to the authentication request if (!sregReq.getAttributes().isEmpty()) { authReq.addExtension(sregReq); } if (!discovered.isVersion2()) { // Option 1: GET HTTP-redirect to the OpenID Provider endpoint // The only method supported in OpenID 1.x // redirect-URL usually limited ~2048 bytes adapter.sendToProvider(1, authReq.getDestinationUrl(true), null); return true; } else { // Option 2: HTML FORM Redirection (Allows payloads >2048 bytes) adapter.sendToProvider(2, authReq.getDestinationUrl(false), authReq.getParameterMap()); } } catch (MessageException e) { throw new OpenIDMessageException(e); } catch (ConsumerException e) { throw new OpenIDConsumerException(e); } return false; }