746e67d8a82424a85356f404841aa3cdc68ba935
[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 String domain;
40     private byte[] cred = null;
41     private long created;
42
43
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]) {
50                 name=content;
51                 cred = null;
52                 return;
53             }
54         }
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();
59         int at;
60         if ((at=name.indexOf('@'))>0) {
61             domain=name.substring(at+1);
62             shortName=name.substring(0, at);
63         } else {
64             shortName = name;
65             domain=defaultDomain;
66             name = name + '@' + defaultDomain;
67         }
68     }
69     
70     public BasicPrincipal(BasicCred bc, String domain) {
71         name = bc.getUser();
72         cred = bc.getCred();
73         this.domain = domain;
74     }
75
76     private class BasicOS extends OutputStream {
77         private boolean first = true;
78         private ByteArrayOutputStream baos;
79         
80         public BasicOS(int size) {
81             baos = new ByteArrayOutputStream(size);
82         }
83
84         @Override
85         public void write(int b) throws IOException {
86             if (b==':' && first) {
87                 first = false;
88                 name = new String(baos.toByteArray());
89                 baos.reset(); // 
90             } else {
91                 baos.write(b);
92             }
93         }
94         
95         private byte[] toCred() {
96             return baos.toByteArray();
97         }
98     }
99     
100     public String getName() {
101         return name;
102     }
103     
104     public String getShortName() {
105         return shortName;
106     }
107     
108     public String getDomain() {
109         return domain;
110     }
111     
112     public byte[] getCred() {
113         return cred;
114     }
115     
116     public long created() {
117         return created;
118     }
119
120     public String toString() {
121         return "Basic Authorization for " + name + " evaluated on " + new Date(created).toString();
122     }
123
124     @Override
125     public String tag() {
126         return "BAth";
127     }
128
129     @Override
130     public String personalName() {
131         return name;  // personalName not available with Basic Auth
132     }
133 }