1 /*
   2  * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.security.auth.login;
  27 
  28 import java.util.Map;
  29 import java.util.Collections;
  30 
  31 /**
  32  * This class represents a single {@code LoginModule} entry
  33  * configured for the application specified in the
  34  * {@code getAppConfigurationEntry(String appName)}
  35  * method in the {@code Configuration} class.  Each respective
  36  * {@code AppConfigurationEntry} contains a {@code LoginModule} name,
  37  * a control flag (specifying whether this {@code LoginModule} is
  38  * REQUIRED, REQUISITE, SUFFICIENT, or OPTIONAL), and LoginModule-specific
  39  * options.  Please refer to the {@code Configuration} class for
  40  * more information on the different control flags and their semantics.
  41  *
  42  * @see javax.security.auth.login.Configuration
  43  */
  44 public class AppConfigurationEntry {
  45 
  46     private String loginModuleName;
  47     private LoginModuleControlFlag controlFlag;
  48     private Map<String,?> options;
  49 
  50     /**
  51      * Default constructor for this class.
  52      *
  53      * <p> This entry represents a single {@code LoginModule}
  54      * entry configured for the application specified in the
  55      * {@code getAppConfigurationEntry(String appName)}
  56      * method from the {@code Configuration} class.
  57      *
  58      * @param loginModuleName String representing the class name of the
  59      *                  {@code LoginModule} configured for the
  60      *                  specified application.
  61      *
  62      * @param controlFlag either REQUIRED, REQUISITE, SUFFICIENT,
  63      *                  or OPTIONAL.
  64      *
  65      * @param options the options configured for this {@code LoginModule}.
  66      *
  67      * @exception IllegalArgumentException if {@code loginModuleName}
  68      *                  is null, if {@code LoginModuleName}
  69      *                  has a length of 0, if {@code controlFlag}
  70      *                  is not either REQUIRED, REQUISITE, SUFFICIENT
  71      *                  or OPTIONAL, or if {@code options} is null.
  72      */
  73     public AppConfigurationEntry(String loginModuleName,
  74                                 LoginModuleControlFlag controlFlag,
  75                                 Map<String,?> options)
  76     {
  77         if (loginModuleName == null || loginModuleName.length() == 0 ||
  78             (controlFlag != LoginModuleControlFlag.REQUIRED &&
  79                 controlFlag != LoginModuleControlFlag.REQUISITE &&
  80                 controlFlag != LoginModuleControlFlag.SUFFICIENT &&
  81                 controlFlag != LoginModuleControlFlag.OPTIONAL) ||
  82             options == null)
  83             throw new IllegalArgumentException();
  84 
  85         this.loginModuleName = loginModuleName;
  86         this.controlFlag = controlFlag;
  87         this.options = Collections.unmodifiableMap(options);
  88     }
  89 
  90     /**
  91      * Get the class name of the configured {@code LoginModule}.
  92      *
  93      * @return the class name of the configured {@code LoginModule} as
  94      *          a String.
  95      */
  96     public String getLoginModuleName() {
  97         return loginModuleName;
  98     }
  99 
 100     /**
 101      * Return the controlFlag
 102      * (either REQUIRED, REQUISITE, SUFFICIENT, or OPTIONAL)
 103      * for this {@code LoginModule}.
 104      *
 105      * @return the controlFlag
 106      *          (either REQUIRED, REQUISITE, SUFFICIENT, or OPTIONAL)
 107      *          for this {@code LoginModule}.
 108      */
 109     public LoginModuleControlFlag getControlFlag() {
 110         return controlFlag;
 111     }
 112 
 113     /**
 114      * Get the options configured for this {@code LoginModule}.
 115      *
 116      * @return the options configured for this {@code LoginModule}
 117      *          as an unmodifiable {@code Map}.
 118      */
 119     public Map<String,?> getOptions() {
 120         return options;
 121     }
 122 
 123     /**
 124      * This class represents whether or not a {@code LoginModule}
 125      * is REQUIRED, REQUISITE, SUFFICIENT or OPTIONAL.
 126      */
 127     public static class LoginModuleControlFlag {
 128 
 129         private String controlFlag;
 130 
 131         /**
 132          * Required {@code LoginModule}.
 133          */
 134         public static final LoginModuleControlFlag REQUIRED =
 135                                 new LoginModuleControlFlag("required");
 136 
 137         /**
 138          * Requisite {@code LoginModule}.
 139          */
 140         public static final LoginModuleControlFlag REQUISITE =
 141                                 new LoginModuleControlFlag("requisite");
 142 
 143         /**
 144          * Sufficient {@code LoginModule}.
 145          */
 146         public static final LoginModuleControlFlag SUFFICIENT =
 147                                 new LoginModuleControlFlag("sufficient");
 148 
 149         /**
 150          * Optional {@code LoginModule}.
 151          */
 152         public static final LoginModuleControlFlag OPTIONAL =
 153                                 new LoginModuleControlFlag("optional");
 154 
 155         private LoginModuleControlFlag(String controlFlag) {
 156             this.controlFlag = controlFlag;
 157         }
 158 
 159         /**
 160          * Return a String representation of this controlFlag.
 161          *
 162          * <p> The String has the format, "LoginModuleControlFlag: <i>flag</i>",
 163          * where <i>flag</i> is either <i>required</i>, <i>requisite</i>,
 164          * <i>sufficient</i>, or <i>optional</i>.
 165          *
 166          * @return a String representation of this controlFlag.
 167          */
 168         public String toString() {
 169             return (sun.security.util.ResourcesMgr.getString
 170                 ("LoginModuleControlFlag.") + controlFlag);
 171         }
 172     }
 173 }