Project

General

Profile

« Previous | Next » 

Revision 57598

added uoa-login-core

View differences:

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 eu.dnetlib.openaire.usermanagement.security.JWTGenerator;
18 17
import org.apache.log4j.Logger;
19 18
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
20 19
import org.mitre.openid.connect.model.UserInfo;
......
54 53
    private String issuer;
55 54

  
56 55
    @GET
57
    @Path("/{userId}")
58
    @Produces(MediaType.APPLICATION_JSON)
59
    public Response getUserById(@PathParam("userId") int userId) {
60
        try {
61
            MigrationUser mUser = sqlMigrationUserDAO.fetchById(userId);
62

  
63
            // Invalide user ID
64
            if (mUser == null) {
65
                String errorMessageJson = compose404Message("Cannot find user with id " + userId + ".");
66

  
67
                return Response
68
                        .status(Response.Status.NOT_FOUND)
69
                        .entity(errorMessageJson)
70
                        .type(MediaType.APPLICATION_JSON)
71
                        .build();
72
            }
73

  
74
            return Response.status(200).entity(composeDataResponse(mUser)).build();
75
        }
76
        catch (SQLException e) {
77
            return Response
78
                    .status(Response.Status.INTERNAL_SERVER_ERROR)
79
                    .entity(compose500Message("Fail to fetch users.", e))
80
                    .type(MediaType.APPLICATION_JSON)
81
                    .build();
82
        }
83
    }
84

  
85
     /* How to check @browser ../authenticate/?username=MY_USERNAME&password=MY_PASSWORD
86
     * http://localhost:8080/uoa-user-management-1.0.0-SNAPSHOT/api/users/authenticate?username=sba&password=12345678
87
    @GET
88
    @Path("/authenticate")
89
    @Produces(MediaType.APPLICATION_JSON)
90
    public Response authenticateUserGET(@QueryParam("username") String username, @QueryParam("password") String password)
91
    {
92
        return commonAuthenticateFunction(username, password);
93

  
94
    }*/
95

  
96
    @POST
97
    @Path("/authenticates")
98
    @Produces(MediaType.APPLICATION_JSON)
99
    @Consumes(MediaType.APPLICATION_JSON)
100
    public Response authenticateUserPOST(String input) {
101
        JsonObject jsonObject = new JsonParser().parse(input).getAsJsonObject();
102

  
103
        String username = jsonObject.get("username").getAsString();
104
        String password = jsonObject.get("password").getAsString();
105

  
106
        return commonAuthenticateFunction(username, password);
107
    }
108

  
109
    private Response commonAuthenticateFunction(String username, String password)
110
    {
111
        try {
112
            boolean usernameExists = mUserActionsLDAP.usernameExists(username);
113

  
114
            // if user was not found
115
            if (!usernameExists) {
116
                String errorMessageJson = compose401Message("Wrong credentials.");
117

  
118
                return Response
119
                        .status(Response.Status.UNAUTHORIZED)
120
                        .entity(errorMessageJson)
121
                        .type(MediaType.APPLICATION_JSON)
122
                        .build();
123
            }
124

  
125
            boolean authenticated = mUserActionsLDAP.authenticate(username, password);
126

  
127
            // if user was not authenticated
128
            if (!authenticated) {
129
                return Response
130
                        .status(Response.Status.UNAUTHORIZED)
131
                        .entity(compose401Message("User " + username + " could not be authenticated."))
132
                        .type(MediaType.APPLICATION_JSON)
133
                        .build();
134
            }
135

  
136
            MigrationUser mUser = sqlMigrationUserDAO.fetchByUsername(username);
137

  
138
            // if user was not found in my db
139
            LDAPUser ldapUser = null;
140
            if (mUser == null) {
141
                mUser = new MigrationUser(username);
142
                ldapUser = mUserActionsLDAP.getUser(username);
143
                mUser.setFullname(ldapUser.getDisplayName());
144
                mUser.setEmail(ldapUser.getEmail());
145
                mUser.setRoleId(2);
146

  
147

  
148
                sqlMigrationUserDAO.insert(mUser);
149
            }
150
            return Response.status(200).entity(composeDataResponse(mUser)).type(MediaType.APPLICATION_JSON).build();
151

  
152
        } catch (LDAPException exc) {
153
            logger.error("Fail to connect to LDAP. ", exc);
154
            return Response
155
                    .status(Response.Status.INTERNAL_SERVER_ERROR)
156
                    .entity(compose500Message("LDAP error.", exc))
157
                    .type(MediaType.APPLICATION_JSON)
158
                    .build();
159

  
160
        } catch (SQLException exc) {
161
            logger.error("Fail to fetch users. ", exc);
162
            return Response
163
                    .status(Response.Status.INTERNAL_SERVER_ERROR)
164
                    .entity(compose500Message("Fail to fetch users.", exc))
165
                    .type(MediaType.APPLICATION_JSON)
166
                    .build();
167
        }
168

  
169
    }
