73093099e4a27c341d24cc92ac6bacacdc1d357e
[aaf/authz.git] / auth / auth-core / src / main / java / org / onap / aaf / auth / org / Organization.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.auth.org;
23
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.GregorianCalendar;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
31 import org.onap.aaf.auth.env.AuthzTrans;
32
33 /**
34  * Organization
35  *
36  * There is Organizational specific information required which we have extracted to a plugin
37  *
38  * It supports using Company Specific User Directory lookups, as well as supporting an
39  * Approval/Validation Process to simplify control of Roles and Permissions for large organizations
40  * in lieu of direct manipulation by a set of Admins.
41  *
42  * @author Jonathan
43  *
44  */
45 public interface Organization {
46     public static final String N_A = "n/a";
47
48     public interface Identity {
49         public String id();
50         public String fullID() throws OrganizationException; // Fully Qualified ID (includes Domain of Organization)
51         public String type();                 // Must be one of "IdentityTypes", see below
52         public Identity responsibleTo() throws OrganizationException;         // Chain of Command, or Application ID Sponsor
53         public List<String> delegate();         // Someone who has authority to act on behalf of Identity
54         public String email();
55         public String fullName();
56         public String firstName();
57         /**
58          * If Responsible entity, then String returned is "null"  meaning "no Objection".
59          * If String exists, it is the Policy objection text setup by the entity.
60          * @return
61          */
62         public String mayOwn();            // Is id passed belong to a person suitable to be Responsible for content Management
63         public boolean isFound();                // Is Identity found in Identity stores
64         public boolean isPerson();                // Whether a Person or a Machine (App)
65         public Organization org();                 // Organization of Identity
66
67
68         public static String mixedCase(String in) {
69             StringBuilder sb = new StringBuilder();
70             for(int i=0;i<in.length();++i) {
71                 if(i==0) {
72                     sb.append(Character.toUpperCase(in.charAt(i)));
73                 } else {
74                     sb.append(Character.toLowerCase(in.charAt(i)));
75                 }
76             }
77             return sb.toString();
78         }
79     }
80
81
82     /**
83      * Name of Organization, suitable for Logging
84      * @return
85      */
86     public String getName();
87
88     /**
89      * Realm, for use in distinguishing IDs from different systems/Companies
90      * @return
91      */
92     public String getRealm();
93
94     public boolean supportsRealm(String user);
95
96     public void addSupportedRealm(String r);
97
98     /**
99      * If Supported, returns Realm, ex: org.onap
100      * ELSE returns null
101      * 
102      * @param user
103      * @return
104      */
105     public String supportedDomain(String user);
106
107         public String getDomain();
108
109     /**
110      * Get Identity information based on userID
111      *
112      * @param id
113      * @return
114      */
115     public Identity getIdentity(AuthzTrans trans, String id) throws OrganizationException;
116
117     /**
118      * Is Revoked
119      *
120      * Deletion of an Identity that has been removed from an Organization can be dangerous.  Mistakes may have been made
121      * in the Organization side, a Feed might be corrupted, an API might not be quite right.
122      *
123      * The implementation of this method can use a double check of some sort, such as comparison of missing ID in Organization
124      * feed with a "Deleted ID" feed.
125      *
126      */
127     public boolean isRevoked(AuthzTrans trans, String id);
128
129
130     /**
131      * Does the ID pass Organization Standards
132      *
133      * Return a Blank (empty) String if empty, otherwise, return a "\n" separated list of
134      * reasons why it fails
135      *
136      * @param id
137      * @return
138      */
139     public String isValidID(AuthzTrans trans, String id);
140
141     /**
142      * Return a Blank (empty) String if empty, otherwise, return a "\n" separated list of
143      * reasons why it fails
144      *
145      *  Identity is passed in to allow policies regarding passwords that are the same as user ID
146      *
147      *  any entries for "prev" imply a reset
148      *
149      * @param id
150      * @param password
151      * @return
152      */
153     public String isValidPassword(final AuthzTrans trans, final String id, final String password, final String ... prev);
154
155     /**
156      * Return a list of Strings denoting Organization Password Rules, suitable for posting on a WebPage with <p>
157      */
158     public String[] getPasswordRules();
159
160     /**
161      *
162      * @param id
163      * @return
164      */
165     public boolean isValidCred(final AuthzTrans trans, final String id);
166
167     /**
168      * If response is Null, then it is valid.  Otherwise, the Organization specific reason is returned.
169      *
170      * @param trans
171      * @param policy
172      * @param executor
173      * @param vars
174      * @return
175      * @throws OrganizationException
176      */
177     public String validate(AuthzTrans trans, Policy policy, Executor executor, String ... vars) throws OrganizationException;
178
179     /**
180      * Does your Company distinguish essential permission structures by kind of Identity?
181      * i.e. Employee, Contractor, Vendor
182      * @return
183      */
184     public Set<String> getIdentityTypes();
185
186     public enum Notify {
187         Approval(1),
188         PasswordExpiration(2),
189         RoleExpiration(3);
190
191         final int id;
192         Notify(int id) {this.id = id;}
193         public int getValue() {return id;}
194         public static Notify from(int type) {
195             for (Notify t : Notify.values()) {
196                 if (t.id==type) {
197                     return t;
198                 }
199             }
200             return null;
201         }
202     }
203
204     public enum Response{
205         OK,
206         ERR_NotImplemented,
207         ERR_UserNotExist,
208         ERR_NotificationFailure,
209         };
210
211     public enum Expiration {
212         Password,
213         TempPassword,
214         Future,
215         UserInRole,
216         UserDelegate,
217         ExtendPassword
218     }
219
220     public enum Policy {
221         CHANGE_JOB,
222         LEFT_COMPANY,
223         CREATE_MECHID,
224         CREATE_MECHID_BY_PERM_ONLY,
225         OWNS_MECHID,
226         AS_RESPONSIBLE,
227         MAY_EXTEND_CRED_EXPIRES,
228         MAY_APPLY_DEFAULT_REALM
229     }
230
231     /**
232      * Notify a User of Action or Info
233      *
234      * @param type
235      * @param url
236      * @param users (separated by commas)
237      * @param ccs (separated by commas)
238      * @param summary
239      */
240
241     public Response notify(AuthzTrans trans, Notify type, String url, String ids[], String ccs[], String summary, Boolean urgent);
242
243     /**
244      * (more) generic way to send an email
245      *
246      * @param toList
247      * @param ccList
248      * @param subject
249      * @param body
250      * @param urgent
251      */
252
253     public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList, String subject, String body, Boolean urgent) throws OrganizationException;
254
255     /**
256      * whenToValidate
257      *
258      * Authz support services will ask the Organization Object at startup when it should
259      * kickoff Validation processes given particular types.
260      *
261      * This allows the Organization to express Policy
262      *
263      * Turn off Validation behavior by returning "null"
264      *
265      */
266     public Date whenToValidate(Notify type, Date lastValidated);
267
268
269     /**
270      * Expiration
271      *
272      * Given a Calendar item of Start (or now), set the Expiration Date based on the Policy
273      * based on type.
274      *
275      * For instance, "Passwords expire in 3 months"
276      *
277      * The Extra Parameter is used by certain Orgs.
278      *
279      * For Password, the extra is UserID, so it can check the User Type
280      *
281      * @param gc
282      * @param exp
283      * @return
284      */
285     public GregorianCalendar expiration(GregorianCalendar gc, Expiration exp, String ... extra);
286
287     /**
288      * Get Email Warning timing policies
289      * @return
290      */
291     public EmailWarnings emailWarningPolicy();
292
293     /**
294      *
295      * @param trans
296      * @param user
297      * @return
298      */
299     public List<Identity> getApprovers(AuthzTrans trans, String user) throws OrganizationException ;
300
301     /**
302      * Get Identities for Escalation Level
303      * 1 = self
304      * 2 = expects both self and immediate responsible party
305      * 3 = expects self, immediate report and any higher that the Organization wants to escalate to in the
306      *     hierarchy.
307      *
308      * Note: this is used to notify of imminent danger of Application's Cred or Role expirations.
309      */
310     public List<Identity> getIDs(AuthzTrans trans, String user, int escalate) throws OrganizationException ;
311
312
313     /*
314      *
315      * @param user
316      * @param type
317      * @param users
318      * @return
319     public Response notifyRequest(AuthzTrans trans, String user, Approval type, List<User> approvers);
320     */
321
322     /**
323      *
324      * @return
325      */
326     public String getApproverType();
327
328     /*
329      * startOfDay - define for company what hour of day business starts (specifically for password and other expiration which
330      *   were set by Date only.)
331      *
332      * @return
333      */
334     public int startOfDay();
335
336     /**
337      * implement this method to support any IDs that can have multiple entries in the cred table
338      * NOTE: the combination of ID/expiration date/(encryption type when implemented) must be unique.
339      *          Since expiration date is based on startOfDay for your company, you cannot create many
340      *          creds for the same ID in the same day.
341      * @param id
342      * @return
343      */
344     public boolean canHaveMultipleCreds(String id);
345
346     boolean isTestEnv();
347
348     public void setTestMode(boolean dryRun);
349
350     public static final Organization NULL = new Organization()
351     {
352         private final GregorianCalendar gc = new GregorianCalendar(1900, 1, 1);
353         private final List<Identity> nullList = new ArrayList<>();
354         private final Set<String> nullStringSet = new HashSet<>();
355         private String[] nullStringArray = new String[0];
356         private final Identity nullIdentity = new Identity() {
357             List<String> nullUser = new ArrayList<>();
358             @Override
359             public String type() {
360                 return N_A;
361             }
362
363             @Override
364             public String mayOwn() {
365                 return N_A; // negative case
366             }
367
368             @Override
369             public boolean isFound() {
370                 return false;
371             }
372
373             @Override
374             public String id() {
375                 return N_A;
376             }
377
378             @Override
379             public String fullID() {
380                 return N_A;
381             }
382
383             @Override
384             public String email() {
385                 return N_A;
386             }
387
388             @Override
389             public List<String> delegate() {
390                 return nullUser;
391             }
392             @Override
393             public String fullName() {
394                 return N_A;
395             }
396             @Override
397             public Organization org() {
398                 return NULL;
399             }
400             @Override
401             public String firstName() {
402                 return N_A;
403             }
404             @Override
405             public boolean isPerson() {
406                 return false;
407             }
408
409             @Override
410             public Identity responsibleTo() {
411                 return null;
412             }
413         };
414         @Override
415         public String getName() {
416             return N_A;
417         }
418
419         @Override
420         public String getRealm() {
421             return N_A;
422         }
423
424         @Override
425         public boolean supportsRealm(String r) {
426             return false;
427         }
428
429         @Override
430         public void addSupportedRealm(String r) {
431         }
432         
433         @Override
434         public String supportedDomain(String r) {
435                 return null;
436         }
437
438         @Override
439         public String getDomain() {
440             return N_A;
441         }
442
443         @Override
444         public Identity getIdentity(AuthzTrans trans, String id) {
445             return nullIdentity;
446         }
447
448         @Override
449         public String isValidID(final AuthzTrans trans, String id) {
450             return N_A;
451         }
452
453         @Override
454         public String isValidPassword(final AuthzTrans trans, final String user, final String password, final String... prev) {
455             return N_A;
456         }
457
458         @Override
459         public Set<String> getIdentityTypes() {
460             return nullStringSet;
461         }
462
463         @Override
464         public Response notify(AuthzTrans trans, Notify type, String url,
465                 String[] users, String[] ccs, String summary, Boolean urgent) {
466             return Response.ERR_NotImplemented;
467         }
468
469         @Override
470         public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList,
471                 String subject, String body, Boolean urgent) throws OrganizationException {
472             return 0;
473         }
474
475         @Override
476         public Date whenToValidate(Notify type, Date lastValidated) {
477             return gc.getTime();
478         }
479
480         @Override
481         public GregorianCalendar expiration(GregorianCalendar gc,
482                 Expiration exp, String... extra) {
483             return gc;
484         }
485
486         @Override
487         public List<Identity> getApprovers(AuthzTrans trans, String user)
488                 throws OrganizationException {
489             return nullList;
490         }
491
492         @Override
493         public String getApproverType() {
494             return "";
495         }
496
497         @Override
498         public int startOfDay() {
499             return 0;
500         }
501
502         @Override
503         public boolean canHaveMultipleCreds(String id) {
504             return false;
505         }
506
507         @Override
508         public boolean isValidCred(final AuthzTrans trans, final String id) {
509             return false;
510         }
511
512         @Override
513         public String validate(AuthzTrans trans, Policy policy, Executor executor, String ... vars)
514                 throws OrganizationException {
515             return "Null Organization rejects all Policies";
516         }
517
518         @Override
519         public boolean isTestEnv() {
520             return false;
521         }
522
523         @Override
524         public void setTestMode(boolean dryRun) {
525         }
526
527         @Override
528         public EmailWarnings emailWarningPolicy() {
529             return new EmailWarnings() {
530
531                 @Override
532                 public long credEmailInterval()
533                 {
534                     return 604800000L; // 7 days in millis 1000 * 86400 * 7
535                 }
536
537                 @Override
538                 public long roleEmailInterval()
539                 {
540                     return 604800000L; // 7 days in millis 1000 * 86400 * 7
541                 }
542
543                 @Override
544                 public long apprEmailInterval() {
545                     return 259200000L; // 3 days in millis 1000 * 86400 * 3
546                 }
547
548                 @Override
549                 public long  credExpirationWarning()
550                 {
551                     return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30  in milliseconds
552                 }
553
554                 @Override
555                 public long roleExpirationWarning()
556                 {
557                     return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30  in milliseconds
558                 }
559
560                 @Override
561                 public long emailUrgentWarning()
562                 {
563                     return( 1209600000L ); // Two weeks, in milliseconds 1000 * 86400 * 14  in milliseconds
564                 }
565
566             };
567
568
569         }
570
571         @Override
572         public String[] getPasswordRules() {
573             return nullStringArray;
574         }
575
576         @Override
577         public boolean isRevoked(AuthzTrans trans, String id) {
578             // provide a corresponding feed that indicates that an ID has been intentionally removed from identities.dat table.
579             return false;
580         }
581
582         @Override
583         public List<Identity> getIDs(AuthzTrans trans, String user, int escalate) throws OrganizationException {
584             // TODO Auto-generated method stub
585             return null;
586         }
587
588     };
589 }
590
591