Skip to content

Instantly share code, notes, and snippets.

@koert
Last active August 29, 2015 14:25
Show Gist options
  • Select an option

  • Save koert/28bfa07cbc5623e1c248 to your computer and use it in GitHub Desktop.

Select an option

Save koert/28bfa07cbc5623e1c248 to your computer and use it in GitHub Desktop.
/**
* Repository for authentication.
* @author Koert Zeilstra
*/
@ApplicationScoped
public class AuthenticationRepository {
private Map<String, AuthenticationToken> tokenCache = new HashMap<>();
public Optional<AuthenticationToken> authenticateUser(String userName, String password) {
Optional<AuthenticationToken> result = Optional.empty();
if ("test".equals(userName) && "welcome".equals(password)) {
AuthenticationToken token = createAuthenticationToken(userName);
result = Optional.of(token);
}
return result;
}
public AuthenticationToken authenticateUser2(String userName, String password) {
AuthenticationToken result = null;
if ("test".equals(userName) && "welcome".equals(password)) {
result = createAuthenticationToken(userName);
}
return result;
}
/**
* @param userName User name.
* @return New authentication token.
*/
public AuthenticationToken createAuthenticationToken(String userName) {
UUID uuid = UUID.randomUUID();
LocalDateTime date = LocalDateTime.now().plusHours(8);
Set<String> roles = new HashSet<>();
User user = new User(userName, "Test", roles);
AuthenticationToken token = new AuthenticationToken(uuid.toString(), date, user);
tokenCache.put(token.getId(), token);
return token;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment