Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Commit

Permalink
Merging the user-password authentication part from feature/daemonauth
Browse files Browse the repository at this point in the history
  • Loading branch information
pimotte committed Aug 1, 2014
1 parent 784620a commit b85bce3
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 9 deletions.
Expand Up @@ -24,7 +24,7 @@ public class WebServerTO {
private boolean enabled = true;

@Element(required = false)
private int port = 8080;
private int port = 8443;

@Element(required = false)
private String host = "localhost";
Expand Down
Expand Up @@ -20,34 +20,30 @@
import static io.undertow.Handlers.path;
import static io.undertow.Handlers.websocket;
import io.undertow.Undertow;
import io.undertow.server.HttpHandler;
import io.undertow.security.idm.IdentityManager;
import io.undertow.server.HttpServerExchange;
import io.undertow.websockets.core.WebSocketChannel;
import io.undertow.websockets.core.WebSockets;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.syncany.config.to.DaemonConfigTO;
import org.syncany.operations.daemon.auth.MapIdentityManager;
import org.syncany.operations.daemon.handlers.InternalRestHandler;
import org.syncany.operations.daemon.handlers.InternalWebInterfaceHandler;
import org.syncany.operations.daemon.handlers.InternalWebSocketHandler;
import org.syncany.operations.daemon.messages.BadRequestResponse;
import org.syncany.operations.daemon.messages.GetFileResponse;
import org.syncany.operations.daemon.messages.GetFileResponseInternal;
import org.syncany.operations.daemon.messages.MessageFactory;
import org.syncany.operations.daemon.messages.Request;
import org.syncany.operations.daemon.messages.Response;
import org.syncany.plugins.Plugins;
import org.syncany.plugins.web.WebInterfacePlugin;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
Expand Down Expand Up @@ -108,6 +104,13 @@ private void initEventBus() {
}

private void initServer(String host, int port) {
final Map<String, char[]> users = new HashMap<>(2);
users.put("userOne", "passwordOne".toCharArray());
users.put("userTwo", "passwordTwo".toCharArray());

final IdentityManager identityManager = new MapIdentityManager(users);


webServer = Undertow
.builder()
.addHttpListener(port, host)
Expand Down
@@ -0,0 +1,98 @@
/*
* Syncany, www.syncany.org
* Copyright (C) 2011-2014 Philipp C. Heckel <philipp.heckel@gmail.com>
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.syncany.operations.daemon.auth;

import io.undertow.security.idm.Account;
import io.undertow.security.idm.Credential;
import io.undertow.security.idm.IdentityManager;
import io.undertow.security.idm.PasswordCredential;

import java.security.Principal;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

public class MapIdentityManager implements IdentityManager {

private final Map<String, char[]> users;

public MapIdentityManager(final Map<String, char[]> users) {
this.users = users;
}

@Override
public Account verify(Account account) {
// An existing account so for testing assume still valid.
return account;
}

@Override
public Account verify(String id, Credential credential) {
Account account = getAccount(id);
if (account != null && verifyCredential(account, credential)) {
return account;
}

return null;
}

@Override
public Account verify(Credential credential) {
// TODO Auto-generated method stub
return null;
}

private boolean verifyCredential(Account account, Credential credential) {
if (credential instanceof PasswordCredential) {
char[] password = ((PasswordCredential) credential).getPassword();
char[] expectedPassword = users.get(account.getPrincipal().getName());

return Arrays.equals(password, expectedPassword);
}
return false;
}

private Account getAccount(final String id) {
if (users.containsKey(id)) {
return new Account() {

private final Principal principal = new Principal() {

@Override
public String getName() {
return id;
}
};

@Override
public Principal getPrincipal() {
return principal;
}

@Override
public Set<String> getRoles() {
return Collections.emptySet();
}

};
}
return null;
}

}

0 comments on commit b85bce3

Please sign in to comment.