use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.
the class AzureAdProfile method isExpired.
@Override
public boolean isExpired() {
try {
JWT jwt = this.getIdToken();
JWTClaimsSet claims = jwt.getJWTClaimsSet();
Date expiresOn = claims.getExpirationTime();
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, idTokenExpireAdvance);
if (expiresOn.before(now.getTime())) {
return true;
}
} catch (ParseException e) {
throw new TechnicalException(e);
}
return false;
}
use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.
the class DbAuthenticatorBuilder method tryBuildDbAuthenticator.
public void tryBuildDbAuthenticator(final Map<String, Authenticator> authenticators, final Map<String, PasswordEncoder> encoders) {
for (int i = 0; i <= MAX_NUM_AUTHENTICATORS; i++) {
if (containsProperty(DB_DATASOURCE_CLASS_NAME, i) || containsProperty(DB_JDBC_URL, i)) {
try {
final DataSource ds = buildDataSource(i);
final DbProfileService authenticator = new DbProfileService(ds);
if (containsProperty(DB_ATTRIBUTES, i)) {
authenticator.setAttributes(getProperty(DB_ATTRIBUTES, i));
}
if (containsProperty(DB_USER_ID_ATTRIBUTE, i)) {
authenticator.setIdAttribute(getProperty(DB_USER_ID_ATTRIBUTE, i));
}
if (containsProperty(DB_USERNAME_ATTRIBUTE, i)) {
authenticator.setUsernameAttribute(getProperty(DB_USERNAME_ATTRIBUTE, i));
}
if (containsProperty(DB_USER_PASSWORD_ATTRIBUTE, i)) {
authenticator.setPasswordAttribute(getProperty(DB_USER_PASSWORD_ATTRIBUTE, i));
}
if (containsProperty(DB_USERS_TABLE, i)) {
authenticator.setUsersTable(getProperty(DB_USERS_TABLE, i));
}
if (containsProperty(DB_PASSWORD_ENCODER, i)) {
authenticator.setPasswordEncoder(encoders.get(getProperty(DB_PASSWORD_ENCODER, i)));
}
authenticators.put(concat("db", i), authenticator);
} catch (final SQLException e) {
throw new TechnicalException(e);
}
}
}
}
use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.
the class SpringEncoderBuilder method tryCreatePasswordEncoder.
public void tryCreatePasswordEncoder(final Map<String, org.pac4j.core.credentials.password.PasswordEncoder> encoders) {
for (int i = 0; i <= MAX_NUM_ENCODERS; i++) {
final String type = getProperty(SPRING_ENCODER_TYPE, i);
if (isNotBlank(type)) {
final PasswordEncoder encoder;
if (SpringEncoderType.NOOP.toString().equalsIgnoreCase(type)) {
encoder = NoOpPasswordEncoder.getInstance();
} else if (SpringEncoderType.BCRYPT.toString().equalsIgnoreCase(type)) {
if (containsProperty(SPRING_ENCODER_BCRYPT_LENGTH, i)) {
encoder = new BCryptPasswordEncoder(getPropertyAsInteger(SPRING_ENCODER_BCRYPT_LENGTH, i));
} else {
encoder = new BCryptPasswordEncoder();
}
} else if (SpringEncoderType.PBKDF2.toString().equalsIgnoreCase(type)) {
if (containsProperty(SPRING_ENCODER_PBKDF2_SECRET, i)) {
final String secret = getProperty(SPRING_ENCODER_PBKDF2_SECRET, i);
if (containsProperty(SPRING_ENCODER_PBKDF2_ITERATIONS, i) && containsProperty(SPRING_ENCODER_PBKDF2_HASH_WIDTH, i)) {
encoder = new Pbkdf2PasswordEncoder(secret, getPropertyAsInteger(SPRING_ENCODER_PBKDF2_ITERATIONS, i), getPropertyAsInteger(SPRING_ENCODER_PBKDF2_HASH_WIDTH, i));
} else {
encoder = new Pbkdf2PasswordEncoder(secret);
}
} else {
encoder = new Pbkdf2PasswordEncoder();
}
} else if (SpringEncoderType.SCRYPT.toString().equalsIgnoreCase(type)) {
if (containsProperty(SPRING_ENCODER_SCRYPT_CPU_COST, i) && containsProperty(SPRING_ENCODER_SCRYPT_MEMORY_COST, i) && containsProperty(SPRING_ENCODER_SCRYPT_PARALLELIZATION, i) && containsProperty(SPRING_ENCODER_SCRYPT_KEY_LENGTH, i) && containsProperty(SPRING_ENCODER_SCRYPT_SALT_LENGTH, i)) {
encoder = new SCryptPasswordEncoder(getPropertyAsInteger(SPRING_ENCODER_SCRYPT_CPU_COST, i), getPropertyAsInteger(SPRING_ENCODER_SCRYPT_MEMORY_COST, i), getPropertyAsInteger(SPRING_ENCODER_SCRYPT_PARALLELIZATION, i), getPropertyAsInteger(SPRING_ENCODER_SCRYPT_KEY_LENGTH, i), getPropertyAsInteger(SPRING_ENCODER_SCRYPT_SALT_LENGTH, i));
} else {
encoder = new SCryptPasswordEncoder();
}
} else if (SpringEncoderType.STANDARD.toString().equalsIgnoreCase(type)) {
if (containsProperty(SPRING_ENCODER_STANDARD_SECRET, i)) {
encoder = new StandardPasswordEncoder(getProperty(SPRING_ENCODER_STANDARD_SECRET, i));
} else {
encoder = new StandardPasswordEncoder();
}
} else {
throw new TechnicalException("Unsupported spring encoder type: " + type);
}
encoders.put(concat(SPRING_ENCODER, i), new SpringSecurityPasswordEncoder(encoder));
}
}
}
use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.
the class DirectCasProxyClientTests method testTokenExistsValidationOccurs.
@Test
public void testTokenExistsValidationOccurs() {
final CasConfiguration configuration = new CasConfiguration();
configuration.setLoginUrl(LOGIN_URL);
configuration.setProtocol(CasProtocol.CAS30_PROXY);
configuration.setDefaultTicketValidator((ticket, service) -> {
if (TICKET.equals(ticket) && CALLBACK_URL.equals(service)) {
return new AssertionImpl(TICKET);
}
throw new TechnicalException("Bad ticket or service");
});
final DirectCasProxyClient client = new DirectCasProxyClient(configuration, CALLBACK_URL);
final MockWebContext context = MockWebContext.create();
context.setFullRequestURL(CALLBACK_URL + "?" + CasConfiguration.TICKET_PARAMETER + "=" + TICKET);
context.addRequestParameter(CasConfiguration.TICKET_PARAMETER, TICKET);
final TokenCredentials credentials = client.getCredentials(context);
assertEquals(TICKET, credentials.getToken());
final CommonProfile profile = credentials.getUserProfile();
assertTrue(profile instanceof CasProfile);
assertEquals(TICKET, profile.getId());
}
use of org.pac4j.core.exception.TechnicalException in project pac4j by pac4j.
the class DefaultSecurityClientFinder method find.
public List<Client> find(final Clients clients, final WebContext context, final String clientNames) {
final List<Client> result = new ArrayList<>();
String securityClientNames = clientNames;
// we don't have defined clients to secure the URL, use the general default security ones from the Clients if they exist
// we check the nullity and not the blankness to allow the blank string to mean no client
// so no clients parameter -> use the default security ones; clients=blank string -> no clients defined
logger.debug("Provided clientNames: {}", securityClientNames);
if (clientNames == null) {
securityClientNames = clients.getDefaultSecurityClients();
logger.debug("Default security clients: {}", securityClientNames);
// still no clients defined and we only have one client, use it
if (securityClientNames == null && clients.findAllClients().size() == 1) {
securityClientNames = clients.getClients().get(0).getName();
logger.debug("Only client: {}", securityClientNames);
}
}
if (CommonHelper.isNotBlank(securityClientNames)) {
final List<String> names = Arrays.asList(securityClientNames.split(Pac4jConstants.ELEMENT_SEPRATOR));
// if a "client_name" parameter is provided on the request, get the client
// and check if it is allowed (defined in the list of the clients)
final String clientNameOnRequest = context.getRequestParameter(clientNameParameter);
logger.debug("clientNameOnRequest: {}", clientNameOnRequest);
if (clientNameOnRequest != null) {
// from the request
final Client client = clients.findClient(clientNameOnRequest);
final String nameFound = client.getName();
// if allowed -> return it
boolean found = false;
for (final String name : names) {
if (CommonHelper.areEqualsIgnoreCaseAndTrim(name, nameFound)) {
result.add(client);
found = true;
break;
}
}
if (!found) {
throw new TechnicalException("Client not allowed: " + nameFound);
}
} else {
// no client provided, return all
for (final String name : names) {
// from its name
final Client client = clients.findClient(name);
result.add(client);
}
}
}
logger.debug("result: {}", result.stream().map(c -> c.getName()).collect(Collectors.toList()));
return result;
}
Aggregations