70676467111feee19bcedd8eabca4d605adc536f
[aaf/authz.git] / authz-service / src / main / java / com / att / authz / cadi / DirectCertIdentity.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.authz.cadi;\r
25 \r
26 import java.nio.ByteBuffer;\r
27 import java.security.Principal;\r
28 import java.security.cert.CertificateException;\r
29 import java.security.cert.X509Certificate;\r
30 import java.util.List;\r
31 \r
32 import javax.servlet.http.HttpServletRequest;\r
33 \r
34 import com.att.authz.env.AuthzTrans;\r
35 import com.att.authz.layer.Result;\r
36 import com.att.cadi.principal.X509Principal;\r
37 import com.att.cadi.taf.cert.CertIdentity;\r
38 import com.att.cadi.taf.cert.X509Taf;\r
39 import com.att.cssa.rserv.TransFilter;\r
40 import com.att.dao.aaf.cached.CachedCertDAO;\r
41 import com.att.dao.aaf.cass.CertDAO.Data;\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