14 |
14 |
import eu.dnetlib.openaire.user.dao.SQLMigrationUserDAO;
|
15 |
15 |
import eu.dnetlib.openaire.user.ldap.MUserActionsLDAP;
|
16 |
16 |
import eu.dnetlib.openaire.user.store.DataSourceConnector;
|
|
17 |
import org.apache.commons.io.IOUtils;
|
|
18 |
import org.apache.http.HttpResponse;
|
|
19 |
import org.apache.http.NameValuePair;
|
|
20 |
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
21 |
import org.apache.http.client.methods.HttpPost;
|
|
22 |
import org.apache.http.impl.client.CloseableHttpClient;
|
|
23 |
import org.apache.http.impl.client.HttpClients;
|
|
24 |
import org.apache.http.message.BasicNameValuePair;
|
17 |
25 |
import org.apache.log4j.Logger;
|
18 |
26 |
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
19 |
27 |
import org.mitre.openid.connect.model.UserInfo;
|
... | ... | |
21 |
29 |
import org.springframework.beans.factory.annotation.Value;
|
22 |
30 |
import org.springframework.http.*;
|
23 |
31 |
import org.springframework.http.HttpMethod;
|
|
32 |
import org.springframework.security.access.prepost.PreAuthorize;
|
|
33 |
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
24 |
34 |
import org.springframework.security.core.context.SecurityContextHolder;
|
|
35 |
import org.springframework.security.core.userdetails.UserDetails;
|
25 |
36 |
import org.springframework.stereotype.Component;
|
26 |
37 |
import org.springframework.web.client.DefaultResponseErrorHandler;
|
27 |
38 |
import org.springframework.web.client.RestTemplate;
|
|
39 |
import sun.net.www.http.HttpClient;
|
28 |
40 |
|
29 |
41 |
import javax.ws.rs.*;
|
30 |
42 |
import javax.ws.rs.core.MediaType;
|
31 |
43 |
import javax.ws.rs.core.Response;
|
|
44 |
import java.io.IOException;
|
|
45 |
import java.io.InputStream;
|
|
46 |
import java.io.UnsupportedEncodingException;
|
|
47 |
import java.nio.charset.StandardCharsets;
|
32 |
48 |
import java.sql.SQLException;
|
|
49 |
import java.util.ArrayList;
|
|
50 |
import java.util.List;
|
33 |
51 |
|
34 |
52 |
/**
|
35 |
53 |
* Created by sofia on 24/11/2016.
|
... | ... | |
52 |
70 |
@Value("${oidc.issuer}")
|
53 |
71 |
private String issuer;
|
54 |
72 |
|
|
73 |
@Value("${oidc.secret}")
|
|
74 |
private String secret;
|
|
75 |
|
|
76 |
@Value("${oidc.id}")
|
|
77 |
private String id;
|
|
78 |
|
55 |
79 |
@GET
|
|
80 |
@Path("/getToken")
|
|
81 |
@Produces(MediaType.APPLICATION_JSON)
|
|
82 |
public Response getToken(@QueryParam("accessToken") String accessToken){
|
|
83 |
logger.debug("Refresh token " + accessToken);
|
|
84 |
System.out.printf("HELLO PAPAGENA");
|
|
85 |
CloseableHttpClient httpclient = HttpClients.createDefault();
|
|
86 |
HttpPost httppost = new HttpPost(issuer+"/token");
|
|
87 |
|
|
88 |
// Request parameters and other properties.
|
|
89 |
List<NameValuePair> params = new ArrayList<NameValuePair>();
|
|
90 |
params.add(new BasicNameValuePair("client_id", id));
|
|
91 |
params.add(new BasicNameValuePair("client_secret", secret));
|
|
92 |
params.add(new BasicNameValuePair("grant_type", "refresh_token"));
|
|
93 |
params.add(new BasicNameValuePair("refresh_token", accessToken));
|
|
94 |
params.add(new BasicNameValuePair("scope", "openid email profile"));
|
|
95 |
try {
|
|
96 |
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
|
|
97 |
//Execute and get the response.
|
|
98 |
HttpResponse response = null;
|
|
99 |
|
|
100 |
response = httpclient.execute(httppost);
|
|
101 |
|
|
102 |
org.apache.http.HttpEntity entity = response.getEntity();
|
|
103 |
|
|
104 |
logger.debug("I am here");
|
|
105 |
if (entity != null) {
|
|
106 |
try (InputStream instream = entity.getContent()) {
|
|
107 |
logger.debug(IOUtils.toString(instream, StandardCharsets.UTF_8.name()));
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
} catch (UnsupportedEncodingException e) {
|
|
112 |
logger.error(e);
|
|
113 |
|
|
114 |
} catch (IOException e) {
|
|
115 |
logger.error(e);
|
|
116 |
}
|
|
117 |
|
|
118 |
logger.info("DDDDDDDD");
|
|
119 |
|
|
120 |
return Response.status(200).type(MediaType.APPLICATION_JSON).build();
|
|
121 |
}
|
|
122 |
|
|
123 |
@GET
|
56 |
124 |
@Path("/getUserInfo")
|
57 |
125 |
@Produces(MediaType.APPLICATION_JSON)
|
|
126 |
//TODO REMOVE THIS and make the request directly to aai service {oidc.issuer} OR! see what is done with redis
|
58 |
127 |
public Response getUserInfo(@QueryParam("accessToken") String accessToken) throws JsonProcessingException {
|
59 |
128 |
//return Response.status(404).entity(compose404Message("This is a test message.")).type(MediaType.APPLICATION_JSON).build();
|
60 |
129 |
// call aai with accessToken
|
... | ... | |
71 |
140 |
//logger.info(restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class));
|
72 |
141 |
ResponseEntity response1 = restTemplate.exchange(fooResourceUrl, HttpMethod.GET, request, Object.class);
|
73 |
142 |
logger.info(response1.getBody().toString());
|
|
143 |
|
74 |
144 |
ObjectMapper mapper = new ObjectMapper();
|
75 |
145 |
|
76 |
146 |
return Response.status(response1.getStatusCode().value()).entity(mapper.writeValueAsString(response1.getBody())).type(MediaType.APPLICATION_JSON).build();
|
... | ... | |
113 |
183 |
}
|
114 |
184 |
return Response.status(200).entity(userInfoJson.toString()).type(MediaType.APPLICATION_JSON).build();
|
115 |
185 |
}
|
|
186 |
|
|
187 |
/*
|
|
188 |
@GET
|
|
189 |
@Path("/katerina")
|
|
190 |
@Produces(MediaType.APPLICATION_JSON)
|
|
191 |
//@PreAuthorize("hasRole('ROLE_USER')")
|
|
192 |
@PreAuthorize("hasAuthority('urn:geant:openaire.eu:group:Registered+User#aai.openaire.eu')")
|
|
193 |
public Response getKaterina() {
|
|
194 |
return Response.status(200).build();
|
|
195 |
}
|
|
196 |
|
|
197 |
@GET
|
|
198 |
@Path("/skata")
|
|
199 |
@Produces(MediaType.APPLICATION_JSON)
|
|
200 |
@PreAuthorize("hasRole('ROLE_USER')")
|
|
201 |
public Response getKaterina2() {
|
|
202 |
return Response.status(200).build();
|
|
203 |
}
|
|
204 |
|
|
205 |
@GET
|
|
206 |
@Path("/skata2")
|
|
207 |
@Produces(MediaType.APPLICATION_JSON)
|
|
208 |
@PreAuthorize("hasAuthority('skata')")
|
|
209 |
public Response getKaterina3() {
|
|
210 |
return Response.status(200).build();
|
|
211 |
}
|
|
212 |
|
|
213 |
|
|
214 |
@GET
|
|
215 |
@Path("/me")
|
|
216 |
//@Produces(MediaType.APPLICATION_JSON)
|
|
217 |
public Response getKaterina(@AuthenticationPrincipal UserDetails userDetails) {
|
|
218 |
//return Response.status(200).entity(userDetails).type(MediaType.APPLICATION_JSON).build();
|
|
219 |
return Response.status(200).build();
|
|
220 |
}
|
|
221 |
*/
|
|
222 |
|
116 |
223 |
/* JSON Utility Methods */
|
117 |
|
|
118 |
224 |
private String compose401Message(String message) {
|
119 |
225 |
return "{ \"status\" : \"error\", \"code\" : \"401\", \"message\" : \" " + message +" \" }";
|
120 |
226 |
}
|
minor changes for finalization of service