001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.ui.web.auth.portal;
023
024import java.security.MessageDigest;
025import java.security.NoSuchAlgorithmException;
026import java.util.Base64;
027import java.util.Date;
028import java.util.List;
029import java.util.Map;
030
031import javax.servlet.http.HttpServletRequest;
032import javax.servlet.http.HttpServletResponse;
033
034import org.nuxeo.ecm.platform.api.login.UserIdentificationInfo;
035import org.nuxeo.ecm.platform.ui.web.auth.interfaces.NuxeoAuthenticationPlugin;
036
037public class PortalAuthenticator implements NuxeoAuthenticationPlugin {
038
039    public static final String SECRET_KEY_NAME = "secret";
040
041    public static final String MAX_AGE_KEY_NAME = "maxAge";
042
043    private static final String TS_HEADER = "NX_TS";
044
045    private static final String RANDOM_HEADER = "NX_RD";
046
047    private static final String TOKEN_HEADER = "NX_TOKEN";
048
049    private static final String USER_HEADER = "NX_USER";
050
051    private static final String TOKEN_SEP = ":";
052
053    //
054    private String secret = "secret";
055
056    // one hour by default
057    private long maxAge = 60 * 60;
058
059    public List<String> getUnAuthenticatedURLPrefix() {
060        return null;
061    }
062
063    public Boolean handleLoginPrompt(HttpServletRequest httpRequest, HttpServletResponse httpResponse, String baseURL) {
064        return false;
065    }
066
067    public UserIdentificationInfo handleRetrieveIdentity(HttpServletRequest httpRequest,
068            HttpServletResponse httpResponse) {
069
070        String ts = httpRequest.getHeader(TS_HEADER);
071        String random = httpRequest.getHeader(RANDOM_HEADER);
072        String token = httpRequest.getHeader(TOKEN_HEADER);
073        String userName = httpRequest.getHeader(USER_HEADER);
074
075        if (userName == null || ts == null || random == null || token == null) {
076            return null;
077        }
078
079        if (validateToken(ts, random, token, userName)) {
080            return new UserIdentificationInfo(userName, userName);
081        } else {
082            return null;
083        }
084    }
085
086    public void initPlugin(Map<String, String> parameters) {
087        if (parameters.containsKey(SECRET_KEY_NAME)) {
088            secret = parameters.get(SECRET_KEY_NAME);
089        }
090        if (parameters.containsKey(MAX_AGE_KEY_NAME)) {
091            String maxAgeStr = parameters.get(MAX_AGE_KEY_NAME);
092            if (maxAgeStr != null && !maxAgeStr.equals("")) {
093                maxAge = Long.parseLong(maxAgeStr);
094            }
095        }
096    }
097
098    public Boolean needLoginPrompt(HttpServletRequest httpRequest) {
099        return false;
100    }
101
102    protected Boolean validateToken(String ts, String random, String token, String userName) {
103        // reconstruct the token
104        String clearToken = ts + TOKEN_SEP + random + TOKEN_SEP + secret + TOKEN_SEP + userName;
105
106        byte[] hashedToken;
107        try {
108            hashedToken = MessageDigest.getInstance("MD5").digest(clearToken.getBytes());
109        } catch (NoSuchAlgorithmException e) {
110            return false;
111        }
112        String base64HashedToken = Base64.getEncoder().encodeToString(hashedToken);
113
114        // check that tokens are the same => that we have the same shared key
115        if (!base64HashedToken.equals(token)) {
116            return false;
117        }
118
119        // check time stamp
120        long portalTS = Long.parseLong(ts);
121        long currentTS = (new Date()).getTime();
122
123        return (currentTS - portalTS) / 1000 <= maxAge;
124    }
125
126}