Search in sources :

Example 16 with AuthenticatedUserDisplayInfo

use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.

the class OrcidOAuth2AP method getUserRecord.

@Override
public OAuth2UserRecord getUserRecord(String code, String state, String redirectUrl) throws IOException, OAuth2Exception {
    OAuth20Service service = getService(state, redirectUrl);
    OAuth2AccessToken accessToken = service.getAccessToken(code);
    if (!accessToken.getScope().contains(scope)) {
        // We did not get the permissions on the scope we need. Abort and inform the user.
        throw new OAuth2Exception(200, BundleUtil.getStringFromBundle("auth.providers.orcid.insufficientScope"), "");
    }
    String orcidNumber = extractOrcidNumber(accessToken.getRawResponse());
    final String userEndpoint = getUserEndpoint(accessToken);
    final OAuthRequest request = new OAuthRequest(Verb.GET, userEndpoint, service);
    request.addHeader("Authorization", "Bearer " + accessToken.getAccessToken());
    request.setCharset("UTF-8");
    final Response response = request.send();
    int responseCode = response.getCode();
    final String body = response.getBody();
    logger.log(Level.FINE, "In getUserRecord. Body: {0}", body);
    if (responseCode == 200) {
        final ParsedUserResponse parsed = parseUserResponse(body);
        AuthenticatedUserDisplayInfo orgData = getOrganizationalData(userEndpoint, accessToken.getAccessToken(), service);
        parsed.displayInfo.setAffiliation(orgData.getAffiliation());
        parsed.displayInfo.setPosition(orgData.getPosition());
        return new OAuth2UserRecord(getId(), orcidNumber, parsed.username, OAuth2TokenData.from(accessToken), parsed.displayInfo, parsed.emails);
    } else {
        throw new OAuth2Exception(responseCode, body, "Error getting the user info record.");
    }
}
Also used : OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) OAuth2UserRecord(edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2UserRecord) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) OAuth2Exception(edu.harvard.iq.dataverse.authorization.providers.oauth2.OAuth2Exception) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 17 with AuthenticatedUserDisplayInfo

use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.

the class OAuth2FirstLoginPage method getWelcomeMessage.

public String getWelcomeMessage() {
    AuthenticatedUserDisplayInfo displayInfo = newUser.getDisplayInfo();
    String displayName = AuthUtil.getDisplayName(displayInfo.getFirstName(), displayInfo.getLastName());
    if (displayName != null) {
        return BundleUtil.getStringFromBundle("oauth2.newAccount.welcomeWithName", Arrays.asList(displayName));
    } else {
        return BundleUtil.getStringFromBundle("oauth2.newAccount.welcomeNoName");
    }
}
Also used : AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo)

Example 18 with AuthenticatedUserDisplayInfo

use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.

the class DataverseUserPage method init.

public String init() {
    // prevent creating a user if signup not allowed.
    boolean safeDefaultIfKeyNotFound = true;
    boolean signupAllowed = settingsWrapper.isTrueForKey(SettingsServiceBean.Key.AllowSignUp.toString(), safeDefaultIfKeyNotFound);
    if (editMode == EditMode.CREATE && !signupAllowed) {
        return "/403.xhtml";
    }
    if (editMode == EditMode.CREATE) {
        if (session.getUser().isAuthenticated()) {
            // we can't be in create mode for an existing user
            editMode = null;
        } else {
            // in create mode for new user
            JH.addMessage(FacesMessage.SEVERITY_INFO, BundleUtil.getStringFromBundle("user.signup.tip"));
            userDisplayInfo = new AuthenticatedUserDisplayInfo();
            return "";
        }
    }
    if (session.getUser().isAuthenticated()) {
        setCurrentUser((AuthenticatedUser) session.getUser());
        userAuthProvider = authenticationService.lookupProvider(currentUser);
        notificationsList = userNotificationService.findByUser(currentUser.getId());
        switch(selectTab) {
            case "notifications":
                activeIndex = 1;
                displayNotification();
                break;
            case "dataRelatedToMe":
                mydatapage.init();
                break;
            // break;
            case "accountInfo":
                activeIndex = 2;
                // activeIndex = 3;
                break;
            case "apiTokenTab":
                activeIndex = 3;
                break;
            default:
                activeIndex = 0;
                break;
        }
    } else {
        return permissionsWrapper.notAuthorized();
    }
    return "";
}
Also used : AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo)

Example 19 with AuthenticatedUserDisplayInfo

use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.

the class GitHubOAuth2APTest method testParseUserResponse.

@Test
public void testParseUserResponse() {
    AbstractOAuth2AuthenticationProvider.ParsedUserResponse expResult = new AbstractOAuth2AuthenticationProvider.ParsedUserResponse(new AuthenticatedUserDisplayInfo("Philip", "Durbin", "philipdurbin@gmail.com", "Harvard", ""), "1938468", "jane_doe");
    AbstractOAuth2AuthenticationProvider.ParsedUserResponse result = parseUserResponse(GITHUB_RESPONSE);
    assertEquals(expResult.displayInfo, result.displayInfo);
    assertEquals("21006", result.userIdInProvider);
}
Also used : AbstractOAuth2AuthenticationProvider(edu.harvard.iq.dataverse.authorization.providers.oauth2.AbstractOAuth2AuthenticationProvider) AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) Test(org.junit.Test)

Example 20 with AuthenticatedUserDisplayInfo

use of edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo in project dataverse by IQSS.

the class OrcidOAuth2APTest method testParseActivitiesResponseNoRole.

@Test
public void testParseActivitiesResponseNoRole() {
    OrcidOAuth2AP sut = new OrcidOAuth2AP("clientId", "clientSecret", "userEndpoint");
    assertNotNull(ACTIVITIES);
    String responseWithNoOrg = ACTIVITIES.replaceAll("\n", "").replaceAll("<employment:role-title>.*</employment:role-title>", "");
    final AuthenticatedUserDisplayInfo actual = sut.parseActivitiesResponse(responseWithNoOrg);
    assertEquals("My Organization Name", actual.getAffiliation());
    assertEquals("department", actual.getPosition());
}
Also used : AuthenticatedUserDisplayInfo(edu.harvard.iq.dataverse.authorization.AuthenticatedUserDisplayInfo) Test(org.junit.Test)

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