comparison src/org/tmatesoft/hg/internal/remote/BasicAuthenticator.java @ 699:a483b2b68a2e

Provisional APIs and respective implementation for http, https and ssh remote repositories
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 08 Aug 2013 19:18:50 +0200
parents
children
comparison
equal deleted inserted replaced
698:822f3a83ff57 699:a483b2b68a2e
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal.remote;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22
23 import org.tmatesoft.hg.auth.HgAuthFailedException;
24 import org.tmatesoft.hg.auth.HgAuthMethod;
25 import org.tmatesoft.hg.auth.HgAuthenticator;
26 import org.tmatesoft.hg.repo.HgRemoteRepository.RemoteDescriptor;
27 import org.tmatesoft.hg.util.LogFacility;
28 import org.tmatesoft.hg.util.LogFacility.Severity;
29
30 /**
31 *
32 * @author Artem Tikhomirov
33 * @author TMate Software Ltd.
34 */
35 public class BasicAuthenticator implements HgAuthenticator {
36 private final LogFacility log;
37
38 public BasicAuthenticator(LogFacility logFacility) {
39 log = logFacility;
40 }
41
42 public void authenticate(RemoteDescriptor rd, HgAuthMethod authMethod) throws HgAuthFailedException {
43 if (authMethod.supportsPublicKey()) {
44 if (tryPlatformDefaultKeyLocations(rd, authMethod)) {
45 return;
46 }
47 }
48 authMethod.noCredentials();
49 }
50
51 // return true is successfully aithenticated
52 protected boolean tryPlatformDefaultKeyLocations(RemoteDescriptor rd, HgAuthMethod authMethod) {
53 final String userHome = System.getProperty("user.home");
54 File sshDir = new File(userHome, ".ssh");
55 if (!sshDir.isDirectory()) {
56 return false;
57 }
58 final String username = System.getProperty("user.name");
59 for (String fn : new String[] { "id_rsa", "id_dsa", "identity"}) {
60 File id = new File(sshDir, fn);
61 if (!id.canRead()) {
62 continue;
63 }
64 try {
65 FileInputStream fis = new FileInputStream(id);
66 authMethod.withPublicKey(username, fis, null);
67 fis.close();
68 return true;
69 } catch (IOException ex) {
70 log.dump(getClass(), Severity.Warn, ex, String.format("Attempting default ssh identity key locations: %s", id));
71 // ignore
72 } catch (HgAuthFailedException ex) {
73 log.dump(getClass(), Severity.Debug, ex, String.format("Attempting default ssh identity key locations: %s", id));
74 // ignore
75 }
76 }
77 return false;
78 }
79 }