]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ef1a61a40bafea28f005ae385ddd17cb1802fb06
[tmp/jakarta-migration.git] /
1 /*
2  * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the GNU Lesser General Public License
6  * (LGPL) version 2.1 which accompanies this distribution, and is available at
7  * http://www.gnu.org/licenses/lgpl.html
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * Contributors:
15  *     Nuxeo - initial API and implementation
16  *
17  * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
18  */
19
20 package org.collectionspace.hello.services.nuxeo;
21 import java.security.MessageDigest;
22 import java.security.NoSuchAlgorithmException;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Random;
27
28 import com.noelios.restlet.util.Base64;
29
30 public class PortalSSOAuthenticationProvider {
31
32     private static final String TOKEN_SEP = ":";
33
34     private static final String TS_HEADER = "NX_TS";
35
36     private static final String RANDOM_HEADER = "NX_RD";
37
38     private static final String TOKEN_HEADER = "NX_TOKEN";
39
40     private static final String USER_HEADER = "NX_USER";
41
42     public static Map<String, String> getHeaders(String secretKey,
43             String userName) {
44
45         Map<String, String> headers = new HashMap<String, String>();
46
47         Date timestamp = new Date();
48         int randomData = new Random(timestamp.getTime()).nextInt();
49
50         String clearToken = timestamp.getTime() + TOKEN_SEP + randomData
51                 + TOKEN_SEP + secretKey + TOKEN_SEP + userName;
52
53         byte[] hashedToken;
54
55         try {
56             hashedToken = MessageDigest.getInstance("MD5").digest(
57                     clearToken.getBytes());
58         } catch (NoSuchAlgorithmException e) {
59             return null;
60         }
61
62         String base64HashedToken = Base64.encodeBytes(hashedToken);
63
64         headers.put(TS_HEADER, String.valueOf(timestamp.getTime()));
65         headers.put(RANDOM_HEADER, String.valueOf(randomData));
66         headers.put(TOKEN_HEADER, base64HashedToken);
67         headers.put(USER_HEADER, userName);
68
69         return headers;
70     }
71
72 }