summaryrefslogtreecommitdiffstats
path: root/base/common/src/com/netscape/certsrv/authentication/AuthInterceptor.java
blob: 249828d329cda101d6490411507a77c0f9c689c0 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//--- BEGIN COPYRIGHT BLOCK ---
//This program is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; version 2 of the License.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License along
//with this program; if not, write to the Free Software Foundation, Inc.,
//51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
//(C) 2013 Red Hat, Inc.
//All rights reserved.
//--- END COPYRIGHT BLOCK ---
package com.netscape.certsrv.authentication;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.security.Principal;
import java.util.Collection;
import java.util.HashSet;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.annotations.interception.Precedence;
import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;

import com.netscape.certsrv.base.ForbiddenException;
import com.netscape.cmscore.realm.PKIPrincipal;


/**
 * @author Endi S. Dewata
 */
@Provider
@ServerInterceptor
@Precedence("SECURITY")
public class AuthInterceptor implements PreProcessInterceptor {

    Properties authProperties;

    @Context
    ServletContext servletContext;

    @Context
    SecurityContext securityContext;

    public synchronized void loadAuthProperties() throws IOException {

        if (authProperties != null) return;

        URL url = servletContext.getResource("/WEB-INF/auth.properties");
        authProperties = new Properties();
        authProperties.load(url.openStream());
    }

    @Override
    public ServerResponse preProcess(
            HttpRequest request,
            ResourceMethod resourceMethod
        ) throws Failure, ForbiddenException {

        Class<?> clazz = resourceMethod.getResourceClass();
        Method method = resourceMethod.getMethod();
        System.out.println("AuthInterceptor: invoking "+clazz.getSimpleName()+"."+method.getName()+"()");

        // Get authentication mapping for the method.
        System.out.println("AuthInterceptor: checking method mapping");
        AuthMapping authMapping = method.getAnnotation(AuthMapping.class);

        // If not available, get authentication mapping for the class.
        if (authMapping == null) {
            System.out.println("AuthInterceptor: checking class mapping");
            authMapping = clazz.getAnnotation(AuthMapping.class);
        }

        String name;
        if (authMapping == null) {
            // If not available, use the default mapping.
            name = "default";
        } else {
            // Get the method label
            name = authMapping.value();
        }

        System.out.println("AuthInterceptor: mapping ID: "+name);

        try {
            loadAuthProperties();

            String value = authProperties.getProperty(name);
            Collection<String> authMethods = new HashSet<String>();
            if (value != null) {
                for (String v : value.split(",")) {
                    authMethods.add(v.trim());
                }
            }

            System.out.println("AuthInterceptor: auth methods: "+authMethods);

            Principal principal = securityContext.getUserPrincipal();

            // If unauthenticated, reject request.
            if (principal == null) {
                if (authMethods.isEmpty() || authMethods.contains("anonymous") || authMethods.contains("any")) {
                    System.out.println("AuthInterceptor: anonymous allowed");
                    return null;
                }
                System.out.println("AuthInterceptor: anonymous not allowed");
                throw new ForbiddenException("No user principal provided.");
            }

            // If unrecognized principal, reject request.
            if (!(principal instanceof PKIPrincipal)) {
                System.out.println("AuthInterceptor: invalid principal");
                throw new ForbiddenException("Invalid user principal");
            }

            PKIPrincipal pkiPrincipal = (PKIPrincipal)principal;
            IAuthToken authToken = pkiPrincipal.getAuthToken();

            // If missing auth token, reject request.
            if (authToken == null) {
                System.out.println("AuthInterceptor: missing authentication token");
                throw new ForbiddenException("No authentication token present.");
            }

            Object authManager = authToken.get(AuthToken.TOKEN_AUTHMGR_INST_NAME);
            System.out.println("Authentication manager: "+authManager);

            // Allow request.
            return null;

        } catch (IOException e) {
            e.printStackTrace();
            throw new Failure(e);
        }
    }
}