Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / TokenRetriever.java @ 01a30cd0

History | View | Annotate | Download (3.8 kB)

1 14ad7326 pastith
/*
2 14ad7326 pastith
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3 14ad7326 pastith
 *
4 14ad7326 pastith
 * This file is part of GSS.
5 14ad7326 pastith
 *
6 14ad7326 pastith
 * GSS is free software: you can redistribute it and/or modify
7 14ad7326 pastith
 * it under the terms of the GNU General Public License as published by
8 14ad7326 pastith
 * the Free Software Foundation, either version 3 of the License, or
9 14ad7326 pastith
 * (at your option) any later version.
10 14ad7326 pastith
 *
11 14ad7326 pastith
 * GSS is distributed in the hope that it will be useful,
12 14ad7326 pastith
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14ad7326 pastith
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 14ad7326 pastith
 * GNU General Public License for more details.
15 14ad7326 pastith
 *
16 14ad7326 pastith
 * You should have received a copy of the GNU General Public License
17 14ad7326 pastith
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 14ad7326 pastith
 */
19 14ad7326 pastith
package gr.ebs.gss.server;
20 14ad7326 pastith
21 14ad7326 pastith
import gr.ebs.gss.client.exceptions.ObjectNotFoundException;
22 14ad7326 pastith
import gr.ebs.gss.client.exceptions.RpcException;
23 14ad7326 pastith
import gr.ebs.gss.server.domain.User;
24 14ad7326 pastith
25 14ad7326 pastith
import java.io.IOException;
26 14ad7326 pastith
import java.io.PrintWriter;
27 553e6584 pastith
import java.net.URLEncoder;
28 14ad7326 pastith
29 14ad7326 pastith
import javax.servlet.http.HttpServletRequest;
30 14ad7326 pastith
import javax.servlet.http.HttpServletResponse;
31 14ad7326 pastith
32 14ad7326 pastith
import org.apache.commons.codec.binary.Base64;
33 14ad7326 pastith
import org.apache.commons.logging.Log;
34 14ad7326 pastith
import org.apache.commons.logging.LogFactory;
35 14ad7326 pastith
36 14ad7326 pastith
/**
37 14ad7326 pastith
 * The servlet that handles authentication token retrieval.
38 14ad7326 pastith
 *
39 14ad7326 pastith
 * @author past
40 14ad7326 pastith
 */
41 978061e3 Panagiotis Astithas
public class TokenRetriever extends BaseServlet {
42 14ad7326 pastith
        /**
43 14ad7326 pastith
         * The serial version UID of the class.
44 14ad7326 pastith
         */
45 14ad7326 pastith
        private static final long serialVersionUID = 1L;
46 14ad7326 pastith
47 14ad7326 pastith
        /**
48 14ad7326 pastith
         * The request parameter name for the nonce.
49 14ad7326 pastith
         */
50 14ad7326 pastith
        private static final String NONCE_PARAM = "nonce";
51 14ad7326 pastith
52 14ad7326 pastith
        /**
53 14ad7326 pastith
         * The request parameter name for the user.
54 14ad7326 pastith
         */
55 14ad7326 pastith
        private static final String USER_PARAM = "user";
56 14ad7326 pastith
57 14ad7326 pastith
        /**
58 14ad7326 pastith
         * The logger.
59 14ad7326 pastith
         */
60 14ad7326 pastith
        private static Log logger = LogFactory.getLog(TokenRetriever.class);
61 14ad7326 pastith
62 14ad7326 pastith
        @Override
63 14ad7326 pastith
        public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
64 14ad7326 pastith
                String username = request.getParameter(USER_PARAM);
65 14ad7326 pastith
                String nonceEncoded = request.getParameter(NONCE_PARAM);
66 14ad7326 pastith
                User user = null;
67 14ad7326 pastith
                if (username == null) {
68 14ad7326 pastith
                        String error = "No username supplied";
69 14ad7326 pastith
                        logger.info(error);
70 14ad7326 pastith
                        response.setContentType("text/html");
71 14ad7326 pastith
                        response.sendError(HttpServletResponse.SC_FORBIDDEN, error);
72 14ad7326 pastith
                        return;
73 14ad7326 pastith
                }
74 14ad7326 pastith
                if (nonceEncoded == null) {
75 14ad7326 pastith
                        String error = "No nonce supplied";
76 14ad7326 pastith
                        logger.info(error);
77 14ad7326 pastith
                        response.setContentType("text/html");
78 14ad7326 pastith
                        response.sendError(HttpServletResponse.SC_FORBIDDEN, error);
79 14ad7326 pastith
                        return;
80 14ad7326 pastith
                }
81 553e6584 pastith
                nonceEncoded = URLEncoder.encode(nonceEncoded, "US-ASCII");
82 14ad7326 pastith
                try {
83 14ad7326 pastith
                        user = getService().findUser(username);
84 14ad7326 pastith
                        if (user == null) {
85 14ad7326 pastith
                                String error = "User was not found";
86 14ad7326 pastith
                                logger.info(error);
87 14ad7326 pastith
                                response.setContentType("text/html");
88 14ad7326 pastith
                                response.sendError(HttpServletResponse.SC_FORBIDDEN, error);
89 14ad7326 pastith
                                return;
90 14ad7326 pastith
                        }
91 14ad7326 pastith
                        String nonce = user.getNonce();
92 14ad7326 pastith
                        if (nonce == null || !nonce.equals(nonceEncoded))
93 14ad7326 pastith
                                throw new ObjectNotFoundException("No match found");
94 14ad7326 pastith
                } catch (RpcException e) {
95 14ad7326 pastith
                        String error = "An error occurred while communicating with the service";
96 14ad7326 pastith
                        logger.error(error, e);
97 14ad7326 pastith
                        response.setContentType("text/html");
98 14ad7326 pastith
                        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, error);
99 14ad7326 pastith
                        return;
100 14ad7326 pastith
                } catch (ObjectNotFoundException e) {
101 14ad7326 pastith
                        logger.info(e.getMessage());
102 14ad7326 pastith
                        response.setContentType("text/html");
103 14ad7326 pastith
                        response.sendError(HttpServletResponse.SC_FORBIDDEN, e.getMessage());
104 14ad7326 pastith
                        return;
105 14ad7326 pastith
                }
106 14ad7326 pastith
                byte[] token = user.getAuthToken();
107 14ad7326 pastith
                if (token == null) {
108 14ad7326 pastith
                        String error = "Authentication token invalid";
109 14ad7326 pastith
                        logger.error(error);
110 14ad7326 pastith
                        response.setContentType("text/html");
111 14ad7326 pastith
                        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, error);
112 14ad7326 pastith
                        return;
113 14ad7326 pastith
                }
114 14ad7326 pastith
                user.setNonce(null);
115 14ad7326 pastith
                user.setNonceExpiryDate(null);
116 14ad7326 pastith
                String tokenEncoded = new String(Base64.encodeBase64(token), "US-ASCII");
117 14ad7326 pastith
                response.setContentType("text/plain");
118 14ad7326 pastith
            PrintWriter out = response.getWriter();
119 14ad7326 pastith
            out.println(tokenEncoded);
120 14ad7326 pastith
        }
121 14ad7326 pastith
}