Skip to content

Commit f3a8549

Browse files
committed
Merge branch '__rultor'
2 parents f2b7c20 + a3a3e37 commit f3a8549

File tree

4 files changed

+138
-3
lines changed

4 files changed

+138
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ bin/
1313

1414
nb-configuration.xml
1515
.DS_Store
16+
/nbproject/

src/main/java/com/amihaiemil/docker/Auth.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
* @version $Id$
3232
* @see <a href="https://docs.docker.com/engine/api/v1.35/#section/Authentication">Authentication</a>
3333
* @since 0.0.1
34-
* @todo #99:30min Implement a new auth named 'Token' that will hold the user's
35-
* identity token from the auth endpoint. Implement some operation that would
36-
* call the /auth endpoint and obtain a token.
34+
* @todo #171:30min We have implemented all forms of Auth. We also have the
35+
* AuthHttpClient. Figure out how to make the library decorate the base
36+
* HttpClient with the AuthHttpClient in a seemless manner.
3737
*/
3838
public interface Auth {
3939
/**
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright (c) 2018, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
27+
package com.amihaiemil.docker;
28+
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.Base64;
31+
import java.util.function.Supplier;
32+
import javax.json.Json;
33+
34+
/**
35+
* An {@link Auth} holding an Identity token.
36+
* <p>
37+
* Identity tokens are obtained after validating your {@link Credentials}
38+
* with a registry. However, the docker engine is capable of obtaining this
39+
* token transparently if you just provide {@link Credentials}.
40+
* <p>
41+
* {@link IdentityToken} is useful for cases when you have already obtained
42+
* a token from a previous session and you wish to reuse it.
43+
*
44+
* @author George Aristy ([email protected])
45+
* @see <a href="https://docs.docker.com/engine/api/v1.35/#section/Authentication">Authentication</a>
46+
* @see <a href="https://docs.docker.com/registry/spec/auth/token/">Token Authentication Specification</a>
47+
* @see <a href="https://docs.docker.com/engine/api/v1.35/#operation/SystemAuth">Check auth configuration</a>
48+
* @version $Id$
49+
* @since 0.0.4
50+
*/
51+
public final class IdentityToken implements Auth {
52+
/**
53+
* Base64-encoded JSON structure holding the identity token.
54+
*/
55+
private final Supplier<String> value;
56+
57+
/**
58+
* Ctor.
59+
* @param value The token's value
60+
*/
61+
public IdentityToken(final String value) {
62+
this.value = () -> Base64.getEncoder().encodeToString(
63+
Json.createObjectBuilder().add("identitytoken", value)
64+
.build().toString()
65+
.getBytes(StandardCharsets.UTF_8)
66+
);
67+
}
68+
69+
@Override
70+
public String encoded() {
71+
return this.value.get();
72+
}
73+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright (c) 2018, Mihai Emil Andronache
3+
* All rights reserved.
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
* 1)Redistributions of source code must retain the above copyright notice,
7+
* this list of conditions and the following disclaimer.
8+
* 2)Redistributions in binary form must reproduce the above copyright notice,
9+
* this list of conditions and the following disclaimer in the documentation
10+
* and/or other materials provided with the distribution.
11+
* 3)Neither the name of docker-java-api nor the names of its
12+
* contributors may be used to endorse or promote products derived from
13+
* this software without specific prior written permission.
14+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24+
* POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.amihaiemil.docker;
27+
28+
import java.nio.charset.StandardCharsets;
29+
import java.util.Base64;
30+
import javax.json.Json;
31+
import org.hamcrest.MatcherAssert;
32+
import org.hamcrest.Matchers;
33+
import org.junit.Test;
34+
35+
/**
36+
* Unit tests for {@link IdentityToken}.
37+
*
38+
* @author George Aristy ([email protected])
39+
* @version $Id$
40+
* @since 0.0.4
41+
*/
42+
public final class IdentityTokenTestCase {
43+
/**
44+
* Correctly encodes to base64 all attributes as a JSON object.
45+
*/
46+
@Test
47+
public void correctEncoding() {
48+
final String token = "abc123";
49+
MatcherAssert.assertThat(
50+
new IdentityToken(token).encoded(),
51+
Matchers.is(
52+
Base64.getEncoder().encodeToString(
53+
Json.createObjectBuilder()
54+
.add("identitytoken", token)
55+
.build().toString()
56+
.getBytes(StandardCharsets.UTF_8)
57+
)
58+
)
59+
);
60+
}
61+
}

0 commit comments

Comments
 (0)