1 /*
2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 /*
27  *
28  *  (C) Copyright IBM Corp. 1999 All Rights Reserved.
29  *  Copyright 1997 The Open Group Research Institute.  All rights reserved.
30  */
31 
32 package sun.security.krb5.internal.rcache;
33 
34 import java.util.Map;
35 import java.util.concurrent.ConcurrentHashMap;
36 
37 import sun.security.krb5.internal.KerberosTime;
38 import sun.security.krb5.internal.KrbApErrException;
39 import sun.security.krb5.internal.ReplayCache;
40 
41 /**
42  * This class stores replay caches. AuthTimeWithHash objects are categorized
43  * into AuthLists keyed by the names of client and server.
44  *
45  * @author Yanni Zhang
46  */
47 public class MemoryCache extends ReplayCache {
48 
49     // TODO: One day we'll need to read dynamic krb5.conf.
50     private static final int lifespan = KerberosTime.getDefaultSkew();
51     private static final boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
52 
53     private final Map<String,AuthList> content = new ConcurrentHashMap<>();
54 
55     @Override
checkAndStore(KerberosTime currTime, AuthTimeWithHash time)56     public synchronized void checkAndStore(KerberosTime currTime, AuthTimeWithHash time)
57             throws KrbApErrException {
58         String key = time.client + "|" + time.server;
59         content.computeIfAbsent(key, k -> new AuthList(lifespan))
60                 .put(time, currTime);
61         if (DEBUG) {
62             System.out.println("MemoryCache: add " + time + " to " + key);
63         }
64         // TODO: clean up AuthList entries with only expired AuthTimeWithHash objects.
65     }
66 
toString()67     public String toString() {
68         StringBuilder sb = new StringBuilder();
69         for (AuthList rc: content.values()) {
70             sb.append(rc.toString());
71         }
72         return sb.toString();
73     }
74 }
75