use of org.springframework.security.oauth2.provider.AuthorizationRequest in project ORCID-Source by ORCID.
the class OauthAuthorizeController method authorize.
@RequestMapping(value = { "/oauth/custom/authorize.json" }, method = RequestMethod.POST)
@ResponseBody
public RequestInfoForm authorize(HttpServletRequest request, HttpServletResponse response, @RequestBody OauthAuthorizeForm form) {
RequestInfoForm requestInfoForm = (RequestInfoForm) request.getSession().getAttribute(REQUEST_INFO_FORM);
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
AuthorizationRequest authorizationRequest = (AuthorizationRequest) request.getSession().getAttribute("authorizationRequest");
Map<String, String> requestParams = new HashMap<String, String>(authorizationRequest.getRequestParameters());
Map<String, String> approvalParams = new HashMap<String, String>();
// Add the persistent token information
if (form.getApproved()) {
requestParams.put(OAuth2Utils.USER_OAUTH_APPROVAL, "true");
approvalParams.put(OAuth2Utils.USER_OAUTH_APPROVAL, "true");
} else {
requestParams.put(OAuth2Utils.USER_OAUTH_APPROVAL, "false");
approvalParams.put(OAuth2Utils.USER_OAUTH_APPROVAL, "false");
}
requestParams.put(OrcidOauth2Constants.TOKEN_VERSION, OrcidOauth2Constants.PERSISTENT_TOKEN);
// Check if the client have persistent tokens enabled
requestParams.put(OrcidOauth2Constants.GRANT_PERSISTENT_TOKEN, "false");
if (hasPersistenTokensEnabled(requestInfoForm.getClientId()))
// Then check if the client granted the persistent token
if (form.getPersistentTokenEnabled())
requestParams.put(OrcidOauth2Constants.GRANT_PERSISTENT_TOKEN, "true");
// strip /email/read-private scope if user has not consented
if (requestInfoForm.containsEmailReadPrivateScope() && !form.isEmailAccessAllowed()) {
requestInfoForm.removeEmailReadPrivateScope();
requestParams.put(OrcidOauth2Constants.SCOPE_PARAM, requestInfoForm.getScopesAsString());
}
// Session status
SimpleSessionStatus status = new SimpleSessionStatus();
authorizationRequest.setRequestParameters(requestParams);
// Authorization request model
Map<String, Object> model = new HashMap<String, Object>();
model.put("authorizationRequest", authorizationRequest);
// Approve
RedirectView view = (RedirectView) authorizationEndpoint.approveOrDeny(approvalParams, model, status, auth);
requestInfoForm.setRedirectUrl(view.getUrl());
if (new HttpSessionRequestCache().getRequest(request, response) != null)
new HttpSessionRequestCache().removeRequest(request, response);
LOGGER.info("OauthConfirmAccessController form.getRedirectUri being sent to client browser: " + requestInfoForm.getRedirectUrl());
return requestInfoForm;
}
use of org.springframework.security.oauth2.provider.AuthorizationRequest in project ORCID-Source by ORCID.
the class T2OrcidApiServiceDelegatorImpl method setSponsorFromAuthentication.
public void setSponsorFromAuthentication(OrcidProfile profile) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (profile.getOrcidHistory() == null) {
OrcidHistory orcidHistory = new OrcidHistory();
orcidHistory.setCreationMethod(CreationMethod.API);
profile.setOrcidHistory(orcidHistory);
}
profile.getOrcidHistory().setSubmissionDate(new SubmissionDate(DateUtils.convertToXMLGregorianCalendar(new Date())));
if (OAuth2Authentication.class.isAssignableFrom(authentication.getClass())) {
OAuth2Request authorizationRequest = ((OAuth2Authentication) authentication).getOAuth2Request();
Source sponsor = new Source();
String sponsorId = authorizationRequest.getClientId();
ClientDetailsEntity clientDetails = clientDetailsManager.findByClientId(sponsorId);
if (clientDetails != null) {
sponsor.setSourceName(new SourceName(clientDetails.getClientName()));
if (OrcidStringUtils.isClientId(sponsorId)) {
sponsor.setSourceClientId(new SourceClientId(sponsorId));
} else {
sponsor.setSourceOrcid(new SourceOrcid(sponsorId));
}
}
profile.getOrcidHistory().setSource(sponsor);
}
}
use of org.springframework.security.oauth2.provider.AuthorizationRequest in project ORCID-Source by ORCID.
the class OrcidAuthorizationCodeServiceTest method testCreateAuthorizationCodeWithValidClient.
@Test
@Rollback
@Transactional
public void testCreateAuthorizationCodeWithValidClient() {
AuthorizationRequest request = getAuthorizationRequest("4444-4444-4444-4441");
OAuth2Authentication oauth2Authentication = new OAuth2Authentication(oAuth2RequestFactory.createOAuth2Request(request), getUserAuthentication());
String authorizationCode = authorizationCodeServices.createAuthorizationCode(oauth2Authentication);
assertNotNull(authorizationCode);
oauth2Authentication = authorizationCodeServices.consumeAuthorizationCode(authorizationCode);
assertNotNull(oauth2Authentication);
}
use of org.springframework.security.oauth2.provider.AuthorizationRequest in project ORCID-Source by ORCID.
the class OrcidAuthorizationCodeServiceTest method testCreateAuthorizationCodeWithInvalidClient.
@Test(expected = InvalidClientException.class)
@Rollback
@Transactional
public void testCreateAuthorizationCodeWithInvalidClient() {
AuthorizationRequest request = getAuthorizationRequest("6444-4444-4444-4441");
OAuth2Authentication auth = new OAuth2Authentication(oAuth2RequestFactory.createOAuth2Request(request), getUserAuthentication());
authorizationCodeServices.createAuthorizationCode(auth);
}
use of org.springframework.security.oauth2.provider.AuthorizationRequest in project ORCID-Source by ORCID.
the class OrcidAuthorizationCodeServiceTest method getAuthorizationRequest.
public AuthorizationRequest getAuthorizationRequest(String clientId) {
Set<GrantedAuthority> grantedAuthorities = new HashSet<GrantedAuthority>(Arrays.asList(new SimpleGrantedAuthority("ROLE_USER")));
Set<String> resourceIds = new HashSet<>();
resourceIds.add("orcid");
Map<String, String> params = new HashMap<String, String>();
params.put(OAuth2Utils.CLIENT_ID, clientId);
params.put(OAuth2Utils.SCOPE, "a-scope");
AuthorizationRequest authorizationRequest = oAuth2RequestFactory.createAuthorizationRequest(params);
authorizationRequest.setAuthorities(grantedAuthorities);
authorizationRequest.setResourceIds(resourceIds);
return authorizationRequest;
}
Aggregations