[CXF-7429] Preventing NPE
[cxf.git] / rt / rs / security / sso / saml / src / main / java / org / apache / cxf / rs / security / saml / sso / MetadataService.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.cxf.rs.security.saml.sso;
20
21 import java.security.PrivateKey;
22 import java.security.cert.X509Certificate;
23 import java.util.ResourceBundle;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26
27 import javax.security.auth.callback.CallbackHandler;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.Produces;
31
32 import org.w3c.dom.Document;
33 import org.apache.cxf.common.i18n.BundleUtils;
34 import org.apache.cxf.common.logging.LogUtils;
35 import org.apache.cxf.jaxrs.utils.ExceptionUtils;
36 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
37 import org.apache.cxf.message.Message;
38 import org.apache.wss4j.common.crypto.Crypto;
39 import org.apache.wss4j.common.crypto.CryptoType;
40 import org.apache.wss4j.common.ext.WSPasswordCallback;
41
42 @Path("metadata")
43 public class MetadataService extends AbstractSSOSpHandler {
44 protected static final Logger LOG = LogUtils.getL7dLogger(MetadataService.class);
45 protected static final ResourceBundle BUNDLE = BundleUtils.getBundle(MetadataService.class);
46
47 private String serviceAddress;
48 private String assertionConsumerServiceAddress;
49 private String logoutServiceAddress;
50 private boolean addEndpointAddressToContext;
51
52 @GET
53 @Produces("text/xml")
54 public Document getMetadata() {
55 try {
56 MetadataWriter metadataWriter = new MetadataWriter();
57
58 Crypto crypto = getSignatureCrypto();
59 if (crypto == null) {
60 LOG.fine("No crypto instance of properties file configured for signature");
61 throw ExceptionUtils.toInternalServerErrorException(null, null);
62 }
63 String signatureUser = getSignatureUsername();
64 if (signatureUser == null) {
65 LOG.fine("No user configured for signature");
66 throw ExceptionUtils.toInternalServerErrorException(null, null);
67 }
68 CallbackHandler callbackHandler = getCallbackHandler();
69 if (callbackHandler == null) {
70 LOG.fine("No CallbackHandler configured to supply a password for signature");
71 throw ExceptionUtils.toInternalServerErrorException(null, null);
72 }
73
74 CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
75 cryptoType.setAlias(signatureUser);
76 X509Certificate[] issuerCerts = crypto.getX509Certificates(cryptoType);
77 if (issuerCerts == null) {
78 throw new Exception(
79 "No issuer certs were found to sign the request using name: " + signatureUser
80 );
81 }
82
83 // Get the password
84 WSPasswordCallback[] cb = {new WSPasswordCallback(signatureUser, WSPasswordCallback.SIGNATURE)};
85 callbackHandler.handle(cb);
86 String password = cb[0].getPassword();
87
88 // Get the private key
89 PrivateKey privateKey = crypto.getPrivateKey(signatureUser, password);
90
91 if (addEndpointAddressToContext) {
92 Message message = JAXRSUtils.getCurrentMessage();
93 String rawPath = (String)message.get("http.base.path");
94 return metadataWriter.getMetaData(rawPath + serviceAddress,
95 rawPath + assertionConsumerServiceAddress,
96 rawPath + logoutServiceAddress,
97 privateKey, issuerCerts[0],
98 true);
99 } else {
100 return metadataWriter.getMetaData(serviceAddress, assertionConsumerServiceAddress,
101 logoutServiceAddress,
102 privateKey, issuerCerts[0],
103 true);
104 }
105 } catch (Exception ex) {
106 LOG.log(Level.FINE, ex.getMessage(), ex);
107 throw ExceptionUtils.toInternalServerErrorException(ex, null);
108 }
109 }
110
111
112 protected void reportError(String code) {
113 org.apache.cxf.common.i18n.Message errorMsg =
114 new org.apache.cxf.common.i18n.Message(code, BUNDLE);
115 LOG.warning(errorMsg.toString());
116 }
117
118 public String getServiceAddress() {
119 return serviceAddress;
120 }
121
122 public void setServiceAddress(String serviceAddress) {
123 this.serviceAddress = serviceAddress;
124 }
125
126 public String getLogoutServiceAddress() {
127 return logoutServiceAddress;
128 }
129
130 public void setLogoutServiceAddress(String logoutServiceAddress) {
131 this.logoutServiceAddress = logoutServiceAddress;
132 }
133
134 public void setAddEndpointAddressToContext(boolean add) {
135 addEndpointAddressToContext = add;
136 }
137
138
139 public String getAssertionConsumerServiceAddress() {
140 return assertionConsumerServiceAddress;
141 }
142
143
144 public void setAssertionConsumerServiceAddress(String assertionConsumerServiceAddress) {
145 this.assertionConsumerServiceAddress = assertionConsumerServiceAddress;
146 }
147 }