Project

General

Profile

« Previous | Next » 

Revision 57920

minor changes for finalization of service

View differences:

modules/dnet-openaire-users/branches/login/src/main/java/eu/dnetlib/openaire/usermanagement/api/Test3Service.java
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
    }
modules/dnet-openaire-users/branches/login/src/main/webapp/WEB-INF/log4j.properties
1
log4j.rootLogger = INFO, R
1
log4j.rootLogger = DEBUG, R
2 2

  
3
log4j.logger.eu.dnetlib = INFO
3
log4j.logger.eu.dnetlib = DEBUG, R
4
log4j.logger.eu.dnetlib.openaire.user = DEBUG, R
4 5
log4j.logger.org.mitre.openid = INFO
5
log4j.logger.org.springframework = INFO, S
6
log4j.logger.org.springframework = DEBUG, S
7
log4j.logger.com.lambdaworks.redis.protocol = INFO, S
8
log4j.logger.org.apache.http = INFO, S
6 9

  
7 10
log4j.additivity.org.springframework = false
8 11

  
modules/dnet-openaire-users/branches/login/pom.xml
88 88
            <version>3.0.1</version>
89 89
            <scope>provided</scope>
90 90
        </dependency>
91
        <dependency>
92
            <groupId>commons-io</groupId>
93
            <artifactId>commons-io</artifactId>
94
            <version>2.6</version>
95
        </dependency>
96
        <dependency>
97
            <groupId>javax.xml.bind</groupId>
98
            <artifactId>jaxb-api</artifactId>
99
            <version>2.3.1</version>
100
        </dependency>
91 101
    </dependencies>
92 102
</project>
93 103

  

Also available in: Unified diff