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.spring;
15  
16  import org.springframework.security.core.GrantedAuthority;
17  import org.springframework.security.core.authority.SimpleGrantedAuthority;
18  
19  import waffle.windows.auth.WindowsAccount;
20  
21  /**
22   * A {@link GrantedAuthorityFactory} that uses the {@link WindowsAccount}'s fqn as the basis of the
23   * {@link GrantedAuthority} string, and (optionally) applies two transformations:
24   * <ul>
25   * <li>prepending a prefix, and</li>
26   * <li>converting to uppercase</li>
27   * </ul>
28   * .
29   */
30  public class FqnGrantedAuthorityFactory implements GrantedAuthorityFactory {
31  
32      /** The prefix. */
33      private final String  prefix;
34      
35      /** The convert to upper case. */
36      private final boolean convertToUpperCase;
37  
38      /**
39       * Instantiates a new fqn granted authority factory.
40       *
41       * @param newPrefix
42       *            the new prefix
43       * @param newConvertToUpperCase
44       *            the new convert to upper case
45       */
46      public FqnGrantedAuthorityFactory(final String newPrefix, final boolean newConvertToUpperCase) {
47          this.prefix = newPrefix;
48          this.convertToUpperCase = newConvertToUpperCase;
49      }
50  
51      /* (non-Javadoc)
52       * @see waffle.spring.GrantedAuthorityFactory#createGrantedAuthority(waffle.windows.auth.WindowsAccount)
53       */
54      @Override
55      public GrantedAuthority createGrantedAuthority(final WindowsAccount windowsAccount) {
56  
57          String grantedAuthorityString = windowsAccount.getFqn();
58  
59          if (this.prefix != null) {
60              grantedAuthorityString = this.prefix + grantedAuthorityString;
61          }
62  
63          if (this.convertToUpperCase) {
64              grantedAuthorityString = grantedAuthorityString.toUpperCase();
65          }
66  
67          return new SimpleGrantedAuthority(grantedAuthorityString);
68      }
69  }