View Javadoc
1   /**
2    * Waffle (https://github.com/dblock/waffle)
3    *
4    * Copyright (c) 2010 - 2015 Application Security, Inc.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Contributors:
12   *     Application Security, Inc.
13   */
14  package waffle.windows.auth.impl;
15  
16  import waffle.windows.auth.IWindowsAccount;
17  
18  import com.sun.jna.platform.win32.Advapi32Util;
19  import com.sun.jna.platform.win32.Advapi32Util.Account;
20  import com.sun.jna.platform.win32.Secur32.EXTENDED_NAME_FORMAT;
21  import com.sun.jna.platform.win32.Secur32Util;
22  
23  /**
24   * Windows Account.
25   * 
26   * @author dblock[at]dblock[dot]org
27   */
28  public class WindowsAccountImpl implements IWindowsAccount {
29  
30      /** The account. */
31      private final Account account;
32  
33      /**
34       * Windows Account.
35       * 
36       * @param newAccount
37       *            Account.
38       */
39      public WindowsAccountImpl(final Account newAccount) {
40          this.account = newAccount;
41      }
42  
43      /**
44       * Windows Account.
45       * 
46       * @param userName
47       *            Fully qualified username.
48       */
49      public WindowsAccountImpl(final String userName) {
50          this(userName, null);
51      }
52  
53      /**
54       * Windows Account.
55       * 
56       * @param accountName
57       *            Username, without a domain or machine.
58       * @param systemName
59       *            Machine name.
60       */
61      public WindowsAccountImpl(final String accountName, final String systemName) {
62          this(Advapi32Util.getAccountByName(systemName, accountName));
63      }
64  
65      /**
66       * Get the SAM-compatible username of the currently logged-on user.
67       * 
68       * @return String.
69       */
70      public static String getCurrentUsername() {
71          return Secur32Util.getUserNameEx(EXTENDED_NAME_FORMAT.NameSamCompatible);
72      }
73  
74      /**
75       * Account domain.
76       * 
77       * @return String.
78       */
79      @Override
80      public String getDomain() {
81          return this.account.domain;
82      }
83  
84      /* (non-Javadoc)
85       * @see waffle.windows.auth.IWindowsAccount#getFqn()
86       */
87      @Override
88      public String getFqn() {
89          return this.account.fqn;
90      }
91  
92      /**
93       * Account name.
94       * 
95       * @return String.
96       */
97      @Override
98      public String getName() {
99          return this.account.name;
100     }
101 
102     /* (non-Javadoc)
103      * @see waffle.windows.auth.IWindowsAccount#getSidString()
104      */
105     @Override
106     public String getSidString() {
107         return this.account.sidString;
108     }
109 
110 }