~ubuntu-branches/ubuntu/precise/commons-httpclient/precise-security

« back to all changes in this revision

Viewing changes to src/java/org/apache/commons/httpclient/auth/BasicScheme.java

  • Committer: Bazaar Package Importer
  • Author(s): Michael Meskes
  • Date: 2006-09-15 20:07:43 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060915200743-t2md4cgfsb07wgn7
Tags: 3.0.1-0.1
* Non-maintainer upload.
* Bump debhelper Build-Depends to (>= 4.1.0) as required by cdbs' 
  debhelper.mk
* Put the coppyright holders in debian/copyright
* Include the jar file in the package. (Closes: #381354)
* Only include one copy of the docs.
  done by James Westby <jw+debian@jameswestby.net>  Mon, 14 Aug 2006 02:29:47 +0100

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/auth/BasicScheme.java,v 1.4.2.3 2004/02/22 18:21:14 olegk Exp $
3
 
 * $Revision: 1.4.2.3 $
4
 
 * $Date: 2004/02/22 18:21:14 $
 
2
 * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/auth/BasicScheme.java,v 1.17 2004/05/13 04:02:00 mbecke Exp $
 
3
 * $Revision: 155418 $
 
4
 * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5
5
 *
6
6
 * ====================================================================
7
7
 *
25
25
 * information on the Apache Software Foundation, please see
26
26
 * <http://www.apache.org/>.
27
27
 *
28
 
 * [Additional notices, if required by prior licensing conditions]
29
 
 *
30
28
 */
31
29
 
32
30
package org.apache.commons.httpclient.auth;
33
31
 
34
 
import org.apache.commons.httpclient.HttpConstants; 
 
32
import org.apache.commons.codec.binary.Base64;
35
33
import org.apache.commons.httpclient.Credentials;
 
34
import org.apache.commons.httpclient.HttpMethod;
36
35
import org.apache.commons.httpclient.UsernamePasswordCredentials;
37
 
import org.apache.commons.httpclient.util.Base64; 
 
36
import org.apache.commons.httpclient.util.EncodingUtil;
38
37
import org.apache.commons.logging.Log;
39
38
import org.apache.commons.logging.LogFactory;
40
39
 
46
45
 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
47
46
 * @author Rodney Waldhoff
48
47
 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
49
 
 * @author Ortwin Gl�ck
 
48
 * @author Ortwin Gl?ck
50
49
 * @author Sean C. Sullivan
51
50
 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a>
52
51
 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
58
57
    /** Log object for this class. */
59
58
    private static final Log LOG = LogFactory.getLog(BasicScheme.class);
60
59
    
 
60
    /** Whether the basic authentication process is complete */
 
61
    private boolean complete;
 
62
    
 
63
    /**
 
64
     * Default constructor for the basic authetication scheme.
 
65
     * 
 
66
     * @since 3.0
 
67
     */
 
68
    public BasicScheme() {
 
69
        super();
 
70
        this.complete = false;
 
71
    }
 
72
 
61
73
    /**
62
74
     * Constructor for the basic authetication scheme.
63
75
     * 
65
77
     * 
66
78
     * @throws MalformedChallengeException is thrown if the authentication challenge
67
79
     * is malformed
 
80
     * 
 
81
     * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} 
 
82
     *             method
68
83
     */
69
84
    public BasicScheme(final String challenge) throws MalformedChallengeException {
70
85
        super(challenge);
 
86
        this.complete = true;
71
87
    }
72
88
 
73
 
 
74
89
    /**
75
90
     * Returns textual designation of the basic authentication scheme.
76
91
     * 
81
96
    }
82
97
 
83
98
    /**
 
99
     * Processes the Basic challenge.
 
100
     *  
 
101
     * @param challenge the challenge string
 
102
     * 
 
103
     * @throws MalformedChallengeException is thrown if the authentication challenge
 
104
     * is malformed
 
105
     * 
 
106
     * @since 3.0
 
107
     */
 
108
    public void processChallenge(String challenge) 
 
109
        throws MalformedChallengeException 
 
110
    {
 
111
        super.processChallenge(challenge);
 
112
        this.complete = true;
 
113
    }
 
114
 
 
115
    /**
 
116
     * Tests if the Basic authentication process has been completed.
 
117
     * 
 
118
     * @return <tt>true</tt> if Basic authorization has been processed,
 
119
     *   <tt>false</tt> otherwise.
 
120
     * 
 
121
     * @since 3.0
 
122
     */
 
123
    public boolean isComplete() {
 
124
        return this.complete;
 
125
    }
 
126
 
 
127
    /**
84
128
     * Produces basic authorization string for the given set of 
85
129
     * {@link Credentials}.
86
130
     * 
87
131
     * @param credentials The set of credentials to be used for athentication
88
132
     * @param method Method name is ignored by the basic authentication scheme
89
133
     * @param uri URI is ignored by the basic authentication scheme
 
134
     * @throws InvalidCredentialsException if authentication credentials
 
135
     *         are not valid or not applicable for this authentication scheme
90
136
     * @throws AuthenticationException if authorization string cannot 
91
137
     *   be generated due to an authentication failure
92
138
     * 
93
139
     * @return a basic authorization string
 
140
     * 
 
141
     * @deprecated Use {@link #authenticate(Credentials, HttpMethod)}
94
142
     */
95
143
    public String authenticate(Credentials credentials, String method, String uri)
96
144
      throws AuthenticationException {
101
149
        try {
102
150
            usernamepassword = (UsernamePasswordCredentials) credentials;
103
151
        } catch (ClassCastException e) {
104
 
            throw new AuthenticationException(
 
152
            throw new InvalidCredentialsException(
105
153
             "Credentials cannot be used for basic authentication: " 
106
154
              + credentials.getClass().getName());
107
155
        }
109
157
    }
110
158
 
111
159
    /**
112
 
     * Return a basic <tt>Authorization</tt> header value for the given 
 
160
     * Returns <tt>false</tt>. Basic authentication scheme is request based.
 
161
     * 
 
162
     * @return <tt>false</tt>.
 
163
     * 
 
164
     * @since 3.0
 
165
     */
 
166
    public boolean isConnectionBased() {
 
167
        return false;    
 
168
    }
 
169
 
 
170
    /**
 
171
     * Produces basic authorization string for the given set of {@link Credentials}.
 
172
     * 
 
173
     * @param credentials The set of credentials to be used for athentication
 
174
     * @param method The method being authenticated
 
175
     * @throws InvalidCredentialsException if authentication credentials
 
176
     *         are not valid or not applicable for this authentication scheme
 
177
     * @throws AuthenticationException if authorization string cannot 
 
178
     *   be generated due to an authentication failure
 
179
     * 
 
180
     * @return a basic authorization string
 
181
     * 
 
182
     * @since 3.0
 
183
     */
 
184
    public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException {
 
185
 
 
186
        LOG.trace("enter BasicScheme.authenticate(Credentials, HttpMethod)");
 
187
 
 
188
        if (method == null) {
 
189
            throw new IllegalArgumentException("Method may not be null");
 
190
        }
 
191
        UsernamePasswordCredentials usernamepassword = null;
 
192
        try {
 
193
            usernamepassword = (UsernamePasswordCredentials) credentials;
 
194
        } catch (ClassCastException e) {
 
195
            throw new InvalidCredentialsException(
 
196
                    "Credentials cannot be used for basic authentication: " 
 
197
                    + credentials.getClass().getName());
 
198
        }
 
199
        return BasicScheme.authenticate(
 
200
            usernamepassword, 
 
201
            method.getParams().getCredentialCharset());
 
202
    }
 
203
    
 
204
    /**
 
205
     * @deprecated Use {@link #authenticate(UsernamePasswordCredentials, String)}
 
206
     * 
 
207
     * Returns a basic <tt>Authorization</tt> header value for the given 
113
208
     * {@link UsernamePasswordCredentials}.
114
209
     * 
115
210
     * @param credentials The credentials to encode.
117
212
     * @return a basic authorization string
118
213
     */
119
214
    public static String authenticate(UsernamePasswordCredentials credentials) {
120
 
 
121
 
        LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials)");
 
215
        return authenticate(credentials, "ISO-8859-1");
 
216
    }
 
217
 
 
218
    /**
 
219
     * Returns a basic <tt>Authorization</tt> header value for the given 
 
220
     * {@link UsernamePasswordCredentials} and charset.
 
221
     * 
 
222
     * @param credentials The credentials to encode.
 
223
     * @param charset The charset to use for encoding the credentials
 
224
     * 
 
225
     * @return a basic authorization string
 
226
     * 
 
227
     * @since 3.0
 
228
     */
 
229
    public static String authenticate(UsernamePasswordCredentials credentials, String charset) {
 
230
 
 
231
        LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)");
122
232
 
123
233
        if (credentials == null) {
124
234
            throw new IllegalArgumentException("Credentials may not be null"); 
125
235
        }
 
236
        if (charset == null || charset.length() == 0) {
 
237
            throw new IllegalArgumentException("charset may not be null or empty");
 
238
        }
126
239
        StringBuffer buffer = new StringBuffer();
127
240
        buffer.append(credentials.getUserName());
128
241
        buffer.append(":");
129
242
        buffer.append(credentials.getPassword());
130
243
        
131
 
        return "Basic " + HttpConstants.getAsciiString(
132
 
            Base64.encode(HttpConstants.getContentBytes(buffer.toString())));
 
244
        return "Basic " + EncodingUtil.getAsciiString(
 
245
                Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset)));
133
246
    }
 
247
    
134
248
}