diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/exception/UnknownUserInfoSchemaException.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/exception/UnknownUserInfoSchemaException.java new file mode 100644 index 000000000..07b4ee7ab --- /dev/null +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/exception/UnknownUserInfoSchemaException.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright 2012 The MITRE Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.mitre.openid.connect.exception; + +public class UnknownUserInfoSchemaException extends RuntimeException { + + public UnknownUserInfoSchemaException(String string) { + super(string); + } + + /** + * + */ + private static final long serialVersionUID = 1L; +} diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/view/POCOUserInfoView.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/view/POCOUserInfoView.java new file mode 100644 index 000000000..c2669a972 --- /dev/null +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/view/POCOUserInfoView.java @@ -0,0 +1,152 @@ +/******************************************************************************* + * Copyright 2012 The MITRE Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ******************************************************************************/ +package org.mitre.openid.connect.view; + +import java.io.Writer; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.mitre.openid.connect.model.UserInfo; +import org.springframework.validation.BeanPropertyBindingResult; +import org.springframework.web.servlet.view.AbstractView; + +import com.google.gson.ExclusionStrategy; +import com.google.gson.FieldAttributes; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +public class POCOUserInfoView extends AbstractView{ + + /* (non-Javadoc) + * @see org.springframework.web.servlet.view.AbstractView#renderMergedOutputModel(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse) + */ + protected void renderMergedOutputModel(Map model, + HttpServletRequest request, HttpServletResponse response) + throws Exception { + + UserInfo userInfo = (UserInfo) model.get("userInfo"); + + Gson gson = new GsonBuilder() + .setExclusionStrategies(new ExclusionStrategy() { + + public boolean shouldSkipField(FieldAttributes f) { + + return false; + } + + public boolean shouldSkipClass(Class clazz) { + // skip the JPA binding wrapper + if (clazz.equals(BeanPropertyBindingResult.class)) { + return true; + } + return false; + } + + }).create(); + + response.setContentType("application/json"); + Writer out = response.getWriter(); + gson.toJson(toPoco(userInfo),out); + } + + private JsonObject toPoco(UserInfo ui) { + JsonObject poco = new JsonObject(); + + // Envelope Info + poco.addProperty("startIndex", 0); + poco.addProperty("itemsPerPage", 1); + poco.addProperty("totalResults", 1); + + // Build the entry for this userInfo, then add it to entries, then add it to poco + JsonObject entry = new JsonObject(); + entry.addProperty("id", ui.getUserId()); + entry.addProperty("displayName", ui.getNickname()); + + if(ui.getFamilyName() != null + || ui.getGivenName() != null + || ui.getMiddleName() != null) { + JsonObject name = new JsonObject(); + name.addProperty("familyName", ui.getFamilyName()); + name.addProperty("givenName", ui.getGivenName()); + name.addProperty("middleName", ui.getMiddleName()); + entry.add("name", name); + } + + entry.addProperty("gender", ui.getGender()); + + if(ui.getEmail() != null) { + JsonObject email = new JsonObject(); + email.addProperty("value", ui.getEmail()); + + JsonArray emailArray = new JsonArray(); + emailArray.add(email); + entry.add("emails", emailArray); + } + + if(ui.getPhoneNumber() != null){ + JsonObject phone = new JsonObject(); + phone.addProperty("value", ui.getPhoneNumber()); + + JsonArray phoneArray = new JsonArray(); + phoneArray.add(phone); + entry.add("phoneNumbers", phoneArray); + } + + if(ui.getPicture() != null){ + JsonObject photo = new JsonObject(); + photo.addProperty("value", ui.getPicture()); + + JsonArray photoArray = new JsonArray(); + photoArray.add(photo); + entry.add("photos", photoArray); + } + + if(ui.getWebsite() != null) { + JsonObject website = new JsonObject(); + website.addProperty("value", ui.getWebsite()); + + JsonArray websiteArray = new JsonArray(); + websiteArray.add(website); + entry.add("urls", websiteArray); + } + + if(ui.getAddress() != null) { + JsonObject addr = new JsonObject(); + addr.addProperty("formatted", ui.getAddress().getFormatted()); + addr.addProperty("streetAddress", ui.getAddress().getStreetAddress()); + addr.addProperty("locality", ui.getAddress().getLocality()); + addr.addProperty("region", ui.getAddress().getRegion()); + addr.addProperty("postalCode", ui.getAddress().getPostalCode()); + addr.addProperty("country", ui.getAddress().getCountry()); + + JsonArray addrArray = new JsonArray(); + addrArray.add(addr); + entry.add("addresses", addrArray); + } + + entry.addProperty("updated", ui.getUpdatedTime()); + + JsonArray entryArray = new JsonArray(); + entryArray.add(entry); + poco.add("entry", entryArray); + return poco; + } + +} diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/UserInfoEndpoint.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/UserInfoEndpoint.java index 46bb933d2..07608769f 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/web/UserInfoEndpoint.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/web/UserInfoEndpoint.java @@ -20,6 +20,7 @@ import java.security.Principal; import org.mitre.oauth2.model.ClientDetailsEntity; import org.mitre.oauth2.model.OAuth2AccessTokenEntity; import org.mitre.oauth2.service.OAuth2TokenEntityService; +import org.mitre.openid.connect.exception.UnknownUserInfoSchemaException; import org.mitre.openid.connect.model.UserInfo; import org.mitre.openid.connect.service.UserInfoService; import org.springframework.beans.factory.annotation.Autowired; @@ -73,15 +74,11 @@ public class UserInfoEndpoint { } else if (schema.equalsIgnoreCase( pocoSchema )) { viewName = pocoUserInfoViewName; } else { - //TODO: Create an Error class *UnknownSchema* + throw new UnknownUserInfoSchemaException("Unknown User Info Schema: " + schema ); } - String userId = p.getName(); - UserInfo userInfo = userInfoService.getByUserId(userId); - return new ModelAndView(viewName, "userInfo", userInfo); - } diff --git a/openid-connect-server/src/main/webapp/WEB-INF/spring-servlet.xml b/openid-connect-server/src/main/webapp/WEB-INF/spring-servlet.xml index fa62241f8..cdd87c18a 100644 --- a/openid-connect-server/src/main/webapp/WEB-INF/spring-servlet.xml +++ b/openid-connect-server/src/main/webapp/WEB-INF/spring-servlet.xml @@ -165,6 +165,7 @@ +