Revision 60369
Added by Konstantina Galouni about 3 years ago
UserTokensController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.uoaorcidservice.controllers; |
2 | 2 |
|
3 |
import eu.dnetlib.uoaorcidservice.configuration.properties.OrcidConfig; |
|
3 | 4 |
import eu.dnetlib.uoaorcidservice.entities.UserTokens; |
4 | 5 |
import eu.dnetlib.uoaorcidservice.handlers.utils.AESUtils; |
6 |
import eu.dnetlib.uoaorcidservice.responses.SingleValueWrapperResponse; |
|
5 | 7 |
import eu.dnetlib.uoaorcidservice.services.UserTokensService; |
6 | 8 |
import org.apache.log4j.Logger; |
7 | 9 |
import org.springframework.beans.factory.annotation.Autowired; |
8 | 10 |
import org.springframework.http.*; |
11 |
import org.springframework.security.access.AuthorizationServiceException; |
|
9 | 12 |
import org.springframework.security.access.prepost.PreAuthorize; |
10 | 13 |
import org.springframework.web.bind.annotation.*; |
11 | 14 |
import org.springframework.web.client.DefaultResponseErrorHandler; |
... | ... | |
23 | 26 |
import java.util.List; |
24 | 27 |
|
25 | 28 |
@RestController |
26 |
@RequestMapping("/orcid") |
|
29 |
//@RequestMapping("/orcid")
|
|
27 | 30 |
@CrossOrigin(origins = "*") |
28 | 31 |
public class UserTokensController { |
29 | 32 |
private final Logger log = Logger.getLogger(this.getClass()); |
33 |
private final Logger orcid_log = Logger.getLogger("ORCID-"+this.getClass().getName()); |
|
30 | 34 |
|
31 | 35 |
@Autowired |
36 |
private OrcidConfig orcidConfig; |
|
37 |
|
|
38 |
@Autowired |
|
32 | 39 |
private UserTokensService userTokensService; |
33 | 40 |
|
34 | 41 |
// @RequestMapping(value = "/tokens", method = RequestMethod.GET) |
... | ... | |
41 | 48 |
// return "\""+userTokensService.getUserAccessToken(orcid)+"\""; |
42 | 49 |
// } |
43 | 50 |
|
51 |
@RequestMapping(value = "/local/orcidId", method = RequestMethod.GET) |
|
52 |
public SingleValueWrapperResponse<String> getUserOrcidId() throws BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, IOException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException { |
|
53 |
UserTokens userTokens = userTokensService.getUserTokens(); |
|
54 |
if(userTokens == null) { |
|
55 |
throw new AuthorizationServiceException("User is not registered"); |
|
56 |
} |
|
57 |
String userOrcid = userTokens.getOrcid(); |
|
58 |
return new SingleValueWrapperResponse<String>(userOrcid); |
|
59 |
} |
|
60 |
|
|
44 | 61 |
@PreAuthorize("isAuthenticated()") |
45 |
@RequestMapping(value = "/token/save", method = RequestMethod.GET) |
|
46 |
public String saveUserTokens(@RequestParam String code) throws BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IOException { |
|
62 |
@RequestMapping(value = "/orcid/token/save", method = RequestMethod.GET) |
|
63 |
public String saveUserTokens(@RequestParam String code |
|
64 |
// , @RequestParam String redirect_uri |
|
65 |
) throws BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IOException { |
|
47 | 66 |
log.debug("saveUserTokens: code="+code); |
48 | 67 |
|
49 |
String url = "https://sandbox.orcid.org/oauth/token"; |
|
50 |
String clientId = "APP-A5M3KTX6NCN67L91"; |
|
51 |
String clientSecret = "96b20d71-ae06-4286-bb00-9172536c1ad4"; |
|
68 |
String url = orcidConfig.getTokenURL();//"https://sandbox.orcid.org/oauth/token";
|
|
69 |
String clientId = orcidConfig.getClientId();//"APP-A5M3KTX6NCN67L91";
|
|
70 |
String clientSecret = orcidConfig.getClientSecret();//"96b20d71-ae06-4286-bb00-9172536c1ad4";
|
|
52 | 71 |
|
53 | 72 |
|
54 | 73 |
RestTemplate restTemplate = new RestTemplate(); |
... | ... | |
64 | 83 |
"client_id="+clientId |
65 | 84 |
+"&client_secret="+clientSecret |
66 | 85 |
+"&grant_type=authorization_code" |
67 |
+"&code="+code |
|
68 |
+"&redirect_uri=http://duffy.di.uoa.gr:4300/orcid"; |
|
69 |
log.debug(inputString); |
|
86 |
+"&code="+code; |
|
87 |
// +"&redirect_uri="+redirect_uri;//http://duffy.di.uoa.gr:4300/orcid"; |
|
70 | 88 |
|
71 | 89 |
HttpEntity<String> request = new HttpEntity<>(inputString, headers); |
72 |
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class)); |
|
90 |
orcid_log.info("url: "+url); |
|
91 |
orcid_log.info("request: "+request); |
|
92 |
|
|
73 | 93 |
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); |
74 | 94 |
if(response.getStatusCode() != HttpStatus.OK) { |
75 |
log.debug("User tokens response code is: " + response.getStatusCode());
|
|
76 |
log.debug(response.getBody());
|
|
95 |
orcid_log.error("User tokens response code is: " + response.getStatusCode());
|
|
96 |
orcid_log.error("Unexpected Response: "+response.getBody());
|
|
77 | 97 |
return null; |
78 | 98 |
} else { |
79 |
log.debug(response);
|
|
99 |
orcid_log.info("Response: "+response);
|
|
80 | 100 |
|
81 | 101 |
UserTokens userTokens = userTokensService.json2UserTokens(response.getBody().toString()); |
82 | 102 |
userTokensService.saveUserTokens(userTokens); |
83 | 103 |
|
84 | 104 |
return "\""+userTokens.getAccessToken()+"\""; |
85 | 105 |
} |
106 |
} |
|
86 | 107 |
|
87 |
// try { |
|
88 |
// URL obj = new URL(url); |
|
89 |
// |
|
90 |
// HttpURLConnection con = (HttpURLConnection) obj.openConnection(); |
|
91 |
// con.setRequestMethod("POST"); |
|
92 |
// con.setDoOutput(true); |
|
93 |
// con.setInstanceFollowRedirects(true); |
|
94 |
// con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
|
95 |
// con.setRequestProperty("Accept", "application/json"); |
|
96 |
// |
|
97 |
// String inputString = |
|
98 |
// "client_id="+clientId |
|
99 |
// +"&client_secret="+clientSecret |
|
100 |
// +"&grant_type=authorization_code" |
|
101 |
// +"&code="+code |
|
102 |
// +"&redirect_uri=http://duffy.di.uoa.gr:4300/orcid"; |
|
103 |
// |
|
104 |
// log.debug(inputString); |
|
105 |
// |
|
106 |
// try(OutputStream os = con.getOutputStream()) { |
|
107 |
//// byte[] input = inputString.getBytes(); |
|
108 |
//// os.write(inputString, 0, inputString.length()); |
|
109 |
// |
|
110 |
// OutputStreamWriter osw = new OutputStreamWriter(os); |
|
111 |
// osw.write(inputString); |
|
112 |
// osw.flush(); |
|
113 |
// osw.close(); |
|
114 |
// |
|
115 |
// os.close(); //don't forget to close the OutputStream |
|
116 |
// log.debug("http request body added"); |
|
117 |
// } catch (Exception e) { |
|
118 |
// log.error("Failed to add http request body", e); |
|
119 |
// } |
|
120 |
// |
|
121 |
// con.connect(); |
|
122 |
// |
|
123 |
//// log.debug(con.getRequestMethod()); |
|
124 |
//// log.debug(con.getErrorStream()); |
|
125 |
//// log.debug(con.getContent()); |
|
126 |
// |
|
127 |
// |
|
128 |
// if (con.getResponseCode() != 200) { |
|
129 |
// log.debug("User tokens response code is: " + con.getResponseCode()); |
|
130 |
// return null; |
|
131 |
// } else { |
|
132 |
// BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); |
|
133 |
// StringBuilder response = new StringBuilder(); |
|
134 |
// String inputLine; |
|
135 |
// while ((inputLine = in.readLine()) != null) { |
|
136 |
// response.append(inputLine).append("\n"); |
|
137 |
// } |
|
138 |
// in.close(); |
|
139 |
// log.debug(response); |
|
140 |
// |
|
141 |
// UserTokens userTokens = userTokensService.json2UserTokens(response.toString()); |
|
142 |
// userTokensService.saveUserTokens(userTokens); |
|
143 |
// |
|
144 |
// return "\""+userTokens.getAccessToken()+"\""; |
|
145 |
// } |
|
146 |
// |
|
147 |
// } catch (Exception e) { |
|
148 |
// log.error("An error occured while trying to fetch user tokens ", e); |
|
149 |
// return null; |
|
150 |
// } |
|
108 |
@RequestMapping(value = "/orcid/personal-details", method = RequestMethod.GET) |
|
109 |
public String getPersonalDetailsFromOrcid() throws BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, IOException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException { |
|
110 |
log.debug("getPersonalDetailsFromOrcid"); |
|
111 |
|
|
112 |
UserTokens userTokens = userTokensService.getUserTokens(); |
|
113 |
if(userTokens == null) { |
|
114 |
throw new AuthorizationServiceException("User is not registered"); |
|
115 |
} |
|
116 |
String userOrcid = userTokens.getOrcid(); |
|
117 |
String userAccessToken = userTokens.getAccessToken(); |
|
118 |
|
|
119 |
if(userOrcid == null || userAccessToken == null) { |
|
120 |
throw new AuthorizationServiceException("User is not registered"); |
|
121 |
} |
|
122 |
|
|
123 |
// log.debug("Access token: " + userAccessToken); |
|
124 |
// log.debug("User orcid: " + userOrcid); |
|
125 |
|
|
126 |
String url = orcidConfig.getApiURL()+userOrcid+"/personal-details"; |
|
127 |
|
|
128 |
RestTemplate restTemplate = new RestTemplate(); |
|
129 |
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() { |
|
130 |
protected boolean hasError(HttpStatus statusCode) { |
|
131 |
return false; |
|
132 |
} |
|
133 |
}); |
|
134 |
HttpHeaders headers = new HttpHeaders(); |
|
135 |
headers.add("Accept", "application/json"); |
|
136 |
headers.add("Authorization", "Bearer " + userAccessToken); |
|
137 |
headers.add("Content-Type", "application/orcid+json"); |
|
138 |
HttpEntity<String> request = new HttpEntity<>(headers); |
|
139 |
|
|
140 |
orcid_log.info("request: "+request); |
|
141 |
orcid_log.info("url: "+url); |
|
142 |
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, request, String.class); |
|
143 |
if (response.getStatusCode() != HttpStatus.OK) { |
|
144 |
orcid_log.error("Getting user details response code is: " + response.getStatusCode()); |
|
145 |
orcid_log.error("Unexpected Response: "+response.getBody()); |
|
146 |
return null; |
|
147 |
} else { |
|
148 |
orcid_log.info("response: "+response); |
|
149 |
return response.getBody().toString(); |
|
150 |
} |
|
151 | 151 |
} |
152 | 152 |
|
153 |
|
|
153 | 154 |
@PreAuthorize("isAuthenticated()") |
154 |
@RequestMapping(value = "/tokens/decrypt", method = RequestMethod.GET) |
|
155 |
@RequestMapping(value = "/local/tokens/decrypt", method = RequestMethod.GET)
|
|
155 | 156 |
public UserTokens decryptToken(@RequestParam String aaiId) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException { |
156 | 157 |
return userTokensService.getUserTokensByAai(aaiId); |
157 | 158 |
} |
158 | 159 |
|
160 |
|
|
161 |
@PreAuthorize("isAuthenticated()") |
|
162 |
@RequestMapping(value = "/local/tokens/encrypt", method = RequestMethod.GET) |
|
163 |
public UserTokens encryptToken(@RequestParam String aaiId) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException, IOException { |
|
164 |
UserTokens userTokens = userTokensService.getEncryptedUserTokensByAai(aaiId); |
|
165 |
return userTokensService.encryptTokens(userTokens); |
|
166 |
} |
|
159 | 167 |
} |
Also available in: Unified diff
[Trunk | Orcid Service]: getLocalWorks(@RequestBody String[][] pids)" (/local/works).
1. orcidservice.properties: Added properties "apiURL", "tokenURL", "clientId", "clientSecret" for ORCID API.
2. log4j.properties: Added configuration for ORCID log file "uoa-orcid-service-orcid.log".
3. UoaOrcidServiceApplication.java: Added "OrcidConfig.class" in "@EnableConfigurationProperties".
4. SimpleErrorController.java: If body.path.contains("/uoa-orcid-service/orcid"), log error in ORCID log file too.
5. UserTokensService.java: Added method "getEncryptedUserTokensByAai()".
6. UserTokensController.java:
a. Added ORCID log file for responses by ORCID API.
b. Added @Autowired OrcidConfig for getting ORICD token url, client and secret from properties.
c. Added method "getUserOrcidId()" (/local/orcidId)
d. Added method "getPersonalDetailsFromOrcid()" (/orcid/personal-details)
e. Renamed "/orcid/tokens/decrypt" to "/local/tokens/decrypt" & "/orcid/tokens/encrypt" to "/local/tokens/encrypt".
7. Work.java: Added field "updateDate".
8. WorkDAO.java & MongoDBWorkDAO.java: Added methods "List<Work> findByOrcidOrderByCreationDateDesc(String Orcid);" and "Work findByPutCode(String putCode);".
9. WorkService.java:
a. Added method "getLocalWorkByPutCode()".
b. Method "getLocalWorks()" returns works ordered by most recent creation date.
10. WorkController.java:
a. Added ORCID log file for responses by ORCID API.
b. Added @Autowired OrcidConfig for getting ORICD token url, client and secret from properties.
c. Added "charset=UTF-8" in all Content-Type request headers.
d. Renamed "/orcid/put-code" to "/local/put-code".
e. Added method "List<List
f. Added method "List<Work> getMyLocalWorks()" (/local/works).
g. Added method "updateWork()" (/orcid/work/update/{putCode}).
h. Added method "getWorksByPutCodes()" (/orcid/works).