160 lines
6.7 KiB
Java
160 lines
6.7 KiB
Java
package eu.dnetlib.uoaorcidservice.controllers;
|
|
|
|
import eu.dnetlib.uoaorcidservice.entities.UserTokens;
|
|
import eu.dnetlib.uoaorcidservice.handlers.utils.AESUtils;
|
|
import eu.dnetlib.uoaorcidservice.services.UserTokensService;
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.*;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.client.DefaultResponseErrorHandler;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import javax.crypto.BadPaddingException;
|
|
import javax.crypto.IllegalBlockSizeException;
|
|
import javax.crypto.NoSuchPaddingException;
|
|
import java.io.IOException;
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.spec.InvalidKeySpecException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/orcid")
|
|
@CrossOrigin(origins = "*")
|
|
public class UserTokensController {
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
@Autowired
|
|
private UserTokensService userTokensService;
|
|
|
|
// @RequestMapping(value = "/tokens", method = RequestMethod.GET)
|
|
// public List<UserTokens> getAllUserTokens() {
|
|
// return userTokensService.getAllUserTokens();
|
|
// }
|
|
|
|
// @RequestMapping(value = "/token/access_token", method = RequestMethod.GET)
|
|
// public String getUserAccessTokenByOrcid(@RequestParam String orcid) {
|
|
// return "\""+userTokensService.getUserAccessToken(orcid)+"\"";
|
|
// }
|
|
|
|
@PreAuthorize("isAuthenticated()")
|
|
@RequestMapping(value = "/token/save", method = RequestMethod.GET)
|
|
public String saveUserTokens(@RequestParam String code) throws BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IOException {
|
|
log.debug("saveUserTokens: code="+code);
|
|
|
|
String url = "https://sandbox.orcid.org/oauth/token";
|
|
String clientId = "APP-A5M3KTX6NCN67L91";
|
|
String clientSecret = "96b20d71-ae06-4286-bb00-9172536c1ad4";
|
|
|
|
|
|
RestTemplate restTemplate = new RestTemplate();
|
|
restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){
|
|
protected boolean hasError(HttpStatus statusCode) {
|
|
return false;
|
|
}});
|
|
HttpHeaders headers = new HttpHeaders();
|
|
headers.add("Content-Type","application/x-www-form-urlencoded");
|
|
headers.add("Accept","application/json");
|
|
|
|
String inputString =
|
|
"client_id="+clientId
|
|
+"&client_secret="+clientSecret
|
|
+"&grant_type=authorization_code"
|
|
+"&code="+code
|
|
+"&redirect_uri=http://duffy.di.uoa.gr:4300/orcid";
|
|
log.debug(inputString);
|
|
|
|
HttpEntity<String> request = new HttpEntity<>(inputString, headers);
|
|
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class));
|
|
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
|
|
if(response.getStatusCode() != HttpStatus.OK) {
|
|
log.debug("User tokens response code is: " + response.getStatusCode());
|
|
log.debug(response.getBody());
|
|
return null;
|
|
} else {
|
|
log.debug(response);
|
|
|
|
UserTokens userTokens = userTokensService.json2UserTokens(response.getBody().toString());
|
|
userTokensService.saveUserTokens(userTokens);
|
|
|
|
return "\""+userTokens.getAccessToken()+"\"";
|
|
}
|
|
|
|
// try {
|
|
// URL obj = new URL(url);
|
|
//
|
|
// HttpURLConnection con = (HttpURLConnection) obj.openConnection();
|
|
// con.setRequestMethod("POST");
|
|
// con.setDoOutput(true);
|
|
// con.setInstanceFollowRedirects(true);
|
|
// con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
// con.setRequestProperty("Accept", "application/json");
|
|
//
|
|
// String inputString =
|
|
// "client_id="+clientId
|
|
// +"&client_secret="+clientSecret
|
|
// +"&grant_type=authorization_code"
|
|
// +"&code="+code
|
|
// +"&redirect_uri=http://duffy.di.uoa.gr:4300/orcid";
|
|
//
|
|
// log.debug(inputString);
|
|
//
|
|
// try(OutputStream os = con.getOutputStream()) {
|
|
//// byte[] input = inputString.getBytes();
|
|
//// os.write(inputString, 0, inputString.length());
|
|
//
|
|
// OutputStreamWriter osw = new OutputStreamWriter(os);
|
|
// osw.write(inputString);
|
|
// osw.flush();
|
|
// osw.close();
|
|
//
|
|
// os.close(); //don't forget to close the OutputStream
|
|
// log.debug("http request body added");
|
|
// } catch (Exception e) {
|
|
// log.error("Failed to add http request body", e);
|
|
// }
|
|
//
|
|
// con.connect();
|
|
//
|
|
//// log.debug(con.getRequestMethod());
|
|
//// log.debug(con.getErrorStream());
|
|
//// log.debug(con.getContent());
|
|
//
|
|
//
|
|
// if (con.getResponseCode() != 200) {
|
|
// log.debug("User tokens response code is: " + con.getResponseCode());
|
|
// return null;
|
|
// } else {
|
|
// BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
|
// StringBuilder response = new StringBuilder();
|
|
// String inputLine;
|
|
// while ((inputLine = in.readLine()) != null) {
|
|
// response.append(inputLine).append("\n");
|
|
// }
|
|
// in.close();
|
|
// log.debug(response);
|
|
//
|
|
// UserTokens userTokens = userTokensService.json2UserTokens(response.toString());
|
|
// userTokensService.saveUserTokens(userTokens);
|
|
//
|
|
// return "\""+userTokens.getAccessToken()+"\"";
|
|
// }
|
|
//
|
|
// } catch (Exception e) {
|
|
// log.error("An error occured while trying to fetch user tokens ", e);
|
|
// return null;
|
|
// }
|
|
}
|
|
|
|
@PreAuthorize("isAuthenticated()")
|
|
@RequestMapping(value = "/tokens/decrypt", method = RequestMethod.GET)
|
|
public UserTokens decryptToken(@RequestParam String aaiId) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException {
|
|
return userTokensService.getUserTokensByAai(aaiId);
|
|
}
|
|
|
|
}
|