-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Basic auth to Swagger support.
- Loading branch information
Showing
3 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
src/main/java/io/sinistral/proteus/server/security/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,110 @@ | ||
/** | ||
* | ||
*/ | ||
package io.sinistral.proteus.server.security; | ||
|
||
import java.security.Principal; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import io.undertow.security.idm.Account; | ||
import io.undertow.security.idm.Credential; | ||
import io.undertow.security.idm.IdentityManager; | ||
import io.undertow.security.idm.PasswordCredential; | ||
|
||
/** | ||
* @author jbauer | ||
*/ | ||
public class MapIdentityManager implements IdentityManager | ||
{ | ||
|
||
private final Map<String, char[]> identities; | ||
|
||
public MapIdentityManager(final Map<String, char[]> identities) | ||
{ | ||
this.identities = identities; | ||
} | ||
|
||
@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 = identities.get(account.getPrincipal().getName()); | ||
|
||
return Arrays.equals(password, expectedPassword); | ||
} | ||
return false; | ||
} | ||
|
||
private Account getAccount(final String id) | ||
{ | ||
if (identities.containsKey(id)) | ||
{ | ||
return new UserAccount(id); | ||
} | ||
return null; | ||
} | ||
|
||
private class UserAccount implements Account | ||
{ | ||
|
||
private static final long serialVersionUID = -8234851531206339721L; | ||
|
||
private final Principal principal; | ||
|
||
public UserAccount(String id) | ||
{ | ||
principal = new Principal() | ||
{ | ||
|
||
@Override | ||
public String getName() | ||
{ | ||
return id; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public Principal getPrincipal() | ||
{ | ||
return principal; | ||
} | ||
|
||
@Override | ||
public Set<String> getRoles() | ||
{ | ||
return Collections.emptySet(); | ||
} | ||
} | ||
|
||
} |
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