summaryrefslogtreecommitdiffstats
path: root/java/gov/nist/javax/sip/clientauthutils/CredentialsCache.java
blob: 2a84f28f8a9c9004089e91b51e0ce1a7958b0ff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package gov.nist.javax.sip.clientauthutils;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

import javax.sip.*;
import javax.sip.header.*;
import javax.sip.address.*;
import javax.sip.message.*;

/**
 * A cache of authorization headers to be used for subsequent processing when we
 * set up calls. We cache credentials on a per proxy domain per user basis.
 *
 */

class CredentialsCache {


    /**
     * The key for this map is the proxy domain name. A given proxy authorizes a
     * user for a number of domains. The Hashtable value of the mapping is a
     * mapping of user names to AuthorizationHeader list for that proxy domain.
     */
    private ConcurrentHashMap<String, List<AuthorizationHeader>> authorizationHeaders =
            new ConcurrentHashMap<String, List<AuthorizationHeader>>();
    private Timer timer;

    class TimeoutTask extends TimerTask {
        String callId;
        String userName;

        public TimeoutTask(String userName, String proxyDomain) {
            this.callId = proxyDomain;
            this.userName = userName;
        }

        @Override
        public void run() {
            authorizationHeaders.remove(callId);

        }

    }



    CredentialsCache (Timer timer) {
        this.timer = timer;
    }

    /**
     * Cache the bindings of proxyDomain and authorization header.
     *
     * @param callid
     *            the id of the call that the <tt>authorization</tt> header
     *            belongs to.
     * @param authorization
     *            the authorization header that we'd like to cache.
     */
    void cacheAuthorizationHeader(String callId,
            AuthorizationHeader authorization, int cacheTime) {
        String user = authorization.getUsername();
        if ( callId == null) throw new NullPointerException("Call ID is null!");
        if ( authorization == null) throw new NullPointerException("Null authorization domain");

        List<AuthorizationHeader> authHeaders = authorizationHeaders.get(callId);
        if (authHeaders == null) {
            authHeaders = new LinkedList<AuthorizationHeader>();
            authorizationHeaders.put(callId, authHeaders);
        } else {
            String realm = authorization.getRealm();
            for (ListIterator<AuthorizationHeader> li = authHeaders.listIterator(); li.hasNext();) {
                AuthorizationHeader authHeader = (AuthorizationHeader) li.next();
                if ( realm.equals(authHeader.getRealm()) ) {
                    li.remove();
                }
            }
        }

        authHeaders.add(authorization);

        TimeoutTask timeoutTask  = new TimeoutTask( callId,user);
        if ( cacheTime != -1)
            this.timer.schedule(timeoutTask, cacheTime*1000);


    }

    /**
     * Returns an authorization header cached for the specified call id and null
     * if no authorization header has been previously cached for this call.
     *
     * @param callid
     *            the call id that we'd like to retrive a cached authorization
     *            header for.
     *
     * @return authorization header corresponding to that user for the given
     *         proxy domain.
     */
    Collection<AuthorizationHeader> getCachedAuthorizationHeaders(
            String callid) {
        if (callid == null)
            throw new NullPointerException("Null arg!");
        return this.authorizationHeaders.get(callid);

    }

    /**
     * Remove a cached authorization header.
     *
     * @param callId
     */
    public void removeAuthenticationHeader(String callId) {
        this.authorizationHeaders.remove(callId);

    }

}