1 /*
   2  * Copyright (c) 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package client;
  24 
  25 import java.io.IOException;
  26 import java.security.Principal;
  27 import javax.security.auth.callback.Callback;
  28 import javax.security.auth.callback.CallbackHandler;
  29 import javax.security.auth.callback.NameCallback;
  30 import javax.security.auth.callback.PasswordCallback;
  31 import javax.security.auth.callback.UnsupportedCallbackException;
  32 import javax.security.auth.login.LoginException;
  33 import javax.security.auth.login.LoginContext;
  34 import com.sun.security.auth.UnixPrincipal;
  35 
  36 /**
  37  * JAAS client which will try to authenticate a user through a custom JAAS LOGIN
  38  * Module.
  39  */
  40 public class JaasClient {
  41 
  42     private static final String USER_NAME = "testUser";
  43     private static final String PASSWORD = "testPassword";
  44     private static final String LOGIN_CONTEXT = "ModularLoginConf";
  45 
  46     public static void main(String[] args) {
  47         try {
  48             LoginContext lc = new LoginContext(LOGIN_CONTEXT,
  49                     new MyCallbackHandler());
  50             lc.login();
  51             checkPrincipal(lc, true);
  52             lc.logout();
  53             checkPrincipal(lc, false);
  54         } catch (LoginException le) {
  55             throw new RuntimeException(le);
  56         }
  57         System.out.println("Test passed.");
  58 
  59     }
  60 
  61     /*
  62      * Check context for principal of the test user.
  63      */
  64     private static void checkPrincipal(LoginContext loginContext,
  65             boolean principalShouldExist) {
  66         if (!principalShouldExist) {
  67             if (loginContext.getSubject().getPrincipals().size() != 0) {
  68                 throw new RuntimeException("Test failed. Principal was not "
  69                         + "cleared.");
  70             }
  71             return;
  72         }
  73         for (Principal p : loginContext.getSubject().getPrincipals()) {
  74             if (p instanceof UnixPrincipal
  75                     && USER_NAME.equals(p.getName())) {
  76                 //Proper principal was found, return.
  77                 return;
  78             }
  79         }
  80         throw new RuntimeException("Test failed. UnixPrincipal "
  81                 + USER_NAME + " expected.");
  82 
  83     }
  84 
  85     private static class MyCallbackHandler implements CallbackHandler {
  86 
  87         @Override
  88         public void handle(Callback[] callbacks) throws IOException,
  89                 UnsupportedCallbackException {
  90             for (Callback callback : callbacks) {
  91                 if (callback instanceof NameCallback) {
  92                     ((NameCallback) callback).setName(USER_NAME);
  93                 } else if (callback instanceof PasswordCallback) {
  94                     ((PasswordCallback) callback).setPassword(
  95                             PASSWORD.toCharArray());
  96                 } else {
  97                     throw new UnsupportedCallbackException(callback);
  98                 }
  99             }
 100         }
 101     }
 102 
 103 }