1
|
package eu.dnetlib.uoaorcidservice.services;
|
2
|
|
3
|
import com.google.gson.Gson;
|
4
|
import eu.dnetlib.uoaorcidservice.dao.UserTokensDAO;
|
5
|
import eu.dnetlib.uoaorcidservice.entities.UserTokens;
|
6
|
import eu.dnetlib.uoaorcidservice.handlers.utils.AESUtils;
|
7
|
import eu.dnetlib.uoaorcidservice.handlers.utils.RolesUtils;
|
8
|
import org.apache.log4j.Logger;
|
9
|
import org.springframework.beans.factory.annotation.Autowired;
|
10
|
import org.springframework.stereotype.Service;
|
11
|
|
12
|
import javax.crypto.BadPaddingException;
|
13
|
import javax.crypto.IllegalBlockSizeException;
|
14
|
import javax.crypto.NoSuchPaddingException;
|
15
|
import javax.crypto.SecretKey;
|
16
|
import javax.crypto.spec.IvParameterSpec;
|
17
|
import java.io.BufferedReader;
|
18
|
import java.io.IOException;
|
19
|
import java.io.StringReader;
|
20
|
import java.security.InvalidAlgorithmParameterException;
|
21
|
import java.security.InvalidKeyException;
|
22
|
import java.security.NoSuchAlgorithmException;
|
23
|
import java.security.spec.InvalidKeySpecException;
|
24
|
import java.util.List;
|
25
|
|
26
|
@Service
|
27
|
public class UserTokensService {
|
28
|
private final Logger log = Logger.getLogger(this.getClass());
|
29
|
|
30
|
@Autowired
|
31
|
private UserTokensDAO userTokensDAO;
|
32
|
|
33
|
@Autowired
|
34
|
private RolesUtils rolesUtils;
|
35
|
|
36
|
public List<UserTokens> getAllUserTokens() {
|
37
|
return userTokensDAO.findAll();
|
38
|
}
|
39
|
|
40
|
public String getUserAccessToken(String orcid) throws BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IOException {
|
41
|
log.debug("getUserAccessToken: orcid="+orcid);
|
42
|
|
43
|
// String aaiId = "3";
|
44
|
String aaiId = rolesUtils.getAaiId();
|
45
|
UserTokens userTokens = userTokensDAO.findByAaiId(aaiId);
|
46
|
if(userTokens == null) {
|
47
|
return null;
|
48
|
}
|
49
|
|
50
|
userTokens = decryptTokens(userTokens);
|
51
|
|
52
|
log.debug("userTokens.getAccessToken(): "+userTokens.getAccessToken());
|
53
|
|
54
|
return userTokens.getAccessToken();
|
55
|
}
|
56
|
|
57
|
public String getCurrentUserOrcid() {
|
58
|
log.debug("getCurrentUserOrcid");
|
59
|
|
60
|
// String aaiId = "1";
|
61
|
String aaiId = rolesUtils.getAaiId();
|
62
|
UserTokens userTokens = userTokensDAO.findByAaiId(aaiId);
|
63
|
if(userTokens == null) {
|
64
|
return null;
|
65
|
}
|
66
|
|
67
|
log.debug("userTokens.getOrcidId(): "+userTokens.getOrcid());
|
68
|
|
69
|
return userTokens.getOrcid();
|
70
|
}
|
71
|
|
72
|
public UserTokens getUserTokens() throws BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IOException {
|
73
|
log.debug("getUserTokens");
|
74
|
String aaiId = rolesUtils.getAaiId();
|
75
|
UserTokens userTokens = userTokensDAO.findByAaiId(aaiId);
|
76
|
return decryptTokens(userTokens);
|
77
|
}
|
78
|
|
79
|
public UserTokens json2UserTokens(String json) {
|
80
|
log.debug("json2UserTokens: "+json);
|
81
|
|
82
|
if (json == null) {
|
83
|
return null;
|
84
|
}
|
85
|
|
86
|
BufferedReader br = new BufferedReader(new StringReader(json));
|
87
|
//convert the json string back to object
|
88
|
Gson gson = new Gson();
|
89
|
UserTokens userTokens = null;
|
90
|
try {
|
91
|
userTokens = gson.fromJson(br, UserTokens.class);
|
92
|
} catch (Exception e) {
|
93
|
log.debug("Error in parsing json response. Given json is : " + json, e);
|
94
|
}
|
95
|
|
96
|
return userTokens;
|
97
|
}
|
98
|
|
99
|
public void saveUserTokens(UserTokens userTokens) throws InvalidKeySpecException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, IOException {
|
100
|
userTokens.setAaiId(rolesUtils.getAaiId());
|
101
|
userTokens = encryptTokens(userTokens);
|
102
|
|
103
|
userTokensDAO.save(userTokens);
|
104
|
}
|
105
|
|
106
|
public UserTokens encryptTokens(UserTokens userTokens) throws InvalidKeySpecException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, IOException {
|
107
|
// String password = "mypassword";
|
108
|
// String salt = "034131145430079";//userTokens.getAaiId();
|
109
|
// IvParameterSpec ivParameterSpec = AESUtils.generateIv();
|
110
|
// SecretKey key = AESUtils.getKeyFromPassword(password,salt);
|
111
|
//
|
112
|
// log.debug("password: "+password);
|
113
|
// log.debug("salt: "+salt);
|
114
|
|
115
|
// String cipherText = AESUtils.encryptPasswordBased("test0", key, ivParameterSpec);
|
116
|
|
117
|
String cipherText = AESUtils.encryptPasswordBased(userTokens.getAccessToken(), userTokens.getAaiId());
|
118
|
userTokens.setAccessToken(cipherText);
|
119
|
cipherText = AESUtils.encryptPasswordBased(userTokens.getRefreshToken(), userTokens.getAaiId());
|
120
|
userTokens.setRefreshToken(cipherText);
|
121
|
// String decryptedCipherText = AESUtils.decryptPasswordBased(cipherText, key, ivParameterSpec);
|
122
|
|
123
|
return userTokens;
|
124
|
}
|
125
|
|
126
|
public UserTokens decryptTokens(UserTokens userTokens) throws InvalidKeySpecException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, IOException {
|
127
|
if(userTokens == null) {
|
128
|
return null;
|
129
|
}
|
130
|
// String password = "mypassword";
|
131
|
// String salt = "034131145430079";//userTokens.getAaiId();
|
132
|
// IvParameterSpec ivParameterSpec = AESUtils.generateIv();
|
133
|
// SecretKey key = AESUtils.getKeyFromPassword(password,salt);
|
134
|
|
135
|
// log.debug("password: "+password);
|
136
|
// log.debug("salt: "+salt);
|
137
|
|
138
|
String token = AESUtils.decryptPasswordBased(userTokens.getAccessToken(), userTokens.getAaiId());
|
139
|
userTokens.setAccessToken(token);
|
140
|
token = AESUtils.decryptPasswordBased(userTokens.getRefreshToken(), userTokens.getAaiId());
|
141
|
userTokens.setRefreshToken(token);
|
142
|
|
143
|
|
144
|
// String plainText = "www.baeldung.com";
|
145
|
// String password2 = "mypassword";
|
146
|
// String salt2 = "034131145430079";
|
147
|
// IvParameterSpec ivParameterSpec2 = AESUtils.generateIv();
|
148
|
// SecretKey key2 = AESUtils.getKeyFromPassword(password2,salt2);
|
149
|
// String cipherText = AESUtils.encryptPasswordBased(plainText, );
|
150
|
// IvParameterSpec ivParameterSpec3 = AESUtils.generateIv();
|
151
|
// SecretKey key3 = AESUtils.getKeyFromPassword(password2,salt2);
|
152
|
// String decryptedCipherText = AESUtils.decryptPasswordBased(cipherText, key3);
|
153
|
// log.debug("test decrypted text: "+decryptedCipherText);
|
154
|
|
155
|
return userTokens;
|
156
|
}
|
157
|
|
158
|
public UserTokens getUserTokensByAai(String aaiId) throws BadPaddingException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IllegalBlockSizeException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IOException {
|
159
|
log.debug("getUserTokens");
|
160
|
UserTokens userTokens = userTokensDAO.findByAaiId(aaiId);
|
161
|
return decryptTokens(userTokens);
|
162
|
}
|
163
|
|
164
|
public UserTokens getEncryptedUserTokensByAai(String aaiId) {
|
165
|
log.debug("getEncryptedUserTokensByAai");
|
166
|
return userTokensDAO.findByAaiId(aaiId);
|
167
|
}
|
168
|
}
|