95f37859d8fac5425aa05bbccb783bb8da56b3a1
[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         RevokedGracePeriodEnds
219     }
220
221     public enum Policy {
222         CHANGE_JOB,
223         LEFT_COMPANY,
224         CREATE_MECHID,
225         CREATE_MECHID_BY_PERM_ONLY,
226         OWNS_MECHID,
227         AS_RESPONSIBLE,
228         MAY_EXTEND_CRED_EXPIRES,
229         MAY_APPLY_DEFAULT_REALM
230     }
231
232     /**
233      * Notify a User of Action or Info
234      *
235      * @param type
236      * @param url
237      * @param users (separated by commas)
238      * @param ccs (separated by commas)
239      * @param summary
240      */
241
242     public Response notify(AuthzTrans trans, Notify type, String url, String ids[], String ccs[], String summary, Boolean urgent);
243
244     /**
245      * (more) generic way to send an email
246      *
247      * @param toList
248      * @param ccList
249      * @param subject
250      * @param body
251      * @param urgent
252      */
253
254     public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList, String subject, String body, Boolean urgent) throws OrganizationException;
255
256     /**
257      * whenToValidate
258      *
259      * Authz support services will ask the Organization Object at startup when it should
260      * kickoff Validation processes given particular types.
261      *
262      * This allows the Organization to express Policy
263      *
264      * Turn off Validation behavior by returning "null"
265      *
266      */
267     public Date whenToValidate(Notify type, Date lastValidated);
268
269
270     /**
271      * Expiration
272      *
273      * Given a Calendar item of Start (or now), set the Expiration Date based on the Policy
274      * based on type.
275      *
276      * For instance, "Passwords expire in 3 months"
277      *
278      * The Extra Parameter is used by certain Orgs.
279      *
280      * For Password, the extra is UserID, so it can check the User Type
281      *
282      * @param gc
283      * @param exp
284      * @return
285      */
286     public GregorianCalendar expiration(GregorianCalendar gc, Expiration exp, String ... extra);
287
288     /**
289      * Get Email Warning timing policies
290      * @return
291      */
292     public EmailWarnings emailWarningPolicy();
293
294     /**
295      *
296      * @param trans
297      * @param user
298      * @return
299      */
300     public List<Identity> getApprovers(AuthzTrans trans, String user) throws OrganizationException ;
301
302     /**
303      * Get Identities for Escalation Level
304      * 1 = self
305      * 2 = expects both self and immediate responsible party
306      * 3 = expects self, immediate report and any higher that the Organization wants to escalate to in the
307      *     hierarchy.
308      *
309      * Note: this is used to notify of imminent danger of Application's Cred or Role expirations.
310      */
311     public List<Identity> getIDs(AuthzTrans trans, String user, int escalate) throws OrganizationException ;
312
313
314     /*
315      *
316      * @param user
317      * @param type
318      * @param users
319      * @return
320     public Response notifyRequest(AuthzTrans trans, String user, Approval type, List<User> approvers);
321     */
322
323     /**
324      *
325      * @return
326      */
327     public String getApproverType();
328
329     /*
330      * startOfDay - define for company what hour of day business starts (specifically for password and other expiration which
331      *   were set by Date only.)
332      *
333      * @return
334      */
335     public int startOfDay();
336
337     /**
338      * implement this method to support any IDs that can have multiple entries in the cred table
339      * NOTE: the combination of ID/expiration date/(encryption type when implemented) must be unique.
340      *          Since expiration date is based on startOfDay for your company, you cannot create many
341      *          creds for the same ID in the same day.
342      * @param id
343      * @return
344      */
345     public boolean canHaveMultipleCreds(String id);
346
347     boolean isTestEnv();
348
349     public void setTestMode(boolean dryRun);
350
351     public static final Organization NULL = new Organization()
352     {
353         private final GregorianCalendar gc = new GregorianCalendar(1900, 1, 1);
354         private final List<Identity> nullList = new ArrayList<>();
355         private final Set<String> nullStringSet = new HashSet<>();
356         private String[] nullStringArray = new String[0];
357         private final Identity nullIdentity = new Identity() {
358             List<String> nullUser = new ArrayList<>();
359             @Override
360             public String type() {
361                 return N_A;
362             }
363
364             @Override
365             public String mayOwn() {
366                 return N_A; // negative case
367             }
368
369             @Override
370             public boolean isFound() {
371                 return false;
372             }
373
374             @Override
375             public String id() {
376                 return N_A;
377             }
378
379             @Override
380             public String fullID() {
381                 return N_A;
382             }
383
384             @Override
385             public String email() {
386                 return N_A;
387             }
388
389             @Override
390             public List<String> delegate() {
391                 return nullUser;
392             }
393             @Override
394             public String fullName() {
395                 return N_A;
396             }
397             @Override
398             public Organization org() {
399                 return NULL;
400             }
401             @Override
402             public String firstName() {
403                 return N_A;
404             }
405             @Override
406             public boolean isPerson() {
407                 return false;
408             }
409
410             @Override
411             public Identity responsibleTo() {
412                 return null;
413             }
414         };
415         @Override
416         public String getName() {
417             return N_A;
418         }
419
420         @Override
421         public String getRealm() {
422             return N_A;
423         }
424
425         @Override
426         public boolean supportsRealm(String r) {
427             return false;
428         }
429
430         @Override
431         public void addSupportedRealm(String r) {
432         }
433         
434         @Override
435         public String supportedDomain(String r) {
436                 return null;
437         }
438
439         @Override
440         public String getDomain() {
441             return N_A;
442         }
443
444         @Override
445         public Identity getIdentity(AuthzTrans trans, String id) {
446             return nullIdentity;
447         }
448
449         @Override
450         public String isValidID(final AuthzTrans trans, String id) {
451             return N_A;
452         }
453
454         @Override
455         public String isValidPassword(final AuthzTrans trans, final String user, final String password, final String... prev) {
456             return N_A;
457         }
458
459         @Override
460         public Set<String> getIdentityTypes() {
461             return nullStringSet;
462         }
463
464         @Override
465         public Response notify(AuthzTrans trans, Notify type, String url,
466                 String[] users, String[] ccs, String summary, Boolean urgent) {
467             return Response.ERR_NotImplemented;
468         }
469
470         @Override
471         public int sendEmail(AuthzTrans trans, List<String> toList, List<String> ccList,
472                 String subject, String body, Boolean urgent) throws OrganizationException {
473             return 0;
474         }
475
476         @Override
477         public Date whenToValidate(Notify type, Date lastValidated) {
478             return gc.getTime();
479         }
480
481         @Override
482         public GregorianCalendar expiration(GregorianCalendar gc,
483                 Expiration exp, String... extra) {
484             return gc;
485         }
486
487         @Override
488         public List<Identity> getApprovers(AuthzTrans trans, String user)
489                 throws OrganizationException {
490             return nullList;
491         }
492
493         @Override
494         public String getApproverType() {
495             return "";
496         }
497
498         @Override
499         public int startOfDay() {
500             return 0;
501         }
502
503         @Override
504         public boolean canHaveMultipleCreds(String id) {
505             return false;
506         }
507
508         @Override
509         public boolean isValidCred(final AuthzTrans trans, final String id) {
510             return false;
511         }
512
513         @Override
514         public String validate(AuthzTrans trans, Policy policy, Executor executor, String ... vars)
515                 throws OrganizationException {
516             return "Null Organization rejects all Policies";
517         }
518
519         @Override
520         public boolean isTestEnv() {
521             return false;
522         }
523
524         @Override
525         public void setTestMode(boolean dryRun) {
526         }
527
528         @Override
529         public EmailWarnings emailWarningPolicy() {
530             return new EmailWarnings() {
531
532                 @Override
533                 public long credEmailInterval()
534                 {
535                     return 604800000L; // 7 days in millis 1000 * 86400 * 7
536                 }
537
538                 @Override
539                 public long roleEmailInterval()
540                 {
541                     return 604800000L; // 7 days in millis 1000 * 86400 * 7
542                 }
543
544                 @Override
545                 public long apprEmailInterval() {
546                     return 259200000L; // 3 days in millis 1000 * 86400 * 3
547                 }
548
549                 @Override
550                 public long  credExpirationWarning()
551                 {
552                     return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30  in milliseconds
553                 }
554
555                 @Override
556                 public long roleExpirationWarning()
557                 {
558                     return( 2592000000L ); // One month, in milliseconds 1000 * 86400 * 30  in milliseconds
559                 }
560
561                 @Override
562                 public long emailUrgentWarning()
563                 {
564                     return( 1209600000L ); // Two weeks, in milliseconds 1000 * 86400 * 14  in milliseconds
565                 }
566
567             };
568
569
570         }
571
572         @Override
573         public String[] getPasswordRules() {
574             return nullStringArray;
575         }
576
577         @Override
578         public boolean isRevoked(AuthzTrans trans, String id) {
579             // provide a corresponding feed that indicates that an ID has been intentionally removed from identities.dat table.
580             return false;
581         }
582
583         @Override
584         public List<Identity> getIDs(AuthzTrans trans, String user, int escalate) throws OrganizationException {
585             // TODO Auto-generated method stub
586             return null;
587         }
588
589     };
590 }
591
592