01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.security;
18:
19: import java.io.IOException;
20: import java.security.Signature;
21:
22: import javax.servlet.ServletInputStream;
23: import javax.servlet.http.HttpServletRequest;
24: import javax.servlet.http.HttpServletRequestWrapper;
25:
26: import org.apache.commons.codec.binary.Base64;
27: import org.apache.commons.lang.StringUtils;
28: import org.kuali.bus.services.KSBServiceLocator;
29: import org.kuali.rice.RiceConstants;
30:
31: /**
32: * An HttpServletRequestWrapper which will wraps the underlying request's InputStream in a
33: * SignatureVerifyingInputStream which will verify the digital signature of the request after
34: * all of the data has been read from the input stream.
35: *
36: * @author Kuali Rice Team (kuali-rice@googlegroups.com)
37: */
38: public class SignatureVerifyingRequestWrapper extends
39: HttpServletRequestWrapper {
40:
41: private byte[] digitalSignature;
42: private Signature signature;
43:
44: public SignatureVerifyingRequestWrapper(HttpServletRequest request) {
45: super (request);
46: String encodedSignature = request
47: .getHeader(RiceConstants.DIGITAL_SIGNATURE_HEADER);
48: if (StringUtils.isEmpty(encodedSignature)) {
49: throw new RuntimeException(
50: "A digital signature was required on the request but none was found.");
51: }
52: String verificationAlias = request
53: .getHeader(RiceConstants.KEYSTORE_ALIAS_HEADER);
54: if (StringUtils.isEmpty(verificationAlias)) {
55: throw new RuntimeException(
56: "A verification alias was required on the request but none was found.");
57: }
58: try {
59: this .digitalSignature = Base64
60: .decodeBase64(encodedSignature.getBytes("UTF-8"));
61: this .signature = KSBServiceLocator
62: .getDigitalSignatureService()
63: .getSignatureForVerification(verificationAlias);
64: } catch (Exception e) {
65: throw new RuntimeException(
66: "Failed to initialize digital signature verification.",
67: e);
68: }
69: }
70:
71: @Override
72: public ServletInputStream getInputStream() throws IOException {
73: return new SignatureVerifyingInputStream(this.digitalSignature,
74: this.signature, super.getInputStream());
75: }
76:
77: }
|