Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code #400

Closed
wants to merge 8 commits into from
Prev Previous commit
Next Next commit
Solved-Smell: Solved Design Smell 'Broken Hierarchy' in `SshShellPass…
…wordAuthenticationProvider` using Replace Conditional with Polymorphism
  • Loading branch information
smangukia committed Nov 30, 2024
commit de47251cf77b9df50f2d34bcea27d3f4013b5b5c
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@
* limitations under the License.
*/

package com.github.fonimus.ssh.shell.auth;
package com.github.fonimus.ssh.shell.auth;

import org.apache.sshd.server.auth.password.PasswordAuthenticator;

/**
* Interface to implements custom authentication provider
*/
@FunctionalInterface
public interface SshShellAuthenticationProvider
extends PasswordAuthenticator {

String AUTHENTICATION_ATTRIBUTE = "authentication";

}
import org.apache.sshd.server.auth.password.PasswordAuthenticator;

/**
* Interface to implement custom authentication providers.
*/
@FunctionalInterface
public interface SshShellAuthenticationProvider extends PasswordAuthenticator {

String AUTHENTICATION_ATTRIBUTE = "authentication";

/**
* Returns the type of authentication provided by this implementation.
*
* @return the authentication type
*/
default String getAuthenticationType() {
return "Generic";
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,44 @@
* limitations under the License.
*/

package com.github.fonimus.ssh.shell.auth;

import lombok.extern.slf4j.Slf4j;
import org.apache.sshd.server.auth.password.PasswordChangeRequiredException;
import org.apache.sshd.server.session.ServerSession;

import java.util.UUID;

/**
* Password implementation
*/
@Slf4j
public class SshShellPasswordAuthenticationProvider
implements SshShellAuthenticationProvider {

private final String user;

private final String password;

public SshShellPasswordAuthenticationProvider(String user, String password) {
this.user = user;
String pass = password;
if (pass == null) {
pass = UUID.randomUUID().toString();
LOGGER.info(" --- Generating password for ssh connection: {}", pass);
}
this.password = pass;
}

@Override
public boolean authenticate(String username, String pass,
ServerSession serverSession) throws PasswordChangeRequiredException {

serverSession.getIoSession().setAttribute(AUTHENTICATION_ATTRIBUTE, new SshAuthentication(username, username));

return username.equals(this.user) && pass.equals(this.password);
}
}
package com.github.fonimus.ssh.shell.auth;

import lombok.extern.slf4j.Slf4j;
import org.apache.sshd.server.auth.password.PasswordChangeRequiredException;
import org.apache.sshd.server.session.ServerSession;

import java.util.UUID;

/**
* Password implementation
*/
@Slf4j
public class SshShellPasswordAuthenticationProvider implements SshShellAuthenticationProvider {

private final String user;

private final String password;

public SshShellPasswordAuthenticationProvider(String user, String password) {
this.user = user;
String pass = password;
if (pass == null) {
pass = UUID.randomUUID().toString();
LOGGER.info(" --- Generating password for ssh connection: {}", pass);
}
this.password = pass;
}

@Override
public boolean authenticate(String username, String pass, ServerSession serverSession)
throws PasswordChangeRequiredException {

serverSession.getIoSession().setAttribute(AUTHENTICATION_ATTRIBUTE, new SshAuthentication(username, username));
return username.equals(this.user) && pass.equals(this.password);
}

@Override
public String getAuthenticationType() {
return "Password";
}
}