Collection syntax change because of Sonar
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / oauth / TokenPerm.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.oauth;
23
24 import java.io.Reader;
25 import java.io.StringReader;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.onap.aaf.cadi.aaf.AAFPermission;
31 import org.onap.aaf.cadi.persist.Persist;
32 import org.onap.aaf.cadi.persist.Persisting;
33 import org.onap.aaf.misc.env.APIException;
34 import org.onap.aaf.misc.rosetta.InJson;
35 import org.onap.aaf.misc.rosetta.Parse;
36 import org.onap.aaf.misc.rosetta.ParseException;
37 import org.onap.aaf.misc.rosetta.Parsed;
38 import org.onap.aaf.misc.rosetta.InJson.State;
39 import org.onap.aaf.misc.rosetta.env.RosettaDF;
40
41 import aaf.v2_0.Perms;
42 import aafoauth.v2_0.Introspect;
43
44 public class TokenPerm extends Persisting<Introspect>{
45         private static final List<AAFPermission> NULL_PERMS = new ArrayList<>();
46         private Introspect introspect;
47         private List<AAFPermission> perms;
48         private String scopes;
49         public TokenPerm(Persist<Introspect,?> p, RosettaDF<Perms> permsDF, Introspect ti, byte[] hash, Path path) throws APIException {
50                 super(p,ti,ti.getExp(),hash,path); // ti.getExp() is seconds after Jan 1, 1970 )
51                 this.introspect = ti;
52                 if(ti.getContent()==null || ti.getContent().length()==0) {
53                         perms = NULL_PERMS;
54                 } else {
55                         LoadPermissions lp;
56                         try {
57                                 lp = new LoadPermissions(new StringReader(ti.getContent()));
58                                 perms = lp.perms;
59                         } catch (ParseException e) {
60                                 throw new APIException("Error parsing Content",e);
61                         }
62                 }
63                 scopes = ti.getScope();
64         }
65         
66         public List<AAFPermission> perms() {
67                 return perms;
68         }
69         
70         public String getClientId() {
71                 return introspect.getClientId();
72         }
73         
74         public String getUsername() {
75                 return introspect.getUsername();
76         }
77         
78         public String getToken() {
79                 return introspect.getAccessToken();
80         }
81         
82         public synchronized String getScopes() {
83                 return scopes;
84         }
85
86         public Introspect getIntrospect() {
87                 return introspect;
88         }
89         
90         // Direct Parse Perms into List
91         public static class LoadPermissions {
92                 public List<AAFPermission> perms;
93
94                 public LoadPermissions(Reader r) throws ParseException {
95                         PermInfo pi = new PermInfo();
96                         InJson ij = new InJson();
97                         Parsed<State> pd =  ij.newParsed();
98                         boolean inPerms = false, inPerm = false;
99                         while((pd = ij.parse(r,pd.reuse())).valid()) {
100                                 switch(pd.event) {
101                                         case Parse.START_DOC:
102                                                 perms = new ArrayList<>();
103                                                 break;
104                                         case Parse.START_ARRAY:
105                                                 inPerms = "perm".equals(pd.name);
106                                                 break;
107                                         case '{':
108                                                 if(inPerms) {
109                                                         inPerm=true;
110                                                         pi.clear();
111                                                 }
112                                                 break;
113                                         case ',':
114                                                 if(inPerm) {
115                                                         pi.eval(pd);
116                                                 }
117                                                 break;
118                                         case '}':
119                                                 if(inPerms) {
120                                                         if(inPerm) {
121                                                                 pi.eval(pd);
122                                                                 AAFPermission perm = pi.create();
123                                                                 if(perm!=null) {
124                                                                         perms.add(perm);
125                                                                 }
126                                                         }
127                                                         inPerm=false;
128                                                 }
129                                                 break;
130                                         case Parse.END_ARRAY:
131                                                 if(inPerms) {
132                                                         inPerms=false;
133                                                 }
134                                                 break;
135                                         case Parse.END_DOC:
136                                                 break;
137                                 }
138                         }
139                 }
140         }
141         
142         // Gathering object for parsing objects, then creating AAF Permission
143         private static class PermInfo {
144                 public String type,instance,action;
145                 public void clear() {
146                         type=instance=action=null;
147                 }
148                 public void eval(Parsed<State> pd) {
149                         if(pd.hasName()) {
150                                 switch(pd.name) {
151                                         case "type":
152                                                 type=pd.sb.toString();
153                                                 break;
154                                         case "instance":
155                                                 instance=pd.sb.toString();
156                                                 break;
157                                         case "action":
158                                                 action=pd.sb.toString();
159                                                 break;
160                                 }
161                         }
162                 }
163                 public AAFPermission create() {
164                         if(type!=null && instance!=null && action !=null) {
165                                 return new AAFPermission(type, instance, action);
166                         } else {
167                                 return null;
168                         }
169                 }
170         }
171 }