170

  
171
    @GET
172
    @Path("/changeRole")
173
    @Produces(MediaType.APPLICATION_JSON)
174
    public Response changeRole(@QueryParam("roleId") int roleId, @QueryParam("userId") int userId)
175
    {
176
        RoleDAO roleDAO = new RoleDAO();
177
        try
178
        {
179
            Role role = roleDAO.fetchById(roleId);
180
            if (role == null)
181
            {
182
                //fetch all roleids TODO
183
                String errorMessageJson = compose404Message("Cannot find role with id" + roleId + ".");
184

  
185
                return Response
186
                        .status(Response.Status.NOT_FOUND)
187
                        .entity(errorMessageJson)
188
                        .type(MediaType.APPLICATION_JSON)
189
                        .build();
190
            }
191

  
192
            MigrationUser mUser = sqlMigrationUserDAO.fetchById(userId);
193

  
194
            if (mUser == null)
195
            {
196
                String errorMessageJson = compose404Message("Cannot find user with id " + userId + ".");
197

  
198
                return Response
199
                        .status(Response.Status.NOT_FOUND)
200
                        .entity(errorMessageJson)
201
                        .type(MediaType.APPLICATION_JSON)
202
                        .build();
203
            }
204

  
205
            mUser.setRoleId(roleId);
206
            sqlMigrationUserDAO.update(mUser);
207

  
208
            return Response.status(200).entity(composeDataResponse(mUser)).build();
209
        }
210
        catch (SQLException exc)
211
        {
212
            return Response
213
                    .status(Response.Status.INTERNAL_SERVER_ERROR)
214
                    .entity(compose500Message("Fail to fetch users.", exc))
215
                    .type(MediaType.APPLICATION_JSON)
216
                    .build();
217
        }
218
    }
219

  
220
    @GET
221 56
    @Path("/getUserInfo")
222 57
    @Produces(MediaType.APPLICATION_JSON)
223 58
    public Response getUserInfo(@QueryParam("accessToken") String accessToken) throws JsonProcessingException {
......
293 128
                "\"description\" : \""+  exception.getMessage() +"\" }";
294 129
    }
295 130

  
296
    private String composeDataResponse(UserInfo user) {
297
        return "{ \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : \"" + JWTGenerator.generateToken(user, "my-very-secret") + "\" }";
298
    }
299

  
300
    private String composeDataResponse(MigrationUser user) {
301
        //return "{ \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(user) + " }";
302
        return "{ \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : \"" + JWTGenerator.generateToken(user, "my-very-secret") + "\" }";
303
    }
304

  
305 131
    private String composeDataResponse(LDAPUser user) {
306 132
        return " { \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(user) + " }";
307 133
    }
308 134

  
309
//        private String composeDataResponse(String username) {
310
//            return " { \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(username) + " }";
311
//        }
312

  
313 135
    private String composeDataResponse(String fullname) {
314 136
        return " { \"status\" : \"success\", \"code\": \"200\", " + "\"data\" : " + new Gson().toJson(fullname) + " }";
315 137
    }

Also available in: Unified diff