2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.oauth;
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;
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;
41 import aaf.v2_0.Perms;
42 import aafoauth.v2_0.Introspect;
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 )
52 if(ti.getContent()==null || ti.getContent().length()==0) {
57 lp = new LoadPermissions(new StringReader(ti.getContent()));
59 } catch (ParseException e) {
60 throw new APIException("Error parsing Content",e);
63 scopes = ti.getScope();
66 public List<AAFPermission> perms() {
70 public String getClientId() {
71 return introspect.getClientId();
74 public String getUsername() {
75 return introspect.getUsername();
78 public String getToken() {
79 return introspect.getAccessToken();
82 public synchronized String getScopes() {
86 public Introspect getIntrospect() {
90 // Direct Parse Perms into List
91 public static class LoadPermissions {
92 public List<AAFPermission> perms;
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()) {
101 case Parse.START_DOC:
102 perms = new ArrayList<>();
104 case Parse.START_ARRAY:
105 inPerms = "perm".equals(pd.name);
122 AAFPermission perm = pi.create();
130 case Parse.END_ARRAY:
142 // Gathering object for parsing objects, then creating AAF Permission
143 private static class PermInfo {
144 public String ns,type,instance,action;
145 public void clear() {
146 ns=type=instance=action=null;
148 public void eval(Parsed<State> pd) {
155 type=pd.sb.toString();
158 instance=pd.sb.toString();
161 action=pd.sb.toString();
166 public AAFPermission create() {
167 if(type!=null && instance!=null && action !=null) {
168 return new AAFPermission(ns,type, instance, action);