Project

General

Profile

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

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

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

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

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

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

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

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

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

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

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

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

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

    
77

    
78

    
79
}
(5-5/15)