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 byte[] cred = null;
43 public BasicPrincipal(String content,String domain) throws IOException {
44 created = System.currentTimeMillis();
45 ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());
46 // Read past "Basic ", ensuring it starts with it.
47 for(int i=0;i<basic.length;++i) {
48 if(bis.read()!=basic[i]) {
54 BasicOS bos = new BasicOS(content.length());
55 Symm.base64.decode(bis,bos); // note: writes directly to name until ':'
56 if(name==null) throw new IOException("Invalid Coding");
57 else cred = bos.toCred();
59 if((at=name.indexOf('@'))>0) {
60 domain=name.substring(at+1);
61 shortName=name.substring(0, at);
64 name = name + '@' + domain;
68 public BasicPrincipal(BasicCred bc, String domain) {
73 private class BasicOS extends OutputStream {
74 private boolean first = true;
75 private ByteArrayOutputStream baos;
77 public BasicOS(int size) {
78 baos = new ByteArrayOutputStream(size);
82 public void write(int b) throws IOException {
85 name = new String(baos.toByteArray());
92 private byte[] toCred() {
93 return baos.toByteArray();
97 public String getName() {
101 public String getShortName() {
105 public byte[] getCred() {
109 public long created() {
113 public String toString() {
114 return "Basic Authorization for " + name + " evaluated on " + new Date(created).toString();
118 public String tag() {
123 public String personalName() {
124 return name; // personalName not available with Basic Auth