예제 #1
0
  @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;
  }
예제 #2
0
  @RequestMapping(value = "/oauth/{TOKEN}", method = RequestMethod.GET)
  @Timed
  public void oauthUser(
      @PathVariable("TOKEN") String ott, HttpServletRequest request, HttpServletResponse response)
      throws IOException, JAXBException, TokenNotFoundException, TokenAlreadyUsedException {
    log.debug("REST OAUTH START. token [{}]", ott);

    Token token = tokenRepository.findOneByOtt(ott);
    if (token == null) {
      throw new TokenNotFoundException(ott);
    }
    // if( token.getDateUsed() !=null){
    //	throw new TokenAlreadyUsedException(ott);
    // }

    Person person = token.getPerson();
    Application application = token.getApplication();

    // qual è l'organizzazione dell'utente?
    String orgUnit = null;
    if (application.getAllOrg()) orgUnit = token.getOrgUnit();
    else orgUnit = application.getOrgUnit();

    List<Application> applicationForUser =
        applicationRepository.findAllByOrgUnitOrAllOrgIsTrue(orgUnit);
    Map<Long, Application> applicationForUserMap = new HashMap<Long, Application>();
    for (Application i : applicationForUser) applicationForUserMap.put(i.getId(), i);

    List<RelPersonApplication> listApplicationAuth =
        relPersonApplicationRepository.findAllByPersonIsAndLastIsTrue(person);
    Map<Long, RelPersonApplication> mapRelOld = new HashMap<Long, RelPersonApplication>();
    // Set old application access key invalid
    for (int i = 0; i < listApplicationAuth.size(); i++) {
      RelPersonApplication applicationAuthorize = listApplicationAuth.get(i);
      applicationAuthorize.setValid(false);
      applicationAuthorize.setLast(false);
      relPersonApplicationRepository.save(applicationAuthorize);
      mapRelOld.put(applicationAuthorize.getApplication().getId(), applicationAuthorize);
      if ((applicationAuthorize.getCustom() == true)
          && (applicationForUserMap.get(applicationAuthorize.getApplication().getId()) == null)) {
        applicationForUser.add(applicationAuthorize.getApplication());
      }
    }

    // token.setDateUsed(DateTime.now());
    // tokenRepository.save(token);

    Application applicationAuthorize = null;
    RelPersonApplication relPersonApplication = null;

    // Create new access key record
    for (int i = 0; i < applicationForUser.size(); i++) {
      applicationAuthorize = applicationForUser.get(i);
      relPersonApplication = new RelPersonApplication();
      relPersonApplication.setApplication(applicationAuthorize);
      relPersonApplication.setPerson(person);
      relPersonApplication.setToken(token);
      if (mapRelOld.get(applicationAuthorize.getId()) != null) {
        RelPersonApplication relOld = mapRelOld.get(applicationAuthorize.getId());
        relPersonApplication.setDateReleased(relOld.getDateReleased());
        relPersonApplication.setOauthAccessToken(relOld.getOauthAccessToken());
        relPersonApplication.setCustom(relOld.getCustom());
      } else {
        relPersonApplication.setCustom(false);
      }
      relPersonApplication.setValid(null);
      relPersonApplication.setLast(true);

      relPersonApplicationRepository.save(relPersonApplication);
    }

    String urlToRedirect = getLandingPageURL(request, ott);

    log.info(
        "REST OAUTH REDIRECT TO APP. listApp [{}], appId [{}], token [{}], urlRedirect [{}]",
        applicationForUser.size(),
        applicationAuthorize.getApplicationID(),
        ott,
        urlToRedirect);
    log.debug("REST OAUTH FINISH. token [{}], urlToRedirect [{}]", ott, urlToRedirect);

    response.sendRedirect(urlToRedirect);
    return;
  }