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.ccsdk.apps.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.ccsdk.apps.cadi.lur.LocalPermission;
33 * Class to hold info from the User Perspective.
37 public final class User<PERM extends Permission> {
38 private static final Map<String,Permission> NULL_MAP = new HashMap<>();
41 public Principal principal;
42 Map<String, Permission> perms;
44 private final long interval;
47 // Note: This should only be used for Local RBAC (in memory)
48 public User(Principal principal) {
49 this.principal = principal;
50 name = principal.getName();
52 permExpires = Long.MAX_VALUE; // Never. Well, until 64 bits of millis since 1970 expires...
57 public User(String name, byte[] cred) {
58 this.principal = null;
62 permExpires = Long.MAX_VALUE; // Never. Well, until 64 bits of millis since 1970 expires...
67 public User(Principal principal, long expireInterval) {
68 this.principal = principal;
69 this.name = principal.getName();
71 expireInterval = Math.max(expireInterval, 0); // avoid < 1
72 interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));
78 public User(String name, byte[] cred, long expireInterval) {
79 this.principal = null;
83 expireInterval = Math.max(expireInterval, 0); // avoid < 1
84 interval = Math.max(AbsUserCache.MIN_INTERVAL,Math.min(expireInterval,AbsUserCache.MAX_INTERVAL));
89 public void renewPerm() {
90 permExpires = System.currentTimeMillis()+interval;
93 public long permExpires() {
97 public boolean permExpired() {
98 return System.currentTimeMillis() > permExpires;
101 public boolean noPerms() {
102 return perms==null || perms==NULL_MAP || perms.values().size()==0;
105 public synchronized void setNoPerms() {
110 public boolean permsUnloaded() {
111 return perms==null || perms==NULL_MAP;
114 public synchronized void incCount() {
118 public synchronized void resetCount() {
122 public Map<String,Permission> newMap() {
123 return new ConcurrentHashMap<>();
126 public void add(LocalPermission permission) {
127 if (perms==NULL_MAP) {
130 perms.put(permission.getKey(),permission);
133 public void add(Map<String, Permission> newMap, PERM permission) {
134 newMap.put(permission.getKey(),permission);
137 public synchronized void setMap(Map<String, Permission> newMap) {
142 public boolean contains(Permission perm) {
143 for (Permission p : perms.values()) {
144 if (p.match(perm)) return true;
149 public void copyPermsTo(List<Permission> sink) {
150 sink.addAll(perms.values());
153 public String toString() {
154 StringBuilder sb = new StringBuilder();
155 sb.append(principal.getName());
157 boolean first = true;
158 synchronized(perms) {
159 for (Permission gp : perms.values()) {
166 sb.append(gp.getKey());
169 return sb.toString();
172 public byte[] getCred() {