200b81741e2d07acb6160058021516088cf458fd
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / principal / X509Principal.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.principal;
23
24 import java.io.IOException;
25 import java.security.cert.CertificateEncodingException;
26 import java.security.cert.X509Certificate;
27 import java.util.regex.Pattern;
28
29 import org.onap.aaf.cadi.GetCred;
30 import org.onap.aaf.cadi.taf.basic.BasicHttpTaf;
31
32 public class X509Principal extends BearerPrincipal implements GetCred {
33         private static final Pattern pattern = Pattern.compile("[a-zA-Z0-9]*\\@[a-zA-Z0-9.]*");
34         private final X509Certificate cert;
35         private final String name;
36         private byte[] content;
37         private BasicHttpTaf bht;  
38
39         public X509Principal(String identity, X509Certificate cert) {
40                 name = identity;
41                 content = null;
42                 this.cert = cert;
43         }
44
45         public X509Principal(String identity, X509Certificate cert, byte[] content, BasicHttpTaf bht) {
46                 name = identity;
47                 this.content = content;
48                 this.cert = cert;
49                 this.bht = bht;
50         }
51
52         public X509Principal(X509Certificate cert, byte[] content, BasicHttpTaf bht) throws IOException {
53                 this.content=content;
54                 this.cert = cert;
55                 String _name = null;
56                 String subj = cert.getSubjectDN().getName();
57                 int cn = subj.indexOf("OU=");
58                 if(cn>=0) {
59                         cn+=3;
60                         int space = subj.indexOf(',',cn);
61                         if(space>=0) {
62                                 String id = subj.substring(cn, space);
63                                 if(pattern.matcher(id).matches()) {
64                                         _name = id;
65                                 }
66                         }
67                 }
68                 if(_name==null) {
69                         throw new IOException("X509 does not have Identity as CN");
70                 }
71                 name = _name;
72                 this.bht = bht;
73         }
74         
75         public String getAsHeader() throws IOException {
76                 try {
77                         if(content==null) {
78                                 content=cert.getEncoded();
79                         }
80                 } catch (CertificateEncodingException e) {
81                         throw new IOException(e);
82                 }
83                 return "X509 " + content;
84         }
85         
86         public String toString() {
87                 return "X509 Authentication for " + name;
88         }
89
90
91         public byte[] getCred() {
92                 try {
93                         return content==null?(content=cert.getEncoded()):content;
94                 } catch (CertificateEncodingException e) {
95                         return null;
96                 }
97         }
98
99         public String getName() {
100                 return name;
101         }
102
103         @Override
104         public String tag() {
105                 return "x509";
106         }
107
108         public BasicHttpTaf getBasicHttpTaf() {
109                 return bht;
110         }
111
112 }