diff --git a/core/src/main/java/org/jclouds/http/filters/BasicAuthentication.java b/core/src/main/java/org/jclouds/http/filters/BasicAuthentication.java new file mode 100644 index 0000000000..ae817d5fb3 --- /dev/null +++ b/core/src/main/java/org/jclouds/http/filters/BasicAuthentication.java @@ -0,0 +1,63 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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 + * @author Adrian Cole + * + */ +@Singleton +public class BasicAuthentication implements HttpRequestFilter { + + private List 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; + } +} \ No newline at end of file diff --git a/core/src/test/java/org/jclouds/http/filters/BasicAuthenticationTest.java b/core/src/test/java/org/jclouds/http/filters/BasicAuthenticationTest.java new file mode 100755 index 0000000000..2e31335198 --- /dev/null +++ b/core/src/test/java/org/jclouds/http/filters/BasicAuthenticationTest.java @@ -0,0 +1,55 @@ +/** + * + * Copyright (C) 2009 Global Cloud Specialists, Inc. + * + * ==================================================================== + * 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=="); + } + +} \ No newline at end of file