Project

General

Profile

Download (2.8 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.util.HashMap;
12
import java.util.Map;
13

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

    
18
import eu.etaxonomy.cdm.common.URI;
19

    
20
/**
21
 * @author cmathew
22
 * @since 28 Apr 2015
23
 */
24
public class CdmVaadinAuthentication {
25

    
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<>();
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
}
(5-5/16)