1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * Copyright © 2017 Amdocs
\r
7 * * ===========================================================================
\r
8 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * * you may not use this file except in compliance with the License.
\r
10 * * You may obtain a copy of the License at
\r
12 * * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * * Unless required by applicable law or agreed to in writing, software
\r
15 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * * See the License for the specific language governing permissions and
\r
18 * * limitations under the License.
\r
19 * * ============LICENSE_END====================================================
\r
21 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
23 ******************************************************************************/
\r
24 package com.att.cadi.principal;
\r
26 import java.io.ByteArrayInputStream;
\r
27 import java.io.ByteArrayOutputStream;
\r
28 import java.io.IOException;
\r
29 import java.io.OutputStream;
\r
30 import java.util.Date;
\r
32 import com.att.cadi.BasicCred;
\r
33 import com.att.cadi.GetCred;
\r
34 import com.att.cadi.Symm;
\r
36 public class BasicPrincipal extends BearerPrincipal implements GetCred {
\r
37 private static byte[] basic = "Basic ".getBytes();
\r
39 private String name = null;
\r
40 private String shortName = null;
\r
41 private byte[] cred = null;
\r
43 private long created;
\r
45 public BasicPrincipal(String content,String domain) throws IOException {
\r
46 created = System.currentTimeMillis();
\r
47 ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());
\r
48 // Read past "Basic ", ensuring it starts with it.
\r
49 for(int i=0;i<basic.length;++i) {
\r
50 if(bis.read()!=basic[i]) {
\r
56 BasicOS bos = new BasicOS(content.length());
\r
57 Symm.base64.decode(bis,bos); // note: writes directly to name until ':'
\r
58 if(name==null) throw new IOException("Invalid Coding");
\r
59 else cred = bos.toCred();
\r
61 if((at=name.indexOf('@'))>0) {
\r
62 domain=name.substring(at+1);
\r
63 shortName=name.substring(0, at);
\r
66 name = name + '@' + domain;
\r
70 public BasicPrincipal(BasicCred bc, String domain) {
\r
71 name = bc.getUser();
\r
72 cred = bc.getCred();
\r
75 private class BasicOS extends OutputStream {
\r
76 private boolean first = true;
\r
77 private ByteArrayOutputStream baos;
\r
79 public BasicOS(int size) {
\r
80 baos = new ByteArrayOutputStream(size);
\r
84 public void write(int b) throws IOException {
\r
85 if(b==':' && first) {
\r
87 name = new String(baos.toByteArray());
\r
94 private byte[] toCred() {
\r
95 return baos.toByteArray();
\r
99 public String getName() {
\r
103 public String getShortName() {
\r
107 public byte[] getCred() {
\r
111 public long created() {
\r
115 public String toString() {
\r
116 return "Basic Authorization for " + name + " evaluated on " + new Date(created).toString();
\r