// Path: server/sshd/src/main/java/org/kercoin/magrit/sshd/auth/GitPublickeyAuthenticator.java
// public class GitPublickeyAuthenticator implements PublickeyAuthenticator {
// 
// 	private final Context context;
// 	private final GitUtils gitUtils;
// 	
// 	private Repository datasource;
// 	
// 	@Inject
// 	public GitPublickeyAuthenticator(Context context, GitUtils gitUtils) {
// 		this.context = context;
// 		this.gitUtils = gitUtils;
// 	}
// 	
// 	private void open() throws IOException {
// 		if (this.datasource == null) {
// 			this.datasource = Git.open(
// 					context.configuration().getPublickeyRepositoryDir()
// 			).getRepository();
// 		}
// 	}
// 	
// 	@Override
// 	public boolean authenticate(String username, PublicKey authKey,
// 			ServerSession session) {
// 		try {
// 			open();
// 			PublicKey targetKey = readKeyFromRepository(username);
// 			return targetKey != null && areEqual(targetKey, authKey);
// 		} catch (Exception e) {
// 			e.printStackTrace();
// 			return false;
// 		}
// 	}
// 	
// 	boolean areEqual(PublicKey ref, PublicKey candidate) {
// 		return Arrays.areEqual(ref.getEncoded(), candidate.getEncoded());
// 	}
// 		
// 	private PublicKey readKeyFromRepository(String username) throws AmbiguousObjectException, Exception {
// 		String revstr = String.format("HEAD:keys/%s.pub", username);
// 		String encoded = gitUtils.show(datasource, revstr);
// 		if (encoded == null)
// 			return null;
// 
// 		return new AuthorizedKeysDecoder().decodePublicKey(encoded);
// 	}
// 
// }
// 
// Path: server/sshd/src/main/java/org/kercoin/magrit/sshd/commands/MagritCommandFactory.java
// @Singleton
// public class MagritCommandFactory implements CommandFactory {
// 
// 	private Iterable<AbstractCommand.CommandProvider<?>> commands;
// 	
// 	@Inject
// 	public MagritCommandFactory(CommandsProvider commandsProvider) {
// 		super();
// 		this.commands = commandsProvider.get();
// 	}
// 
// 	@Override
// 	public Command createCommand(String command) {
// 		if (command == null || command.length() == 0) {
// 			throw new IllegalArgumentException();
// 		}
// 		try {
// 			for (AbstractCommand.CommandProvider<?> cp : commands) {
// 				if (cp.accept(command)) {
// 					return cp.get().command(command);
// 				}
// 			}
// 		} catch (Exception e) {
// 			return new SyntaxErrorCommand(e);
// 		}
// 		return new SyntaxErrorCommand(new IllegalArgumentException("Unknown command " + command));
// 	}
// 
// }


import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.PublickeyAuthenticator;
import org.kercoin.magrit.sshd.auth.GitPublickeyAuthenticator;
import org.kercoin.magrit.sshd.commands.MagritCommandFactory;
import com.google.inject.AbstractModule;
/*
Copyright 2011-2012 Frederic Menou and others referred in AUTHORS file.

This file is part of Magrit.

Magrit is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

Magrit 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with Magrit.
If not, see <http://www.gnu.org/licenses/>.
*/
package org.kercoin.magrit.sshd;



/**
 * @author ptitfred
 *
 */
public class SshdModule extends AbstractModule {

	@Override
	protected void configure() {
// Path: server/sshd/src/main/java/org/kercoin/magrit/sshd/auth/GitPublickeyAuthenticator.java
// public class GitPublickeyAuthenticator implements PublickeyAuthenticator {
// 
// 	private final Context context;
// 	private final GitUtils gitUtils;
// 	
// 	private Repository datasource;
// 	
// 	@Inject
// 	public GitPublickeyAuthenticator(Context context, GitUtils gitUtils) {
// 		this.context = context;
// 		this.gitUtils = gitUtils;
// 	}
// 	
// 	private void open() throws IOException {
// 		if (this.datasource == null) {
// 			this.datasource = Git.open(
// 					context.configuration().getPublickeyRepositoryDir()
// 			).getRepository();
// 		}
// 	}
// 	
// 	@Override
// 	public boolean authenticate(String username, PublicKey authKey,
// 			ServerSession session) {
// 		try {
// 			open();
// 			PublicKey targetKey = readKeyFromRepository(username);
// 			return targetKey != null && areEqual(targetKey, authKey);
// 		} catch (Exception e) {
// 			e.printStackTrace();
// 			return false;
// 		}
// 	}
// 	
// 	boolean areEqual(PublicKey ref, PublicKey candidate) {
// 		return Arrays.areEqual(ref.getEncoded(), candidate.getEncoded());
// 	}
// 		
// 	private PublicKey readKeyFromRepository(String username) throws AmbiguousObjectException, Exception {
// 		String revstr = String.format("HEAD:keys/%s.pub", username);
// 		String encoded = gitUtils.show(datasource, revstr);
// 		if (encoded == null)
// 			return null;
// 
// 		return new AuthorizedKeysDecoder().decodePublicKey(encoded);
// 	}
// 
// }
// 
// Path: server/sshd/src/main/java/org/kercoin/magrit/sshd/commands/MagritCommandFactory.java
// @Singleton
// public class MagritCommandFactory implements CommandFactory {
// 
// 	private Iterable<AbstractCommand.CommandProvider<?>> commands;
// 	
// 	@Inject
// 	public MagritCommandFactory(CommandsProvider commandsProvider) {
// 		super();
// 		this.commands = commandsProvider.get();
// 	}
// 
// 	@Override
// 	public Command createCommand(String command) {
// 		if (command == null || command.length() == 0) {
// 			throw new IllegalArgumentException();
// 		}
// 		try {
// 			for (AbstractCommand.CommandProvider<?> cp : commands) {
// 				if (cp.accept(command)) {
// 					return cp.get().command(command);
// 				}
// 			}
// 		} catch (Exception e) {
// 			return new SyntaxErrorCommand(e);
// 		}
// 		return new SyntaxErrorCommand(new IllegalArgumentException("Unknown command " + command));
// 	}
// 
// }


// Path: server/sshd/src/main/java/org/kercoin/magrit/sshd/SshdModule.java
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.PublickeyAuthenticator;
import org.kercoin.magrit.sshd.auth.GitPublickeyAuthenticator;
import org.kercoin.magrit.sshd.commands.MagritCommandFactory;
import com.google.inject.AbstractModule;

/*
Copyright 2011-2012 Frederic Menou and others referred in AUTHORS file.

This file is part of Magrit.

Magrit is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

Magrit 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public
License along with Magrit.
If not, see <http://www.gnu.org/licenses/>.
*/
package org.kercoin.magrit.sshd;



/**
 * @author ptitfred
 *
 */
public class SshdModule extends AbstractModule {

	@Override
	protected void configure() {
		bind(PublickeyAuthenticator.class).to(GitPublickeyAuthenticator.class);