[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-service / src / main / java / com / att / 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 com.att.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 com.att.authz.env.AuthzTrans;\r
34 import com.att.authz.layer.Result;\r
35 import com.att.cadi.principal.X509Principal;\r
36 import com.att.cadi.taf.cert.CertIdentity;\r
37 import com.att.cadi.taf.cert.X509Taf;\r
38 import com.att.cssa.rserv.TransFilter;\r
39 import com.att.dao.aaf.cached.CachedCertDAO;\r
40 import com.att.dao.aaf.cass.CertDAO.Data;\r
41 \r
42 /**\r
43  * Direct view of CertIdentities\r
44  * \r
45  * Warning:  this class is difficult to instantiate.  The only service that can use it is AAF itself, and is thus \r
46  * entered in the "init" after the CachedCertDAO is created.\r
47  * \r
48  *\r
49  */\r
50 public class DirectCertIdentity implements CertIdentity {\r
51         private static CachedCertDAO certDAO;\r
52 \r
53         @Override\r
54         public Principal identity(HttpServletRequest req, X509Certificate cert, byte[] _certBytes) throws CertificateException {\r
55                 byte[] certBytes = _certBytes;\r
56                 if(cert==null && certBytes==null) {\r
57                     return null;\r
58                 }\r
59                 if(certBytes==null) {\r
60                     certBytes = cert.getEncoded();\r
61                 }\r
62                 byte[] fingerprint = X509Taf.getFingerPrint(certBytes);\r
63 \r
64                 AuthzTrans trans = (AuthzTrans) req.getAttribute(TransFilter.TRANS_TAG);\r
65                 \r
66                 Result<List<Data>> cresp = certDAO.read(trans, ByteBuffer.wrap(fingerprint));\r
67                 if(cresp.isOKhasData()) {\r
68                         Data cdata = cresp.value.get(0);\r
69                         return new X509Principal(cdata.id,cert,certBytes);\r
70                 }\r
71                 return null;\r
72         }\r
73 \r
74         public static void set(CachedCertDAO ccd) {\r
75                 certDAO = ccd;\r
76         }\r
77 \r
78 }\r