+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.principal;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.IOException;\r
-import java.io.OutputStream;\r
-import java.util.Date;\r
-\r
-import org.onap.aaf.cadi.BasicCred;\r
-import org.onap.aaf.cadi.GetCred;\r
-import org.onap.aaf.cadi.Symm;\r
-\r
-public class BasicPrincipal extends BearerPrincipal implements GetCred {\r
- private static byte[] basic = "Basic ".getBytes();\r
-\r
- private String name = null;\r
- private String shortName = null;\r
- private byte[] cred = null;\r
- \r
- private long created;\r
-\r
- public BasicPrincipal(String content,String domain) throws IOException {\r
- created = System.currentTimeMillis();\r
- ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());\r
- // Read past "Basic ", ensuring it starts with it.\r
- for(int i=0;i<basic.length;++i) {\r
- if(bis.read()!=basic[i]) {\r
- name=content;\r
- cred = null;\r
- return;\r
- }\r
- }\r
- BasicOS bos = new BasicOS(content.length());\r
- Symm.base64.decode(bis,bos); // note: writes directly to name until ':'\r
- if(name==null) throw new IOException("Invalid Coding");\r
- else cred = bos.toCred();\r
- int at;\r
- if((at=name.indexOf('@'))>0) {\r
- domain=name.substring(at+1);\r
- shortName=name.substring(0, at);\r
- } else {\r
- shortName = name;\r
- name = name + '@' + domain;\r
- }\r
- }\r
- \r
- public BasicPrincipal(BasicCred bc, String domain) {\r
- name = bc.getUser();\r
- cred = bc.getCred();\r
- }\r
-\r
- private class BasicOS extends OutputStream {\r
- private boolean first = true;\r
- private ByteArrayOutputStream baos;\r
- \r
- public BasicOS(int size) {\r
- baos = new ByteArrayOutputStream(size);\r
- }\r
-\r
- @Override\r
- public void write(int b) throws IOException {\r
- if(b==':' && first) {\r
- first = false;\r
- name = new String(baos.toByteArray());\r
- baos.reset(); // \r
- } else {\r
- baos.write(b);\r
- }\r
- }\r
- \r
- private byte[] toCred() {\r
- return baos.toByteArray();\r
- }\r
- }\r
- \r
- public String getName() {\r
- return name;\r
- }\r
- \r
- public String getShortName() {\r
- return shortName;\r
- }\r
- \r
- public byte[] getCred() {\r
- return cred;\r
- }\r
- \r
- public long created() {\r
- return created;\r
- }\r
-\r
- public String toString() {\r
- return "Basic Authorization for " + name + " evaluated on " + new Date(created).toString();\r
- }\r
-}\r