diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/org/tmatesoft/hg/internal/remote/BasicAuthenticator.java	Thu Aug 08 19:18:50 2013 +0200
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2013 TMate Software Ltd
+ *  
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * For information on how to redistribute this software under
+ * the terms of a license other than GNU General Public License
+ * contact TMate Software at support@hg4j.com
+ */
+package org.tmatesoft.hg.internal.remote;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import org.tmatesoft.hg.auth.HgAuthFailedException;
+import org.tmatesoft.hg.auth.HgAuthMethod;
+import org.tmatesoft.hg.auth.HgAuthenticator;
+import org.tmatesoft.hg.repo.HgRemoteRepository.RemoteDescriptor;
+import org.tmatesoft.hg.util.LogFacility;
+import org.tmatesoft.hg.util.LogFacility.Severity;
+
+/**
+ * 
+ * @author Artem Tikhomirov
+ * @author TMate Software Ltd.
+ */
+public class BasicAuthenticator implements HgAuthenticator {
+	private final LogFacility log;
+
+	public BasicAuthenticator(LogFacility logFacility) {
+		log = logFacility;
+	}
+			
+	public void authenticate(RemoteDescriptor rd, HgAuthMethod authMethod) throws HgAuthFailedException {
+		if (authMethod.supportsPublicKey()) {
+			if (tryPlatformDefaultKeyLocations(rd, authMethod)) {
+				return;
+			}
+		}
+		authMethod.noCredentials();
+	}
+
+	// return true is successfully aithenticated
+	protected boolean tryPlatformDefaultKeyLocations(RemoteDescriptor rd, HgAuthMethod authMethod) {
+		final String userHome = System.getProperty("user.home");
+		File sshDir = new File(userHome, ".ssh");
+		if (!sshDir.isDirectory()) {
+			return false;
+		}
+		final String username = System.getProperty("user.name");
+		for (String fn : new String[] { "id_rsa", "id_dsa", "identity"}) {
+			File id = new File(sshDir, fn);
+			if (!id.canRead()) {
+				continue;
+			}
+			try {
+				FileInputStream fis = new FileInputStream(id);
+				authMethod.withPublicKey(username, fis, null);
+				fis.close();
+				return true;
+			} catch (IOException ex) {
+				log.dump(getClass(), Severity.Warn, ex, String.format("Attempting default ssh identity key locations: %s", id));
+				// ignore
+			} catch (HgAuthFailedException ex) {
+				log.dump(getClass(), Severity.Debug, ex, String.format("Attempting default ssh identity key locations: %s", id));
+				// ignore
+			}
+		}
+		return false;
+	}
+}
\ No newline at end of file