|
| 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 | +} |
0 commit comments