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.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.util.Date;
30 import org.onap.aaf.cadi.BasicCred;
31 import org.onap.aaf.cadi.GetCred;
32 import org.onap.aaf.cadi.Symm;
34 public class BasicPrincipal extends BearerPrincipal implements GetCred {
35 private static byte[] basic = "Basic ".getBytes();
37 private String name = null;
38 private String shortName = null;
39 private String domain;
40 private byte[] cred = null;
44 public BasicPrincipal(String content,String defaultDomain) throws IOException {
45 created = System.currentTimeMillis();
46 ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());
47 // Read past "Basic ", ensuring it starts with it.
48 for (int i=0;i<basic.length;++i) {
49 if (bis.read()!=basic[i]) {
55 BasicOS bos = new BasicOS(content.length());
56 Symm.base64.decode(bis,bos); // note: writes directly to name until ':'
57 if (name==null) throw new IOException("Invalid Coding");
58 else cred = bos.toCred();
60 if ((at=name.indexOf('@'))>0) {
61 domain=name.substring(at+1);
62 shortName=name.substring(0, at);
66 name = name + '@' + defaultDomain;
70 public BasicPrincipal(BasicCred bc, String domain) {
76 private class BasicOS extends OutputStream {
77 private boolean first = true;
78 private ByteArrayOutputStream baos;
80 public BasicOS(int size) {
81 baos = new ByteArrayOutputStream(size);
85 public void write(int b) throws IOException {
86 if (b==':' && first) {
88 name = new String(baos.toByteArray());
95 private byte[] toCred() {
96 return baos.toByteArray();
100 public String getName() {
104 public String getShortName() {
108 public String getDomain() {
112 public byte[] getCred() {
116 public long created() {
120 public String toString() {
121 return "Basic Authorization for " + name + " evaluated on " + new Date(created).toString();
125 public String tag() {
130 public String personalName() {
131 return name; // personalName not available with Basic Auth