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