Skip to content

Commit 90bc881

Browse files
author
Yaniv Inbar
committed
Fix bugs in GoogleStorageAuthentication (Issue 68)
1 parent db950eb commit 90bc881

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

google-api-client/src/com/google/api/client/googleapis/auth/storage/GoogleStorageAuthentication.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ public void intercept(HttpRequest request) throws IOException {
137137
messageBuf.append('/').append(bucket);
138138
}
139139
}
140+
if (url.pathParts != null) {
141+
messageBuf.append(url.getRawPath());
142+
}
140143
if (url.get("acl") != null) {
141144
messageBuf.append("?acl");
142145
} else if (url.get("location") != null) {
@@ -148,7 +151,6 @@ public void intercept(HttpRequest request) throws IOException {
148151
} else if (url.get("torrent") != null) {
149152
messageBuf.append("?torrent");
150153
}
151-
messageBuf.append(url.getRawPath());
152154
try {
153155
request.headers.authorization =
154156
"GOOG1 " + accessKey + ":" + HmacSha.sign(secret, messageBuf.toString());
Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
/*
22
* Copyright (c) 2010 Google Inc.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5-
* use this file except in compliance with the License. You may obtain a copy of
6-
* the License at
7-
*
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
87
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13-
* License for the specific language governing permissions and limitations under
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
1412
* the License.
1513
*/
1614

1715
package com.google.api.client.googleapis.auth.storage;
1816

17+
import com.google.api.client.auth.HmacSha;
1918
import com.google.api.client.googleapis.GoogleHeaders;
2019
import com.google.api.client.googleapis.GoogleTransport;
2120
import com.google.api.client.http.HttpExecuteIntercepter;
@@ -25,31 +24,48 @@
2524

2625
import junit.framework.TestCase;
2726

28-
import java.io.IOException;
2927
import java.util.Arrays;
3028

3129
/**
3230
* Tests {@link GoogleStorageAuthentication}.
33-
*
31+
*
3432
* @author Yaniv Inbar
3533
*/
3634
public class GoogleStorageAuthenticationTest extends TestCase {
3735

38-
public GoogleStorageAuthenticationTest() {
39-
}
36+
private static final String ACCESS_KEY = "GOOGTS7C7FUP3AIRVJTE";
37+
private static final String SECRET = "abc";
4038

4139
public GoogleStorageAuthenticationTest(String name) {
4240
super(name);
4341
}
4442

45-
public void test() throws IOException {
43+
public void test() throws Exception {
44+
subtest("http://travel-maps.commondatastorage.googleapis.com/europe/france/paris.jpg",
45+
"PUT\n" + "\n" + "image/jpg\n" + "Mon, 15 Feb 2010 21:30:39 GMT\n"
46+
+ "x-goog-acl:public-read\n" + "x-goog-meta-reviewer:bob,jane\n"
47+
+ "/travel-maps/europe/france/paris.jpg");
48+
subtest("http://travel-maps.commondatastorage.googleapis.com/europe/france/paris.jpg?acl",
49+
"PUT\n" + "\n" + "image/jpg\n" + "Mon, 15 Feb 2010 21:30:39 GMT\n"
50+
+ "x-goog-acl:public-read\n" + "x-goog-meta-reviewer:bob,jane\n"
51+
+ "/travel-maps/europe/france/paris.jpg?acl");
52+
subtest("http://travel-maps.commondatastorage.googleapis.com?acl",
53+
"PUT\n" + "\n" + "image/jpg\n" + "Mon, 15 Feb 2010 21:30:39 GMT\n"
54+
+ "x-goog-acl:public-read\n" + "x-goog-meta-reviewer:bob,jane\n" + "/travel-maps?acl");
55+
subtest("http://travel-maps.commondatastorage.googleapis.com",
56+
"PUT\n" + "\n" + "image/jpg\n" + "Mon, 15 Feb 2010 21:30:39 GMT\n"
57+
+ "x-goog-acl:public-read\n" + "x-goog-meta-reviewer:bob,jane\n" + "/travel-maps");
58+
subtest("http://travel-maps.commondatastorage.googleapis.com/",
59+
"PUT\n" + "\n" + "image/jpg\n" + "Mon, 15 Feb 2010 21:30:39 GMT\n"
60+
+ "x-goog-acl:public-read\n" + "x-goog-meta-reviewer:bob,jane\n" + "/travel-maps/");
61+
}
62+
63+
private void subtest(String url, String messageToSign) throws Exception {
4664
HttpTransport transport = GoogleTransport.create();
47-
GoogleStorageAuthentication.authorize(transport, "GOOGTS7C7FUP3AIRVJTE",
48-
"abc");
65+
GoogleStorageAuthentication.authorize(transport, ACCESS_KEY, SECRET);
4966
HttpExecuteIntercepter intercepter = transport.intercepters.get(1);
5067
HttpRequest request = transport.buildPutRequest();
51-
request
52-
.setUrl("http://travel-maps.commondatastorage.googleapis.com/europe/france/paris.jpg");
68+
request.setUrl(url);
5369
GoogleHeaders headers = (GoogleHeaders) request.headers;
5470
headers.date = "Mon, 15 Feb 2010 21:30:39 GMT";
5571
MockHttpContent content = new MockHttpContent();
@@ -59,7 +75,7 @@ public void test() throws IOException {
5975
headers.googAcl = "public-read";
6076
headers.set("x-goog-meta-reviewer", Arrays.asList("bob", "jane"));
6177
intercepter.intercept(request);
62-
assertEquals("GOOG1 GOOGTS7C7FUP3AIRVJTE:ovyTUuOaD+E6l/Xu+eOAhZ/8LKk=",
78+
assertEquals(messageToSign, "GOOG1 " + ACCESS_KEY + ":" + HmacSha.sign(SECRET, messageToSign),
6379
request.headers.authorization);
6480
}
6581
}

0 commit comments

Comments
 (0)