2 * Copyright (C) 2018, 2020 Thomas Wolf <thomas.wolf@paranor.ch> and others
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Distribution License v. 1.0 which is available at
6 * https://www.eclipse.org/org/documents/edl-v10.php.
8 * SPDX-License-Identifier: BSD-3-Clause
10 package org.eclipse.jgit.internal.transport.sshd;
12 import static org.apache.sshd.core.CoreModuleProperties.PASSWORD_PROMPTS;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.security.GeneralSecurityException;
17 import java.util.Arrays;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.atomic.AtomicInteger;
21 import java.util.function.Supplier;
23 import org.apache.sshd.common.AttributeRepository.AttributeKey;
24 import org.apache.sshd.common.NamedResource;
25 import org.apache.sshd.common.config.keys.FilePasswordProvider;
26 import org.apache.sshd.common.session.SessionContext;
27 import org.eclipse.jgit.annotations.NonNull;
28 import org.eclipse.jgit.transport.CredentialsProvider;
29 import org.eclipse.jgit.transport.URIish;
30 import org.eclipse.jgit.transport.sshd.KeyPasswordProvider;
33 * A bridge from sshd's {@link FilePasswordProvider} to our per-session
34 * {@link KeyPasswordProvider} API.
36 public class PasswordProviderWrapper implements FilePasswordProvider {
38 private static final AttributeKey<PerSessionState> STATE = new AttributeKey<>();
40 private static class PerSessionState {
42 Map<String, AtomicInteger> counts = new ConcurrentHashMap<>();
44 KeyPasswordProvider delegate;
48 private final Supplier<KeyPasswordProvider> factory;
51 * Creates a new {@link PasswordProviderWrapper}.
54 * to use to create per-session {@link KeyPasswordProvider}s
56 public PasswordProviderWrapper(
57 @NonNull Supplier<KeyPasswordProvider> factory) {
58 this.factory = factory;
61 private PerSessionState getState(SessionContext context) {
62 PerSessionState state = context.getAttribute(STATE);
64 state = new PerSessionState();
65 state.delegate = factory.get();
66 state.delegate.setAttempts(
67 PASSWORD_PROMPTS.getRequiredDefault().intValue());
68 context.setAttribute(STATE, state);
74 public String getPassword(SessionContext session, NamedResource resource,
75 int attemptIndex) throws IOException {
76 String key = resource.getName();
77 PerSessionState state = getState(session);
78 int attempt = state.counts
79 .computeIfAbsent(key, k -> new AtomicInteger()).get();
80 char[] passphrase = state.delegate.getPassphrase(toUri(key), attempt);
81 if (passphrase == null) {
85 return new String(passphrase);
87 Arrays.fill(passphrase, '\000');
92 public ResourceDecodeResult handleDecodeAttemptResult(
93 SessionContext session, NamedResource resource, int retryIndex,
94 String password, Exception err)
95 throws IOException, GeneralSecurityException {
96 String key = resource.getName();
97 PerSessionState state = getState(session);
98 AtomicInteger count = state.counts.get(key);
99 int numberOfAttempts = count == null ? 0 : count.incrementAndGet();
100 ResourceDecodeResult result = null;
102 if (state.delegate.keyLoaded(toUri(key), numberOfAttempts, err)) {
103 result = ResourceDecodeResult.RETRY;
105 result = ResourceDecodeResult.TERMINATE;
108 if (result != ResourceDecodeResult.RETRY) {
109 state.counts.remove(key);
116 * Creates a {@link URIish} from a given string. The
117 * {@link CredentialsProvider} uses uris as resource identifications.
123 private URIish toUri(String resourceKey) {
125 return new URIish(resourceKey);
126 } catch (URISyntaxException e) {
127 return new URIish().setPath(resourceKey); // Doesn't check!!