Project

General

Profile

Download (2.82 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2015 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.vaadin.util;
11

    
12
import java.net.URI;
13
import java.util.HashMap;
14
import java.util.Map;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.security.core.Authentication;
18
import org.springframework.security.core.context.SecurityContextHolder;
19

    
20
/**
21
 * @author cmathew
22
 * @date 28 Apr 2015
23
 *
24
 */
25
public class CdmVaadinAuthentication {
26
    private final static Logger logger = Logger.getLogger(CdmVaadinAuthentication.class);
27

    
28
    public static final String KEY = "key_authentication";
29

    
30
    Map<String, Authentication> hostAuthenticationMap = new HashMap<String, Authentication>();
31

    
32
    public void addAuthentication(URI requestSourceUri, String requestSourceContext, Authentication authentication) {
33
        addAuthentication(getRequestSource(requestSourceUri, requestSourceContext), authentication);
34
    }
35

    
36
    public void addAuthentication(String requestSource, Authentication authentication) {
37
        if(requestSource == null || requestSource.isEmpty()) {
38
            throw new IllegalStateException("When setting authentication, host cannot be null or empty");
39
        }
40

    
41
        if(authentication == null) {
42
            throw new IllegalStateException("When setting authentication, authentication object cannot be null");
43
        }
44
        hostAuthenticationMap.put(requestSource, authentication);
45
    }
46

    
47
    public boolean isAuthenticated(URI uri, String context) {
48
        if(uri != null && context != null && !context.isEmpty()) {
49
            Authentication authentication = hostAuthenticationMap.get(getRequestSource(uri, context));
50
            if(authentication != null) {
51
                return authentication.isAuthenticated();
52
            }
53
        }
54
        return false;
55
    }
56

    
57
    public Authentication getAuthentication(URI uri, String context){
58
        return hostAuthenticationMap.get(getRequestSource(uri, context));
59
    }
60

    
61
    public boolean setSecurityContextAuthentication(URI uri, String context) {
62
        if(uri != null && context != null && !context.isEmpty()) {
63
            Authentication authentication = hostAuthenticationMap.get(getRequestSource(uri, context));
64
            if(authentication != null && authentication.isAuthenticated()) {
65
                SecurityContextHolder.getContext().setAuthentication(authentication);
66
                return true;
67
            }
68
        }
69
        return false;
70
    }
71

    
72
    public static String getRequestSource(URI uri, String context) {
73
        String source = uri.getHost() + ":" + String.valueOf(uri.getPort()) + context;
74
        logger.warn(" request source : " + source);
75
        return source;
76
    }
77

    
78

    
79

    
80
}
(4-4/8)