7df3adc3037754d3607ad0b439cafec899bc9788
[aaf/authz.git] / authz-service / src / main / java / org / onap / aaf / authz / cadi / DirectCertIdentity.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.authz.cadi;\r
24 \r
25 import java.nio.ByteBuffer;\r
26 import java.security.Principal;\r
27 import java.security.cert.CertificateException;\r
28 import java.security.cert.X509Certificate;\r
29 import java.util.List;\r
30 \r
31 import javax.servlet.http.HttpServletRequest;\r
32 \r
33 import org.onap.aaf.authz.env.AuthzTrans;\r
34 import org.onap.aaf.authz.layer.Result;\r
35 import org.onap.aaf.cssa.rserv.TransFilter;\r
36 import org.onap.aaf.dao.aaf.cached.CachedCertDAO;\r
37 import org.onap.aaf.dao.aaf.cass.CertDAO.Data;\r
38 \r
39 import org.onap.aaf.cadi.principal.X509Principal;\r
40 import org.onap.aaf.cadi.taf.cert.CertIdentity;\r
41 import org.onap.aaf.cadi.taf.cert.X509Taf;\r
42 \r
43 /**\r
44  * Direct view of CertIdentities\r
45  * \r
46  * Warning:  this class is difficult to instantiate.  The only service that can use it is AAF itself, and is thus \r
47  * entered in the "init" after the CachedCertDAO is created.\r
48  * \r
49  *\r
50  */\r
51 public class DirectCertIdentity implements CertIdentity {\r
52         private static CachedCertDAO certDAO;\r
53 \r
54         @Override\r
55         public Principal identity(HttpServletRequest req, X509Certificate cert, byte[] _certBytes) throws CertificateException {\r
56                 byte[] certBytes = _certBytes;\r
57                 if(cert==null && certBytes==null) {\r
58                     return null;\r
59                 }\r
60                 if(certBytes==null) {\r
61                     certBytes = cert.getEncoded();\r
62                 }\r
63                 byte[] fingerprint = X509Taf.getFingerPrint(certBytes);\r
64 \r
65                 AuthzTrans trans = (AuthzTrans) req.getAttribute(TransFilter.TRANS_TAG);\r
66                 \r
67                 Result<List<Data>> cresp = certDAO.read(trans, ByteBuffer.wrap(fingerprint));\r
68                 if(cresp.isOKhasData()) {\r
69                         Data cdata = cresp.value.get(0);\r
70                         return new X509Principal(cdata.id,cert,certBytes);\r
71                 }\r
72                 return null;\r
73         }\r
74 \r
75         public static void set(CachedCertDAO ccd) {\r
76                 certDAO = ccd;\r
77         }\r
78 \r
79 }\r