This repository has been archived by the owner on Jul 26, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merging the user-password authentication part from feature/daemonauth
- Loading branch information
Showing
3 changed files
with
110 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
syncany-daemon/src/main/java/org/syncany/operations/daemon/auth/MapIdentityManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |