Fix Bath config issue
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / principal / BasicPrincipal.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.principal;
23
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;
29
30 import org.onap.aaf.cadi.BasicCred;
31 import org.onap.aaf.cadi.GetCred;
32 import org.onap.aaf.cadi.Symm;
33
34 public class BasicPrincipal extends BearerPrincipal implements GetCred {
35         private static byte[] basic = "Basic ".getBytes();
36
37         private String name = null;
38         private String shortName = null;
39         private byte[] cred = null;
40         
41         private long created;
42
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]) {
49                                 name=content;
50                                 cred = null;
51                                 return;
52                         }
53                 }
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();
58                 int at;
59                 if((at=name.indexOf('@'))>0) {
60                         domain=name.substring(at+1);
61                         shortName=name.substring(0, at);
62                 } else {
63                         shortName = name;
64                         name = name + '@' + domain;
65                 }
66         }
67         
68         public BasicPrincipal(BasicCred bc, String domain) {
69                 name = bc.getUser();
70                 cred = bc.getCred();
71         }
72
73         private class BasicOS extends OutputStream {
74                 private boolean first = true;
75                 private ByteArrayOutputStream baos;
76                 
77                 public BasicOS(int size) {
78                         baos = new ByteArrayOutputStream(size);
79                 }
80
81                 @Override
82                 public void write(int b) throws IOException {
83                         if(b==':' && first) {
84                                 first = false;
85                                 name = new String(baos.toByteArray());
86                                 baos.reset(); // 
87                         } else {
88                                 baos.write(b);
89                         }
90                 }
91                 
92                 private byte[] toCred() {
93                         return baos.toByteArray();
94                 }
95         }
96         
97         public String getName() {
98                 return name;
99         }
100         
101         public String getShortName() {
102                 return shortName;
103         }
104         
105         public byte[] getCred() {
106                 return cred;
107         }
108         
109         public long created() {
110                 return created;
111         }
112
113         public String toString() {
114                 return "Basic Authorization for " + name + " evaluated on " + new Date(created).toString();
115         }
116
117         @Override
118         public String tag() {
119                 return "BAth";
120         }
121
122         @Override
123         public String personalName() {
124                 return name;  // personalName not available with Basic Auth
125         }
126 }