Add Notice of aaf/cadi source moving to aaf/authz
[aaf/cadi.git] / core / src / main / java / org / onap / aaf / cadi / principal / X509Principal.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.cadi.principal;\r
24 \r
25 import java.io.IOException;\r
26 import java.security.cert.CertificateEncodingException;\r
27 import java.security.cert.X509Certificate;\r
28 import java.util.regex.Pattern;\r
29 \r
30 import org.onap.aaf.cadi.GetCred;\r
31 \r
32 public class X509Principal extends BearerPrincipal implements GetCred {\r
33         private static final Pattern pattern = Pattern.compile("[a-zA-Z0-9]*\\@[a-zA-Z0-9.]*");\r
34         private byte[] content;  \r
35         private X509Certificate cert;\r
36         private String name;\r
37 \r
38         public X509Principal(String identity, X509Certificate cert, byte[] content) {\r
39                 name = identity;\r
40                 this.content = content;\r
41                 this.cert = cert;\r
42         }\r
43         \r
44         public X509Principal(X509Certificate cert, byte[] content) throws IOException {\r
45                 this.content=content;\r
46                 this.cert = cert;\r
47                 String subj = cert.getSubjectDN().getName();\r
48                 int cn = subj.indexOf("OU=");\r
49                 if(cn>=0) {\r
50                         cn+=3;\r
51                         int space = subj.indexOf(',',cn);\r
52                         if(space>=0) {\r
53                                 String id = subj.substring(cn, space);\r
54                                 if(pattern.matcher(id).matches()) {\r
55                                         name = id;\r
56                                 }\r
57                         }\r
58                 }\r
59                 if(name==null)\r
60                         throw new IOException("X509 does not have Identity as CN");\r
61                 \r
62         }\r
63         \r
64         \r
65         public String getAsHeader() throws IOException {\r
66                 try {\r
67                         if(content==null) \r
68                                 content=cert.getEncoded();\r
69                 } catch (CertificateEncodingException e) {\r
70                         throw new IOException(e);\r
71                 }\r
72                 return "X509 " + content;\r
73         }\r
74         \r
75         public String toString() {\r
76                 return "X509 Authentication for " + name;\r
77         }\r
78 \r
79 \r
80         public byte[] getCred() {\r
81                 try {\r
82                         return content==null?(content=cert.getEncoded()):content;\r
83                 } catch (CertificateEncodingException e) {\r
84                         return null;\r
85                 }\r
86         }\r
87 \r
88 \r
89         public String getName() {\r
90                 return name;\r
91         }\r
92 }\r