use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.
the class AuthenticatedUserTest method testApplyDisplayInfo.
@Test
public void testApplyDisplayInfo() {
System.out.println("applyDisplayInfo");
AuthenticatedUserDisplayInfo inf = new AuthenticatedUserDisplayInfo("Homer", "Simpson", "Homer.Simpson@someU.edu", "UnitTester", "In-Memory user");
testUser.applyDisplayInfo(inf);
assertEquals(inf, testUser.getDisplayInfo());
}
use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.
the class GoogleOAuth2AP method parseUserResponse.
@Override
protected ParsedUserResponse parseUserResponse(String responseBody) {
try (StringReader rdr = new StringReader(responseBody);
JsonReader jrdr = Json.createReader(rdr)) {
JsonObject response = jrdr.readObject();
AuthenticatedUserDisplayInfo displayInfo = new AuthenticatedUserDisplayInfo(response.getString("given_name", ""), response.getString("family_name", ""), response.getString("email", ""), "", "");
String persistentUserId = response.getString("id");
String username = response.getString("email");
if (username != null) {
username = username.split("@")[0].trim();
} else {
// compose a username from given and family names
username = response.getString("given_name", "") + "." + response.getString("family_name", "");
username = username.trim();
if (username.isEmpty()) {
username = UUID.randomUUID().toString();
} else {
username = username.replaceAll(" ", "-");
}
}
return new ParsedUserResponse(displayInfo, persistentUserId, username);
}
}
use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.
the class OrcidOAuth2AP method parseActivitiesResponse.
protected AuthenticatedUserDisplayInfo parseActivitiesResponse(String responseBody) {
DocumentBuilderFactory dbFact = DocumentBuilderFactory.newInstance();
try (StringReader reader = new StringReader(responseBody)) {
DocumentBuilder db = dbFact.newDocumentBuilder();
Document doc = db.parse(new InputSource(reader));
String organization = getNodes(doc, "activities:employments", "employment:employment-summary", "employment:organization", "common:name").stream().findFirst().map(Node::getTextContent).map(String::trim).orElse(null);
String department = getNodes(doc, "activities:employments", "employment:employment-summary", "employment:department-name").stream().findFirst().map(Node::getTextContent).map(String::trim).orElse(null);
String role = getNodes(doc, "activities:employments", "employment:employment-summary", "employment:role-title").stream().findFirst().map(Node::getTextContent).map(String::trim).orElse(null);
String position = Stream.of(role, department).filter(Objects::nonNull).collect(joining(", "));
return new AuthenticatedUserDisplayInfo(null, null, null, organization, position);
} catch (SAXException ex) {
logger.log(Level.SEVERE, "XML error parsing response body from ORCiD: " + ex.getMessage(), ex);
} catch (IOException ex) {
logger.log(Level.SEVERE, "I/O error parsing response body from ORCiD: " + ex.getMessage(), ex);
} catch (ParserConfigurationException ex) {
logger.log(Level.SEVERE, "While parsing the ORCiD response: Bad parse configuration. " + ex.getMessage(), ex);
}
return null;
}
use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.
the class OrcidOAuth2AP method parseUserResponse.
@Override
protected ParsedUserResponse parseUserResponse(String responseBody) {
DocumentBuilderFactory dbFact = DocumentBuilderFactory.newInstance();
try (StringReader reader = new StringReader(responseBody)) {
DocumentBuilder db = dbFact.newDocumentBuilder();
Document doc = db.parse(new InputSource(reader));
String firstName = getNodes(doc, "person:person", "person:name", "personal-details:given-names").stream().findFirst().map(Node::getTextContent).map(String::trim).orElse("");
String familyName = getNodes(doc, "person:person", "person:name", "personal-details:family-name").stream().findFirst().map(Node::getTextContent).map(String::trim).orElse("");
// fallback - try to use the credit-name
if ((firstName + familyName).equals("")) {
firstName = getNodes(doc, "person:person", "person:name", "personal-details:credit-name").stream().findFirst().map(Node::getTextContent).map(String::trim).orElse("");
}
String primaryEmail = getPrimaryEmail(doc);
List<String> emails = getAllEmails(doc);
// make the username up
String username;
if (primaryEmail.length() > 0) {
username = primaryEmail.split("@")[0];
} else {
username = firstName.split(" ")[0] + "." + familyName;
}
username = username.replaceAll("[^a-zA-Z0-9.]", "");
// returning the parsed user. The user-id-in-provider will be added by the caller, since ORCiD passes it
// on the access token response.
// Affilifation added after a later call.
final ParsedUserResponse userResponse = new ParsedUserResponse(new AuthenticatedUserDisplayInfo(firstName, familyName, primaryEmail, "", ""), null, username);
userResponse.emails.addAll(emails);
return userResponse;
} catch (SAXException ex) {
logger.log(Level.SEVERE, "XML error parsing response body from ORCiD: " + ex.getMessage(), ex);
} catch (IOException ex) {
logger.log(Level.SEVERE, "I/O error parsing response body from ORCiD: " + ex.getMessage(), ex);
} catch (ParserConfigurationException ex) {
logger.log(Level.SEVERE, "While parsing the ORCiD response: Bad parse configuration. " + ex.getMessage(), ex);
}
return null;
}
use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.
the class GitHubOAuth2AP method parseUserResponse.
@Override
protected ParsedUserResponse parseUserResponse(String responseBody) {
try (StringReader rdr = new StringReader(responseBody);
JsonReader jrdr = Json.createReader(rdr)) {
JsonObject response = jrdr.readObject();
// Github has no concept of a family name
ShibUserNameFields shibUserNameFields = ShibUtil.findBestFirstAndLastName(null, null, response.getString("name", ""));
AuthenticatedUserDisplayInfo displayInfo = new AuthenticatedUserDisplayInfo(shibUserNameFields.getFirstName(), shibUserNameFields.getLastName(), response.getString("email", ""), response.getString("company", ""), "");
Integer persistentUserId = response.getInt("id");
String username = response.getString("login");
return new ParsedUserResponse(displayInfo, persistentUserId.toString(), username, displayInfo.getEmailAddress().length() > 0 ? Collections.singletonList(displayInfo.getEmailAddress()) : Collections.emptyList());
}
}
Aggregations