001    /** 
002     * Copyright (C) 2009 "Darwin V. Felix" <darwinfelix@users.sourceforge.net>
003     * 
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Lesser General Public
006     * License as published by the Free Software Foundation; either
007     * version 2.1 of the License, or (at your option) any later version.
008     * 
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Lesser General Public License for more details.
013     * 
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this library; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017     */
018    
019    package net.sourceforge.spnego;
020    
021    import net.sourceforge.spnego.SpnegoHttpFilter.Constants;
022    
023    /**
024     * Example schemes are "Negotiate" and "Basic". 
025     * 
026     * <p>See examples and tutorials at 
027     * <a href="http://spnego.sourceforge.net" target="_blank">http://spnego.sourceforge.net</a>
028     * </p>
029     * 
030     * @author Darwin V. Felix
031     *
032     */
033    final class SpnegoAuthScheme {
034        
035        /** Zero length byte array. */
036        private static final transient byte[] EMPTY_BYTE_ARRAY = new byte[0];
037    
038        /** HTTP (Request) "Authorization" Header scheme. */ 
039        private final transient String scheme;
040    
041        /** HTTP (Request) scheme token. */
042        private final transient String token;
043        
044        /** true if Basic Auth scheme. */
045        private final transient boolean basicScheme;
046        
047        /** true if Negotiate scheme. */
048        private final transient boolean negotiateScheme;
049        
050        /** true if NTLM token. */
051        private final transient boolean ntlm;
052    
053        /**
054         * 
055         * @param authScheme 
056         * @param authToken 
057         */
058        public SpnegoAuthScheme(final String authScheme, final String authToken) {
059            this.scheme = authScheme;
060            this.token = authToken;
061            
062            if (null == authToken || authToken.isEmpty()) {
063                this.ntlm = false;
064            } else {
065                this.ntlm = authToken.startsWith(SpnegoHttpFilter.Constants.NTLM_PROLOG);
066            }
067            
068            this.negotiateScheme = Constants.NEGOTIATE_HEADER.equalsIgnoreCase(authScheme);
069            this.basicScheme = Constants.BASIC_HEADER.equalsIgnoreCase(authScheme);
070        }
071        
072        /**
073         * Returns true if this SpnegoAuthScheme is of type "Basic".
074         * 
075         * @return true if Basic Auth scheme
076         */
077        boolean isBasicScheme() {
078            return this.basicScheme;
079        }
080        
081        /**
082         * Returns true if this SpnegoAuthScheme is of type "Negotiate".
083         * 
084         * @return true if Negotiate scheme
085         */
086        boolean isNegotiateScheme() {
087            return this.negotiateScheme;
088        }
089        /**
090         * Returns true if NTLM.
091         * 
092         * @return true if Servlet Filter received NTLM token
093         */
094        boolean isNtlmToken() {
095            return this.ntlm;
096        }
097    
098        /**
099         * Returns HTTP Authorization scheme.
100         * 
101         * @return "Negotiate" or "Basic"
102         */
103        public String getScheme() {
104            return this.scheme;
105        }
106    
107        /**
108         * Returns a copy of byte[].
109         * 
110         * @return copy of token
111         */
112        public byte[] getToken() {
113            return (null == this.token) ? EMPTY_BYTE_ARRAY : Base64.decode(this.token);
114        }
115    }