|
| 1 | +/* |
| 2 | + * Copyright 2023 Inrupt Inc. |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal in |
| 6 | + * the Software without restriction, including without limitation the rights to use, |
| 7 | + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 8 | + * Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | + * subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 15 | + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 16 | + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 17 | + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 18 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 19 | + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | + */ |
| 21 | +package com.inrupt.client.auth; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.*; |
| 24 | + |
| 25 | +import com.inrupt.client.Request; |
| 26 | + |
| 27 | +import java.net.URI; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Optional; |
| 31 | +import java.util.Set; |
| 32 | +import java.util.UUID; |
| 33 | +import java.util.concurrent.CompletableFuture; |
| 34 | +import java.util.concurrent.CompletionStage; |
| 35 | + |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | + |
| 38 | +class ReactiveAuthorizationTest { |
| 39 | + |
| 40 | + @Test |
| 41 | + void testProhibitedAuth() { |
| 42 | + final ReactiveAuthorization auth = new ReactiveAuthorization(); |
| 43 | + final Session session = new BasicAuthSession(); |
| 44 | + final Request req = Request.newBuilder(URI.create("https://storage.example")).build(); |
| 45 | + final Optional<Credential> credential = auth.negotiate(session, req, |
| 46 | + Collections.singleton(Challenge.of("Basic"))).toCompletableFuture().join(); |
| 47 | + assertFalse(credential.isPresent()); |
| 48 | + } |
| 49 | + |
| 50 | + static class BasicAuthSession implements Session { |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getId() { |
| 54 | + return UUID.randomUUID().toString(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public Optional<URI> getPrincipal() { |
| 59 | + return Optional.empty(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public Set<String> supportedSchemes() { |
| 64 | + return Collections.singleton("Basic"); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Optional<Credential> getCredential(final URI name, final URI uri) { |
| 69 | + return Optional.empty(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Optional<Credential> fromCache(final Request request) { |
| 74 | + return Optional.empty(); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Optional<String> generateProof(final String jkt, final Request request) { |
| 79 | + return Optional.empty(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public Optional<String> selectThumbprint(final Collection<String> algorithms) { |
| 84 | + return Optional.empty(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public CompletionStage<Optional<Credential>> authenticate(final Authenticator authenticator, |
| 89 | + final Request request, final Set<String> algorithms) { |
| 90 | + return authenticator.authenticate(this, request, algorithms).thenApply(Optional::ofNullable); |
| 91 | + } |
| 92 | + |
| 93 | + /* deprecated */ |
| 94 | + @Override |
| 95 | + public CompletionStage<Optional<Credential>> authenticate(final Request request, |
| 96 | + final Set<String> algorithms) { |
| 97 | + return CompletableFuture.completedFuture(Optional.empty()); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments