mirror of https://github.com/apache/jclouds.git
added support for BASIC authentication
git-svn-id: http://jclouds.googlecode.com/svn/trunk@1914 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
parent
3cde3d6356
commit
eaa4674918
|
@ -0,0 +1,63 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*/
|
||||||
|
package org.jclouds.http.filters;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
|
|
||||||
|
import org.jclouds.http.HttpException;
|
||||||
|
import org.jclouds.http.HttpRequest;
|
||||||
|
import org.jclouds.http.HttpRequestFilter;
|
||||||
|
import org.jclouds.http.HttpUtils;
|
||||||
|
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uses Basic Authentication to sign the request.
|
||||||
|
*
|
||||||
|
* @see <a href= "http://en.wikipedia.org/wiki/Basic_access_authentication" />
|
||||||
|
* @author Adrian Cole
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Singleton
|
||||||
|
public class BasicAuthentication implements HttpRequestFilter {
|
||||||
|
|
||||||
|
private List<String> credentialList;
|
||||||
|
|
||||||
|
public BasicAuthentication(String user, String password) throws UnsupportedEncodingException {
|
||||||
|
this.credentialList = Collections.singletonList("Basic "
|
||||||
|
+ HttpUtils.toBase64String(String.format("%s:%s", checkNotNull(user, "user"),
|
||||||
|
checkNotNull(password, "password")).getBytes("UTF-8")));
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpRequest filter(HttpRequest request) throws HttpException {
|
||||||
|
request.getHeaders().replaceValues(HttpHeaders.AUTHORIZATION, credentialList);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,55 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright (C) 2009 Global Cloud Specialists, Inc. <info@globalcloudspecialists.com>
|
||||||
|
*
|
||||||
|
* ====================================================================
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you under the Apache License, Version 2.0 (the
|
||||||
|
* "License"); you may not use this file except in compliance
|
||||||
|
* with the License. You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing,
|
||||||
|
* software distributed under the License is distributed on an
|
||||||
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||||
|
* KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations
|
||||||
|
* under the License.
|
||||||
|
* ====================================================================
|
||||||
|
*/
|
||||||
|
package org.jclouds.http.filters;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
|
|
||||||
|
import org.jclouds.http.HttpRequest;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Adrian Cole
|
||||||
|
*/
|
||||||
|
@Test(groups = "unit", testName = "http.BasicAuthenticationTest")
|
||||||
|
public class BasicAuthenticationTest {
|
||||||
|
|
||||||
|
private static final String USER = "Aladdin";
|
||||||
|
private static final String PASSWORD = "open sesame";
|
||||||
|
|
||||||
|
|
||||||
|
public void testAuth() throws UnsupportedEncodingException {
|
||||||
|
BasicAuthentication filter = new BasicAuthentication(USER, PASSWORD);
|
||||||
|
HttpRequest request = new HttpRequest("GET", URI.create("http://localhost"));
|
||||||
|
filter.filter(request);
|
||||||
|
assertEquals(request.getFirstHeaderOrNull(HttpHeaders.AUTHORIZATION),
|
||||||
|
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue