a6a9bb76afe0d1f9d67c57f9a44b67fa0a7e6011
[aaf/cadi.git] / aaf / src / main / java / com / att / cadi / aaf / AAFTransmutate.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.aaf;\r
25 \r
26 import java.security.Principal;\r
27 import java.util.regex.Pattern;\r
28 \r
29 import com.att.cadi.Transmutate;\r
30 import com.att.cadi.lur.ConfigPrincipal;\r
31 import com.att.cadi.principal.BasicPrincipal;\r
32 import com.att.cadi.principal.CSPPrincipal_T;\r
33 \r
34 /**\r
35  * AAFTransmutate\r
36  * \r
37  * Each System determines the mechanisms for which one Principal is transmutated to another, such as whether it is created\r
38  * independently, etc.\r
39  * \r
40  * For AAF, the only important thing is that these are valid ATTUID/mechIDs, to avoid unnecessary user hits\r
41  * \r
42  * attUIDs look like ab1234 or AB1234 or AZ123a\r
43  * mechids look like m12345\r
44  * \r
45  *\r
46  */\r
47 public final class AAFTransmutate implements Transmutate<Principal> {\r
48         private Pattern pattern = Pattern.compile("[a-zA-Z]\\w\\d\\d\\d\\w");\r
49 \r
50         public Principal mutate(Principal p) {\r
51                 // Accept these three internal kinds of Principals\r
52                 if(p instanceof CSPPrincipal_T \r
53                         || p instanceof BasicPrincipal\r
54                         || p instanceof ConfigPrincipal) { \r
55                         return p;\r
56                 } else { \r
57                         \r
58                         final String name = p.getName();\r
59                         final int idx = name.indexOf('@');\r
60                         String shortName;\r
61                         if(idx>0) { // strip off any domain\r
62                                 shortName = name.substring(0,idx); \r
63                         } else {\r
64                                 shortName = name;\r
65                         }\r
66 \r
67                         // Check for ATTUID specs before creating CSP_T\r
68                         return pattern.matcher(shortName).matches()?\r
69                                 new CSP_T(name): // Note: use REAL name, short name for CSP_T\r
70                                 null;\r
71                 }\r
72         }\r
73 \r
74         /**\r
75          * Essential Principal reflecting CSP Principal\r
76          * \r
77          *\r
78          */\r
79         private final class CSP_T implements CSPPrincipal_T {\r
80                 private String name;\r
81                 public CSP_T(String name) {\r
82                         this.name = name;\r
83                 }\r
84                 public String getName() {\r
85                         return name;\r
86                 }\r
87         }\r
88 }\r