1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.cadi.principal;
\r
25 import java.io.ByteArrayInputStream;
\r
26 import java.io.ByteArrayOutputStream;
\r
27 import java.io.IOException;
\r
28 import java.io.OutputStream;
\r
29 import java.util.Date;
\r
31 import org.onap.aaf.cadi.BasicCred;
\r
32 import org.onap.aaf.cadi.GetCred;
\r
33 import org.onap.aaf.cadi.Symm;
\r
35 public class BasicPrincipal extends BearerPrincipal implements GetCred {
\r
36 private static byte[] basic = "Basic ".getBytes();
\r
38 private String name = null;
\r
39 private String shortName = null;
\r
40 private byte[] cred = null;
\r
42 private long created;
\r
44 public BasicPrincipal(String content,String domain) throws IOException {
\r
45 created = System.currentTimeMillis();
\r
46 ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());
\r
47 // Read past "Basic ", ensuring it starts with it.
\r
48 for(int i=0;i<basic.length;++i) {
\r
49 if(bis.read()!=basic[i]) {
\r
55 BasicOS bos = new BasicOS(content.length());
\r
56 Symm.base64.decode(bis,bos); // note: writes directly to name until ':'
\r
57 if(name==null) throw new IOException("Invalid Coding");
\r
58 else cred = bos.toCred();
\r
60 if((at=name.indexOf('@'))>0) {
\r
61 domain=name.substring(at+1);
\r
62 shortName=name.substring(0, at);
\r
65 name = name + '@' + domain;
\r
69 public BasicPrincipal(BasicCred bc, String domain) {
\r
70 name = bc.getUser();
\r
71 cred = bc.getCred();
\r
74 private class BasicOS extends OutputStream {
\r
75 private boolean first = true;
\r
76 private ByteArrayOutputStream baos;
\r
78 public BasicOS(int size) {
\r
79 baos = new ByteArrayOutputStream(size);
\r
83 public void write(int b) throws IOException {
\r
84 if(b==':' && first) {
\r
86 name = new String(baos.toByteArray());
\r
93 private byte[] toCred() {
\r
94 return baos.toByteArray();
\r
98 public String getName() {
\r
102 public String getShortName() {
\r
106 public byte[] getCred() {
\r
110 public long created() {
\r
114 public String toString() {
\r
115 return "Basic Authorization for " + name + " evaluated on " + new Date(created).toString();
\r