001/*
002 * (C) Copyright 2006-2011 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 *     slacoin
018 */
019package org.nuxeo.ecm.automation.client.jaxrs.spi.auth;
020
021import java.security.MessageDigest;
022import java.security.NoSuchAlgorithmException;
023import java.util.Date;
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Random;
027
028import javax.ws.rs.core.MultivaluedMap;
029
030import org.nuxeo.ecm.automation.client.jaxrs.spi.Connector;
031import org.nuxeo.ecm.automation.client.jaxrs.spi.Request;
032import org.nuxeo.ecm.automation.client.jaxrs.spi.RequestInterceptor;
033import org.nuxeo.ecm.automation.client.jaxrs.util.Base64;
034
035import com.sun.jersey.api.client.ClientHandlerException;
036import com.sun.jersey.api.client.ClientRequest;
037import com.sun.jersey.api.client.ClientResponse;
038
039/**
040 * @author matic
041 */
042public class PortalSSOAuthInterceptor extends RequestInterceptor {
043
044    protected static final Random RANDOM = new Random();
045
046    protected final String secret;
047
048    protected final String username;
049
050    public PortalSSOAuthInterceptor(String secretKey, String userName) {
051        this.secret = secretKey;
052        this.username = userName;
053    }
054
055    @Override
056    public void processRequest(Request request, Connector connector) {
057        request.putAll(computeHeaders());
058    }
059
060    protected Map<String, String> computeHeaders() {
061        // compute token
062        long ts = new Date().getTime();
063        long random = RANDOM.nextInt();
064
065        String clearToken = String.format("%d:%d:%s:%s", ts, random, secret, username);
066
067        byte[] hashedToken;
068
069        try {
070            hashedToken = MessageDigest.getInstance("MD5").digest(clearToken.getBytes());
071        } catch (NoSuchAlgorithmException e) {
072            throw new RuntimeException("Cannot compute token", e);
073        }
074
075        String base64HashedToken = Base64.encode(hashedToken);
076        Map<String, String> headers = new HashMap<String, String>();
077        headers.put("NX_TS", String.valueOf(ts));
078        headers.put("NX_RD", String.valueOf(random));
079        headers.put("NX_TOKEN", base64HashedToken);
080        headers.put("NX_USER", username);
081        return headers;
082    }
083
084    @Override
085    public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
086        Map<String, String> computedHeaders = computeHeaders();
087        MultivaluedMap<String, Object> headers = cr.getHeaders();
088        for (Map.Entry<String, String> entry : computedHeaders.entrySet()) {
089            headers.add(entry.getKey(), entry.getValue());
090        }
091        return getNext().handle(cr);
092    }
093}