2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.principal;
24 import java.io.IOException;
25 import java.security.cert.CertificateEncodingException;
26 import java.security.cert.X509Certificate;
27 import java.util.regex.Pattern;
29 import org.onap.aaf.cadi.GetCred;
30 import org.onap.aaf.cadi.taf.basic.BasicHttpTaf;
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;
39 public X509Principal(String identity, X509Certificate cert) {
45 public X509Principal(String identity, X509Certificate cert, byte[] content, BasicHttpTaf bht) {
47 this.content = content;
52 public X509Principal(X509Certificate cert, byte[] content, BasicHttpTaf bht) throws IOException {
56 String subj = cert.getSubjectDN().getName();
57 int cn = subj.indexOf("OU=");
60 int space = subj.indexOf(',',cn);
62 String id = subj.substring(cn, space);
63 if (pattern.matcher(id).matches()) {
69 throw new IOException("X509 does not have Identity as CN");
75 public String getAsHeader() throws IOException {
78 content=cert.getEncoded();
80 } catch (CertificateEncodingException e) {
81 throw new IOException(e);
83 return "X509 " + content;
86 public String toString() {
87 return "X509 Authentication for " + name;
91 public byte[] getCred() {
93 return content==null?(content=cert.getEncoded()):content;
94 } catch (CertificateEncodingException e) {
99 public String getName() {
104 public String tag() {
108 public BasicHttpTaf getBasicHttpTaf() {