FINERACT-724 fix oauth authentication, replace DefaultPasswordEncoderFactories with standard spring security PasswordEncoderFactories

This commit is contained in:
ibondarenko
2019-11-22 18:28:10 +02:00
parent 1e0e6ca3e7
commit 347108b382
7 changed files with 34 additions and 125 deletions
@@ -1,54 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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.apache.fineract.infrastructure.core.domain;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.DelegatingPasswordEncoder;
import org.springframework.security.crypto.password.Md4PasswordEncoder;
import org.springframework.security.crypto.password.MessageDigestPasswordEncoder;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
import java.util.HashMap;
import java.util.Map;
public class DefaultPasswordEncoderFactories {
@SuppressWarnings("deprecation")
public static PasswordEncoder createDelegatingPasswordEncoder() {
String encodingId = "bcrypt";
Map<String, PasswordEncoder> encoders = new HashMap<>();
encoders.put(encodingId, new BCryptPasswordEncoder());
encoders.put("ldap", new org.springframework.security.crypto.password.LdapShaPasswordEncoder());
encoders.put("MD4", new Md4PasswordEncoder());
encoders.put("MD5", new MessageDigestPasswordEncoder("MD5"));
encoders.put("noop", NoOpPasswordEncoder.getInstance());
encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
encoders.put("scrypt", new SCryptPasswordEncoder());
encoders.put("SHA-1", new MessageDigestPasswordEncoder("SHA-1"));
encoders.put("SHA-256", new MessageDigestPasswordEncoder("SHA-256"));
DelegatingPasswordEncoder delegatingPasswordEncoder = new DelegatingPasswordEncoder(encodingId, encoders);
delegatingPasswordEncoder.setDefaultPasswordEncoderForMatches(new MessageDigestPasswordEncoder("SHA-256"));
return delegatingPasswordEncoder;
}
}
@@ -1,33 +0,0 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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.apache.fineract.infrastructure.core.security;
import org.apache.fineract.infrastructure.core.domain.DefaultPasswordEncoderFactories;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
public class ApplicationSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return DefaultPasswordEncoderFactories.createDelegatingPasswordEncoder();
}
}
@@ -18,6 +18,7 @@
*/ */
package org.apache.fineract.organisation.office.domain; package org.apache.fineract.organisation.office.domain;
import java.io.Serializable;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
@@ -45,7 +46,7 @@ import org.joda.time.LocalDate;
@Entity @Entity
@Table(name = "m_office", uniqueConstraints = { @UniqueConstraint(columnNames = { "name" }, name = "name_org"), @Table(name = "m_office", uniqueConstraints = { @UniqueConstraint(columnNames = { "name" }, name = "name_org"),
@UniqueConstraint(columnNames = { "external_id" }, name = "externalid_org") }) @UniqueConstraint(columnNames = { "external_id" }, name = "externalid_org") })
public class Office extends AbstractPersistableCustom<Long> { public class Office extends AbstractPersistableCustom<Long> implements Serializable {
@OneToMany(fetch = FetchType.LAZY) @OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id") @JoinColumn(name = "parent_id")
@@ -24,9 +24,11 @@ import javax.persistence.Table;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom; import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import java.io.Serializable;
@Entity @Entity
@Table(name = "m_permission") @Table(name = "m_permission")
public class Permission extends AbstractPersistableCustom<Long> { public class Permission extends AbstractPersistableCustom<Long> implements Serializable {
@Column(name = "grouping", nullable = false, length = 45) @Column(name = "grouping", nullable = false, length = 45)
private String grouping; private String grouping;
@@ -18,6 +18,7 @@
*/ */
package org.apache.fineract.useradministration.domain; package org.apache.fineract.useradministration.domain;
import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@@ -39,7 +40,7 @@ import org.apache.fineract.useradministration.data.RoleData;
@Entity @Entity
@Table(name = "m_role", uniqueConstraints = { @UniqueConstraint(columnNames = { "name" }, name = "unq_name") }) @Table(name = "m_role", uniqueConstraints = { @UniqueConstraint(columnNames = { "name" }, name = "unq_name") })
public class Role extends AbstractPersistableCustom<Long> { public class Role extends AbstractPersistableCustom<Long> implements Serializable {
@Column(name = "name", unique = true, nullable = false, length = 100) @Column(name = "name", unique = true, nullable = false, length = 100)
private String name; private String name;
@@ -69,9 +69,9 @@
<beans:property name="realmName" value="Fineract Platform API" /> <beans:property name="realmName" value="Fineract Platform API" />
</beans:bean> </beans:bean>
<beans:bean id="passwordEncoder" class="org.apache.fineract.infrastructure.core.domain.DefaultPasswordEncoderFactories" <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.factory.PasswordEncoderFactories"
factory-method="createDelegatingPasswordEncoder"> factory-method="createDelegatingPasswordEncoder">
</beans:bean> </beans:bean>
<beans:bean id="customAuthenticationProvider" <beans:bean id="customAuthenticationProvider"
class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
@@ -132,7 +132,7 @@
<access-denied-handler ref="oauthAccessDeniedHandler" /> <access-denied-handler ref="oauthAccessDeniedHandler" />
</http> </http>
<beans:bean id="passwordEncoder" class="org.apache.fineract.infrastructure.core.domain.DefaultPasswordEncoderFactories" <beans:bean id="passwordEncoder" class="org.springframework.security.crypto.factory.PasswordEncoderFactories"
factory-method="createDelegatingPasswordEncoder"> factory-method="createDelegatingPasswordEncoder">
</beans:bean> </beans:bean>
@@ -144,12 +144,10 @@
<beans:bean id="oauthAccessDeniedHandler" <beans:bean id="oauthAccessDeniedHandler"
class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" /> class="org.springframework.security.oauth2.provider.error.OAuth2AccessDeniedHandler" />
<beans:bean id="clientCredentialsTokenEndpointFilter" <beans:bean id="clientCredentialsTokenEndpointFilter"
class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter"> class="org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter">
<beans:constructor-arg value="/api/oauth/token" /> <beans:constructor-arg value="/api/oauth/token" />
<beans:property name="authenticationManager" ref="clientAuthenticationManager" /> <beans:property name="authenticationManager" ref="clientAuthenticationManager" />
</beans:bean> </beans:bean>
<beans:bean id="accessDecisionManager" <beans:bean id="accessDecisionManager"
@@ -169,14 +167,9 @@
</beans:constructor-arg> </beans:constructor-arg>
</beans:bean> </beans:bean>
<authentication-manager id="clientAuthenticationManager" erase-credentials="false">
<authentication-manager id="clientAuthenticationManager">
<authentication-provider user-service-ref="clientDetailsUserService" /> <authentication-provider user-service-ref="clientDetailsUserService" />
</authentication-manager> <authentication-provider ref="customAuthenticationProvider" />
<authentication-manager alias="authenticationManager"
erase-credentials="false">
<authentication-provider ref="customAuthenticationProvider" />
</authentication-manager> </authentication-manager>
<beans:bean id="clientDetailsUserService" <beans:bean id="clientDetailsUserService"
@@ -195,7 +188,6 @@
<beans:constructor-arg ref="routingDataSource" /> <beans:constructor-arg ref="routingDataSource" />
</beans:bean> </beans:bean>
<beans:bean id="tokenStore" <beans:bean id="tokenStore"
class="org.springframework.security.oauth2.provider.token.store.JdbcTokenStore"> class="org.springframework.security.oauth2.provider.token.store.JdbcTokenStore">
<beans:constructor-arg ref="routingDataSource" /> <beans:constructor-arg ref="routingDataSource" />
@@ -232,8 +224,7 @@
<oauth:password /> <oauth:password />
</oauth:authorization-server> </oauth:authorization-server>
<oauth:resource-server id="resourceServerFilter" <oauth:resource-server id="resourceServerFilter" stateless="false"
token-services-ref="tokenServices" /> token-services-ref="tokenServices" />
</beans:beans> </beans:beans>
</beans:beans> </beans:beans>
@@ -17,4 +17,5 @@
-- under the License. -- under the License.
-- --
UPDATE m_appuser SET password = CONCAT('{', id, '}', password) WHERE password NOT LIKE '{%'; UPDATE m_appuser SET password = CONCAT('{SHA-256}{', id, '}', password) WHERE password NOT LIKE '{%';
UPDATE oauth_client_details SET client_secret = CONCAT('{SHA-256}', SHA2(client_secret, 256)) WHERE client_secret NOT LIKE '{%';