Update AAF Version 1.0.0
[aaf/cadi.git] / core / src / main / java / org / onap / aaf / cadi / principal / BasicPrincipal.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\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
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \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
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package org.onap.aaf.cadi.principal;\r
24 \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
30 \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
34 \r
35 public class BasicPrincipal extends BearerPrincipal implements GetCred {\r
36         private static byte[] basic = "Basic ".getBytes();\r
37 \r
38         private String name = null;\r
39         private String shortName = null;\r
40         private byte[] cred = null;\r
41         \r
42         private long created;\r
43 \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
50                                 name=content;\r
51                                 cred = null;\r
52                                 return;\r
53                         }\r
54                 }\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
59                 int at;\r
60                 if((at=name.indexOf('@'))>0) {\r
61                         domain=name.substring(at+1);\r
62                         shortName=name.substring(0, at);\r
63                 } else {\r
64                         shortName = name;\r
65                         name = name + '@' + domain;\r
66                 }\r
67         }\r
68         \r
69         public BasicPrincipal(BasicCred bc, String domain) {\r
70                 name = bc.getUser();\r
71                 cred = bc.getCred();\r
72         }\r
73 \r
74         private class BasicOS extends OutputStream {\r
75                 private boolean first = true;\r
76                 private ByteArrayOutputStream baos;\r
77                 \r
78                 public BasicOS(int size) {\r
79                         baos = new ByteArrayOutputStream(size);\r
80                 }\r
81 \r
82                 @Override\r
83                 public void write(int b) throws IOException {\r
84                         if(b==':' && first) {\r
85                                 first = false;\r
86                                 name = new String(baos.toByteArray());\r
87                                 baos.reset(); // \r
88                         } else {\r
89                                 baos.write(b);\r
90                         }\r
91                 }\r
92                 \r
93                 private byte[] toCred() {\r
94                         return baos.toByteArray();\r
95                 }\r
96         }\r
97         \r
98         public String getName() {\r
99                 return name;\r
100         }\r
101         \r
102         public String getShortName() {\r
103                 return shortName;\r
104         }\r
105         \r
106         public byte[] getCred() {\r
107                 return cred;\r
108         }\r
109         \r
110         public long created() {\r
111                 return created;\r
112         }\r
113 \r
114         public String toString() {\r
115                 return "Basic Authorization for " + name + " evaluated on " + new Date(created).toString();\r
116         }\r
117 }\r