use of org.springframework.security.oauth.provider.OAuthVersionUnsupportedException in project spring-security-oauth by spring-projects.
the class OAuthProcessingFilterTests method testValidateParams.
/**
* tests validation of the params.
*/
@Test
public void testValidateParams() throws Exception {
OAuthProviderProcessingFilter filter = new OAuthProviderProcessingFilter() {
protected void onValidSignature(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
}
};
ConsumerDetails consumerDetails = mock(ConsumerDetails.class);
HashMap<String, String> params = new HashMap<String, String>();
params.put(OAuthConsumerParameter.oauth_version.toString(), "1.1");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials.");
} catch (OAuthVersionUnsupportedException e) {
params.remove(OAuthConsumerParameter.oauth_version.toString());
}
filter.getAuthenticationEntryPoint().setRealmName("anywho");
params.put("realm", "hello");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials.");
} catch (InvalidOAuthParametersException e) {
}
params.put("realm", "anywho");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for missing signature method.");
} catch (InvalidOAuthParametersException e) {
}
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for missing signature.");
} catch (InvalidOAuthParametersException e) {
}
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
params.put(OAuthConsumerParameter.oauth_signature.toString(), "value");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for missing timestamp.");
} catch (InvalidOAuthParametersException e) {
}
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
params.put(OAuthConsumerParameter.oauth_signature.toString(), "value");
params.put(OAuthConsumerParameter.oauth_timestamp.toString(), "value");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for missing nonce.");
} catch (InvalidOAuthParametersException e) {
}
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
params.put(OAuthConsumerParameter.oauth_signature.toString(), "value");
params.put(OAuthConsumerParameter.oauth_timestamp.toString(), "value");
params.put(OAuthConsumerParameter.oauth_nonce.toString(), "value");
try {
filter.validateOAuthParams(consumerDetails, params);
fail("should have thrown a bad credentials for bad timestamp.");
} catch (InvalidOAuthParametersException e) {
}
OAuthNonceServices nonceServices = mock(OAuthNonceServices.class);
filter.setNonceServices(nonceServices);
params.remove("realm");
params.put(OAuthConsumerParameter.oauth_signature_method.toString(), "sigmethod");
params.put(OAuthConsumerParameter.oauth_signature.toString(), "value");
params.put(OAuthConsumerParameter.oauth_timestamp.toString(), "1111111");
params.put(OAuthConsumerParameter.oauth_nonce.toString(), "value");
filter.validateOAuthParams(consumerDetails, params);
verify(nonceServices).validateNonce(consumerDetails, 1111111L, "value");
}
use of org.springframework.security.oauth.provider.OAuthVersionUnsupportedException in project spring-security-oauth by spring-projects.
the class OAuthProviderProcessingFilter method validateOAuthParams.
/**
* Validates the OAuth parameters for the given consumer. Base implementation validates only those parameters
* that are required for all OAuth requests. This includes the nonce and timestamp, but not the signature.
*
* @param consumerDetails The consumer details.
* @param oauthParams The OAuth parameters to validate.
* @throws InvalidOAuthParametersException If the OAuth parameters are invalid.
*/
protected void validateOAuthParams(ConsumerDetails consumerDetails, Map<String, String> oauthParams) throws InvalidOAuthParametersException {
String version = oauthParams.get(OAuthConsumerParameter.oauth_version.toString());
if ((version != null) && (!"1.0".equals(version))) {
throw new OAuthVersionUnsupportedException("Unsupported OAuth version: " + version);
}
String realm = oauthParams.get("realm");
realm = realm == null || "".equals(realm) ? null : realm;
if ((realm != null) && (!realm.equals(this.authenticationEntryPoint.getRealmName()))) {
throw new InvalidOAuthParametersException(messages.getMessage("OAuthProcessingFilter.incorrectRealm", new Object[] { realm, this.getAuthenticationEntryPoint().getRealmName() }, "Response realm name '{0}' does not match system realm name of '{1}'"));
}
String signatureMethod = oauthParams.get(OAuthConsumerParameter.oauth_signature_method.toString());
if (signatureMethod == null) {
throw new InvalidOAuthParametersException(messages.getMessage("OAuthProcessingFilter.missingSignatureMethod", "Missing signature method."));
}
String signature = oauthParams.get(OAuthConsumerParameter.oauth_signature.toString());
if (signature == null) {
throw new InvalidOAuthParametersException(messages.getMessage("OAuthProcessingFilter.missingSignature", "Missing signature."));
}
String timestamp = oauthParams.get(OAuthConsumerParameter.oauth_timestamp.toString());
if (timestamp == null) {
throw new InvalidOAuthParametersException(messages.getMessage("OAuthProcessingFilter.missingTimestamp", "Missing timestamp."));
}
String nonce = oauthParams.get(OAuthConsumerParameter.oauth_nonce.toString());
if (nonce == null) {
throw new InvalidOAuthParametersException(messages.getMessage("OAuthProcessingFilter.missingNonce", "Missing nonce."));
}
try {
getNonceServices().validateNonce(consumerDetails, Long.parseLong(timestamp), nonce);
} catch (NumberFormatException e) {
throw new InvalidOAuthParametersException(messages.getMessage("OAuthProcessingFilter.invalidTimestamp", new Object[] { timestamp }, "Timestamp must be a positive integer. Invalid value: {0}"));
}
validateAdditionalParameters(consumerDetails, oauthParams);
}
Aggregations