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;
24 import java.security.Principal;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.concurrent.ConcurrentHashMap;
30 import org.onap.aaf.cadi.lur.LocalPermission;
33 * Class to hold info from the User Perspective.
38 public final class User<PERM extends Permission> {
39 private static final Map<String,Permission> NULL_MAP = new HashMap<String,Permission>();
42 public Principal principal;
43 Map<String, Permission> perms ;
45 private final long interval;
48 // Note: This should only be used for Local RBAC (in memory)
49 public User(Principal principal) {
50 this.principal = principal;
51 name = principal.getName();
53 permExpires = Long.MAX_VALUE; // Never. Well, until 64 bits of millis since 1970 expires...
58 public User(String name, byte[] cred) {
59 this.principal = null;
63 permExpires = Long.MAX_VALUE; // Never. Well, until 64 bits of millis since 1970 expires...
68 public User(Principal principal, long expireInterval) {
69 this.principal = principal;
70 this.name = principal.getName();
72 expireInterval = Math.max(expireInterval, 0); // avoid < 1
73 interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));
79 public User(String name, byte[] cred, long expireInterval) {
80 this.principal = null;
84 expireInterval = Math.max(expireInterval, 0); // avoid < 1
85 interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));
90 public void renewPerm() {
91 permExpires = System.currentTimeMillis()+interval;
94 public long permExpires() {
98 public boolean permExpired() {
99 return System.currentTimeMillis() > permExpires;
102 public boolean noPerms() {
103 return perms==null || perms==NULL_MAP || perms.values().size()==0;
106 public synchronized void setNoPerms() {
111 public boolean permsUnloaded() {
112 return perms==null || perms==NULL_MAP;
115 public synchronized void incCount() {
119 public synchronized void resetCount() {
123 public Map<String,Permission> newMap() {
124 return new ConcurrentHashMap<String,Permission>();
127 public void add(LocalPermission permission) {
128 if(perms==NULL_MAP) {
131 perms.put(permission.getKey(),permission);
134 public void add(Map<String, Permission> newMap, PERM permission) {
135 newMap.put(permission.getKey(),permission);
138 public synchronized void setMap(Map<String, Permission> newMap) {
143 public boolean contains(Permission perm) {
144 for (Permission p : perms.values()) {
145 if (p.match(perm)) return true;
150 public void copyPermsTo(List<Permission> sink) {
151 sink.addAll(perms.values());
154 public String toString() {
155 StringBuilder sb = new StringBuilder();
156 sb.append(principal.getName());
158 boolean first = true;
159 synchronized(perms) {
160 for(Permission gp : perms.values()) {
167 sb.append(gp.getKey());
170 return sb.toString();
173 public byte[] getCred() {