Search in sources :

Example 6 with AuthenticatedUserDisplayInfo

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());
}
Also used : AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) Test(org.junit.Test)

Example 7 with AuthenticatedUserDisplayInfo

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);
    }
}
Also used : AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject)

Example 8 with AuthenticatedUserDisplayInfo

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;
}
Also used : AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Node(org.w3c.dom.Node) StringReader(java.io.StringReader) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 9 with AuthenticatedUserDisplayInfo

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;
}
Also used : AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 10 with AuthenticatedUserDisplayInfo

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());
    }
}
Also used : ShibUserNameFields(edu.harvard.iq.dataverse.authorization.providers.shib.ShibUserNameFields) AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject)

Aggregations

AuthenticatedUserDisplayInfo (edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo)20 Test (org.junit.Test)8 AuthenticatedUser (edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser)5 StringReader (java.io.StringReader)4 BuiltinUser (edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinUser)3 IOException (java.io.IOException)3 Path (javax.ws.rs.Path)3 UserIdentifier (edu.harvard.iq.dataverse.authorization.UserIdentifier)2 AbstractOAuth2AuthenticationProvider (edu.harvard.iq.dataverse.authorization.providers.oauth2.AbstractOAuth2AuthenticationProvider)2 ShibUserNameFields (edu.harvard.iq.dataverse.authorization.providers.shib.ShibUserNameFields)2 JsonArrayBuilder (javax.json.JsonArrayBuilder)2 JsonObject (javax.json.JsonObject)2 JsonObjectBuilder (javax.json.JsonObjectBuilder)2 JsonReader (javax.json.JsonReader)2 PUT (javax.ws.rs.PUT)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 Document (org.w3c.dom.Document)2 Node (org.w3c.dom.Node)2