@RequestMapping(value = "/oauth/finish", method = RequestMethod.GET) @Timed public void oauthUserFinish( HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "code", required = false) String code, @RequestParam(value = "state", required = false) String state, @RequestParam(value = "error", required = false) String error, @RequestParam(value = "error_description", required = false) String errorDescription) throws IOException, JAXBException, RelPersonApplicationNotFoundException, InterruptedException { log.debug( "REST OAUTHFINISH START. code [{}],state/relId [{}],error [{}], error_description [{}]", code, state, error, errorDescription); RelPersonApplication relPersonApplication = relPersonApplicationRepository.findOne(new Long(state)); if ((relPersonApplication == null) || (relPersonApplication.getValid() != null)) { String relPersonApplicationId = "-1"; if (relPersonApplication != null) relPersonApplicationId = relPersonApplication.getId().toString(); throw new RelPersonApplicationNotFoundException(relPersonApplicationId); } Application application = relPersonApplication.getApplication(); Person person = relPersonApplication.getPerson(); Token token = relPersonApplication.getToken(); // invalido token if (token.getDateUsed() == null) { token.setDateUsed(DateTime.now()); tokenRepository.save(token); } if (error != null) { log.info( "REST OAUTHFINISH {}. localId [{}],appId [{}],error [{}], error_description [{}]", error, person.getLocalID(), application.getApplicationID(), error, errorDescription); if (error.equals("access_denied")) { relPersonApplication.setDenied(true); relPersonApplication.setDateDenied(DateTime.now()); relPersonApplication.setValid(true); } else { relPersonApplication.setValid(false); } relPersonApplication.setErrorDescription(error); relPersonApplication.setDateReleased(DateTime.now()); relPersonApplicationRepository.save(relPersonApplication); } else { String callBackUrl = getCallbackOrcidURL(request); OrcidOAuthClient clientOrcid = new OrcidOAuthClient( application.getApplicationID(), application.getApplicationSecret(), callBackUrl, orcidApiType); log.info( "REST OAUTHFINISH REQUEST ACCESSTOKEN. localId [{}],appId [{}], appSecret [{}]. orcidApiType [{}]", person.getLocalID(), application.getApplicationID(), application.getApplicationSecret(), orcidApiType); OrcidAccessToken tokenOrcid = clientOrcid.getAccessToken(code); log.info( "REST OAUTHFINISH ACCESSTOKEN RELEASED. localId [{}],appId [{}],accessToken [{}], orcid [{}]", person.getLocalID(), application.getApplicationID(), tokenOrcid.getAccess_token(), tokenOrcid.getOrcid()); relPersonApplication.setDateReleased(DateTime.now()); relPersonApplication.setOauthAccessToken(tokenOrcid.getAccess_token()); relPersonApplication.setValid(true); relPersonApplication.setDenied(false); if ((person.getOrcid() == null) || ((tokenOrcid.getOrcid() != null) && (!person.getOrcid().equals(tokenOrcid.getOrcid())))) { person.setOrcid(tokenOrcid.getOrcid()); person.setOrcidReleaseDate(DateTime.now()); } relPersonApplicationRepository.save(relPersonApplication); personRepository.save(person); // async orcidService.sendNotify(relPersonApplication); } List<RelPersonApplication> listApplicationAuth = relPersonApplicationRepository.findAllByTokenIsAndValidIsNull(token); if (listApplicationAuth.size() > 0) { Application applicationAuthorize = listApplicationAuth.get(0).getApplication(); String callBackUrl = getCallbackOrcidURL(request); OrcidOAuthClient clientOrcid = new OrcidOAuthClient( applicationAuthorize.getApplicationID(), applicationAuthorize.getApplicationSecret(), callBackUrl, orcidApiType); List<OrcidAuthScope> orcidScopes = clientOrcid.getListAllScope(); String urlToRedirect = clientOrcid.getAuthzCodeRegisterRequest( listApplicationAuth.get(0).getId().toString(), orcidScopes, person.getFirstName(), person.getLastName(), person.getEmail()); log.debug( "REST OAUTHFINISH REDIRECT TO APP. listApp [{}], appId [{}], token [{}], urlRedirect [{}]", listApplicationAuth.size(), applicationAuthorize.getApplicationID(), listApplicationAuth.get(0).getToken().getOtt(), urlToRedirect); response.sendRedirect(urlToRedirect); } else { String urlToRedirect = token.getUrlCallback(); if ((urlToRedirect == null) || (urlToRedirect.isEmpty())) { Application appRequest = token.getApplication(); urlToRedirect = appRequest.getUrlCallback(); } if (urlToRedirect.contains("?")) urlToRedirect = urlToRedirect + "&"; else urlToRedirect = urlToRedirect + "?"; urlToRedirect = urlToRedirect + "ott=" + token.getOtt(); RelPersonApplication resultRel = relPersonApplicationRepository.findOneByPersonIsAndApplicationIsAndTokenIs( token.getPerson(), token.getApplication(), token); String resultCode = null; if ((resultRel != null) && (resultRel.getValid()) && ((resultRel.getDenied() == null) || (resultRel.getDenied() == false))) { resultCode = "001"; } else if (resultRel.getDenied()) { resultCode = ResultCode.ERROR_USER_DENIED.getCode(); } else { resultCode = ResultCode.ERROR_ORCID_FOR_USER_MISSING.getCode(); } urlToRedirect = urlToRedirect + "&result-code=" + resultCode; log.debug( "REST OAUTHFINISH FINISH. token [{}], urlToRedirect: [{}], resultCode [{}]", token.getOtt(), urlToRedirect, resultCode); response.sendRedirect(urlToRedirect); } return; }
@RequestMapping(value = "/oauth/apps/{TOKEN}", method = RequestMethod.GET) @Timed public GetLandingPageResponseDTO oauthAppForUser( @PathVariable("TOKEN") String ott, HttpServletRequest request) throws IOException, JAXBException, TokenNotFoundException, TokenAlreadyUsedException { GetLandingPageResponseDTO response = new GetLandingPageResponseDTO(); log.debug("REST GET APPS. token [{}]", ott); Token token = tokenRepository.findOneByOtt(ott); if (token == null) { throw new TokenNotFoundException(ott); } Person person = token.getPerson(); Application application = token.getApplication(); log.debug("REST GET APPS. person [{}], localid [{}]", person, person.getLocalID()); // qual è l'organizzazione dell'utente? String orgUnit = null; if (application.getAllOrg()) orgUnit = token.getOrgUnit(); else orgUnit = application.getOrgUnit(); List<RelPersonApplication> listApplicationAuth = relPersonApplicationRepository.findAllByTokenIsAndValidIsNull(token); String urlRegisterOrcid = null; String urlLoginOrcid = null; if (listApplicationAuth.size() > 0) { Application applicationAuthorize = listApplicationAuth.get(0).getApplication(); String callBackUrl = getCallbackOrcidURL(request); OrcidOAuthClient clientOrcid = new OrcidOAuthClient( applicationAuthorize.getApplicationID(), applicationAuthorize.getApplicationSecret(), callBackUrl, orcidApiType); List<OrcidAuthScope> orcidScopes = clientOrcid.getListAllScope(); urlRegisterOrcid = clientOrcid.getAuthzCodeRegisterRequest( listApplicationAuth.get(0).getId().toString(), orcidScopes, person.getFirstName(), person.getLastName(), person.getEmail()); urlLoginOrcid = clientOrcid.getAuthzCodeLoginRequest( listApplicationAuth.get(0).getId().toString(), orcidScopes, person.getFirstName(), person.getLastName(), person.getEmail()); ; } String urlHelp = null; if ((application.getHelpURL() != null) && (!application.getHelpURL().isEmpty())) { urlHelp = application.getHelpURL(); } else if ((application.getHelpMail() != null) && (!application.getHelpMail().isEmpty())) { urlHelp = "mailto:" + application.getHelpMail() + "?Subject=ORCID Support " + person.getLocalID(); } // List<Application> applicationForUser = // applicationRepository.findAllByOrgUnitOrAllOrgIsTrue(orgUnit); response.setFirstname(person.getFirstName()); response.setLastname(person.getLastName()); response.setUrlRegisterOrcid(urlRegisterOrcid); response.setUrlLoginOrcid(urlLoginOrcid); response.setUrlHelp(urlHelp); response.setListApp(ApplicationMapper.fromListRelApp(listApplicationAuth)); return response; }