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