View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  package org.opencastproject.kernel.security;
23  
24  import org.apache.commons.io.IOUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  import org.springframework.security.crypto.codec.Hex;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.IOException;
32  import java.net.InetAddress;
33  import java.net.UnknownHostException;
34  import java.nio.charset.StandardCharsets;
35  import java.security.MessageDigest;
36  import java.security.NoSuchAlgorithmException;
37  import java.util.Arrays;
38  import java.util.Objects;
39  
40  /**
41   *
42   */
43  public final class SystemTokenRememberMeUtils {
44  
45    private static final Logger logger = LoggerFactory.getLogger(SystemTokenRememberMeUtils.class);
46  
47    /** This is the default cookie key, that is configured in Opencast **/
48    private static final String DEFAULT_COOKIE_KEY = "opencast";
49  
50    private SystemTokenRememberMeUtils() {
51    }
52  
53    public static String augmentKey(String key) {
54  
55      if (!DEFAULT_COOKIE_KEY.equals(key)) {
56        logger.debug("The default cookie key '{}' is not in use. The given key won't be augmented.", DEFAULT_COOKIE_KEY);
57        return key;
58      }
59  
60      // Start with a user key if provided
61      StringBuilder keyBuilder = new StringBuilder(Objects.toString(key, ""));
62  
63      // This will give us the hostname and IP address as something which should be unique per system.
64      // For example: lk.elan-ev.de/10.10.10.31
65      try {
66        keyBuilder.append(InetAddress.getLocalHost());
67      } catch (UnknownHostException e) {
68        // silently ignore this
69      }
70  
71      // Gather additional system properties as key
72      // This requires a proc-fs which should generally be available under Linux.
73      // But even without, we have fallbacks above and below.
74      for (String procFile: Arrays.asList("/proc/version", "/proc/partitions")) {
75        try (FileInputStream fileInputStream = new FileInputStream(new File(procFile))) {
76          keyBuilder.append(IOUtils.toString(fileInputStream, StandardCharsets.UTF_8));
77        } catch (IOException e) {
78          // ignore this
79        }
80      }
81  
82      // If we still have no proper key, just generate a random one.
83      // This will work just fine with the single drawback that restarting Opencast invalidates all remember-me tokens.
84      // But it should be a sufficiently good fallback.
85      key = keyBuilder.toString();
86      if (key.isEmpty()) {
87        logger.warn("Could not generate semi-persistent remember-me key. Will generate a non-persistent random one.");
88        key = Double.toString(Math.random());
89      }
90      logger.debug("Remember me key before hashing: {}", key);
91  
92      // Use a SHA-512 hash as key to have a more sane key.
93      try {
94        MessageDigest digest = MessageDigest.getInstance("SHA-512");
95        key = new String(Hex.encode(digest.digest(key.getBytes())));
96      } catch (NoSuchAlgorithmException e) {
97        logger.warn("No SHA-512 algorithm available!");
98      }
99      logger.debug("Calculated remember me key: {}", key);
100     return key;
101   }
102 }