2020-04-24 03:45:34 +07:00
|
|
|
/*
|
|
|
|
* Copyright 2020 the original author or authors.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.springframework.security.oauth2.server.authorization.authentication;
|
|
|
|
|
|
|
|
import org.springframework.security.authentication.AuthenticationProvider;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.AuthenticationException;
|
2020-04-27 22:10:49 +07:00
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2Error;
|
|
|
|
import org.springframework.security.oauth2.core.OAuth2ErrorCodes;
|
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClient;
|
2020-04-24 03:45:34 +07:00
|
|
|
import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository;
|
2020-04-27 22:10:49 +07:00
|
|
|
import org.springframework.util.Assert;
|
2020-04-24 03:45:34 +07:00
|
|
|
|
|
|
|
/**
|
2020-05-29 22:05:00 +07:00
|
|
|
* An {@link AuthenticationProvider} implementation that validates {@link OAuth2ClientAuthenticationToken}'s.
|
2020-04-27 22:10:49 +07:00
|
|
|
*
|
2020-04-24 03:45:34 +07:00
|
|
|
* @author Joe Grandja
|
2020-04-27 22:10:49 +07:00
|
|
|
* @author Patryk Kostrzewa
|
2020-05-29 22:05:00 +07:00
|
|
|
* @since 0.0.1
|
|
|
|
* @see AuthenticationProvider
|
|
|
|
* @see OAuth2ClientAuthenticationToken
|
|
|
|
* @see RegisteredClientRepository
|
2020-04-24 03:45:34 +07:00
|
|
|
*/
|
|
|
|
public class OAuth2ClientAuthenticationProvider implements AuthenticationProvider {
|
2020-04-27 22:10:49 +07:00
|
|
|
private final RegisteredClientRepository registeredClientRepository;
|
|
|
|
|
|
|
|
/**
|
2020-05-29 22:05:00 +07:00
|
|
|
* Constructs an {@code OAuth2ClientAuthenticationProvider} using the provided parameters.
|
|
|
|
*
|
|
|
|
* @param registeredClientRepository the repository of registered clients
|
2020-04-27 22:10:49 +07:00
|
|
|
*/
|
|
|
|
public OAuth2ClientAuthenticationProvider(RegisteredClientRepository registeredClientRepository) {
|
|
|
|
Assert.notNull(registeredClientRepository, "registeredClientRepository cannot be null");
|
|
|
|
this.registeredClientRepository = registeredClientRepository;
|
|
|
|
}
|
2020-04-24 03:45:34 +07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
2020-05-29 22:05:00 +07:00
|
|
|
String clientId = authentication.getPrincipal().toString();
|
2020-04-27 22:10:49 +07:00
|
|
|
RegisteredClient registeredClient = this.registeredClientRepository.findByClientId(clientId);
|
|
|
|
if (registeredClient == null) {
|
|
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
|
|
|
|
}
|
|
|
|
|
2020-05-29 22:05:00 +07:00
|
|
|
String clientSecret = authentication.getCredentials().toString();
|
|
|
|
if (!registeredClient.getClientSecret().equals(clientSecret)) {
|
2020-04-27 22:10:49 +07:00
|
|
|
throw new OAuth2AuthenticationException(new OAuth2Error(OAuth2ErrorCodes.INVALID_CLIENT));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new OAuth2ClientAuthenticationToken(registeredClient);
|
2020-04-24 03:45:34 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supports(Class<?> authentication) {
|
|
|
|
return OAuth2ClientAuthenticationToken.class.isAssignableFrom(authentication);
|
|
|
|
}
|
|
|
|
}
|