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.");
}
}
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");
}
}
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 "";
}
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);
}
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());
}
Aggregations