split maxkey-authentication-sms

This commit is contained in:
MaxKey
2023-01-03 15:08:07 +08:00
parent 46a57a1a9d
commit f4e36ad0da
23 changed files with 229 additions and 84 deletions

View File

@@ -18,11 +18,10 @@
package org.maxkey.autoconfigure; package org.maxkey.autoconfigure;
import org.maxkey.constants.ConstsPersistence; import org.maxkey.constants.ConstsPersistence;
import org.maxkey.password.onetimepwd.OtpAuthnService; import org.maxkey.password.onetimepwd.MailOtpAuthnService;
import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore; import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
import org.maxkey.persistence.redis.RedisConnectionFactory; import org.maxkey.persistence.redis.RedisConnectionFactory;
import org.maxkey.persistence.service.EmailSendersService; import org.maxkey.persistence.service.EmailSendersService;
import org.maxkey.persistence.service.SmsProviderService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@@ -37,21 +36,20 @@ public class OneTimePasswordAutoConfiguration implements InitializingBean {
LoggerFactory.getLogger(OneTimePasswordAutoConfiguration.class); LoggerFactory.getLogger(OneTimePasswordAutoConfiguration.class);
@Bean(name = "otpAuthnService") @Bean(name = "mailOtpAuthnService")
public OtpAuthnService otpAuthnService( public MailOtpAuthnService mailOtpAuthnService(
@Value("${maxkey.server.persistence}") int persistence, @Value("${maxkey.server.persistence}") int persistence,
SmsProviderService smsProviderService,
EmailSendersService emailSendersService, EmailSendersService emailSendersService,
RedisConnectionFactory redisConnFactory) { RedisConnectionFactory redisConnFactory) {
OtpAuthnService otpAuthnService = MailOtpAuthnService otpAuthnService =
new OtpAuthnService(smsProviderService,emailSendersService); new MailOtpAuthnService(emailSendersService);
if (persistence == ConstsPersistence.REDIS) { if (persistence == ConstsPersistence.REDIS) {
RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory); RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory);
otpAuthnService.setRedisOptTokenStore(redisOptTokenStore); otpAuthnService.setRedisOptTokenStore(redisOptTokenStore);
} }
_logger.debug("OneTimePasswordService {} inited." , _logger.debug("MailOtpAuthnService {} inited." ,
persistence == ConstsPersistence.REDIS ? "Redis" : "InMemory"); persistence == ConstsPersistence.REDIS ? "Redis" : "InMemory");
return otpAuthnService; return otpAuthnService;
} }

View File

@@ -0,0 +1,85 @@
/*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* 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
*
* 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.maxkey.password.onetimepwd;
import java.sql.Types;
import java.util.concurrent.TimeUnit;
import org.maxkey.configuration.EmailConfig;
import org.maxkey.constants.ConstsBoolean;
import org.maxkey.crypto.password.PasswordReciprocal;
import org.maxkey.entity.EmailSenders;
import org.maxkey.password.onetimepwd.impl.MailOtpAuthn;
import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
import org.maxkey.persistence.service.EmailSendersService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
public class MailOtpAuthnService {
protected static final Cache<String, AbstractOtpAuthn> otpAuthnStore =
Caffeine.newBuilder()
.expireAfterWrite(60, TimeUnit.MINUTES)
.build();
EmailSendersService emailSendersService;
RedisOtpTokenStore redisOptTokenStore;
public MailOtpAuthnService(EmailSendersService emailSendersService) {
this.emailSendersService = emailSendersService;
}
public MailOtpAuthnService(RedisOtpTokenStore redisOptTokenStore) {
this.redisOptTokenStore = redisOptTokenStore;
}
public AbstractOtpAuthn getMailOtpAuthn(String instId) {
AbstractOtpAuthn otpAuthn = otpAuthnStore.getIfPresent(instId);
if(otpAuthn == null) {
EmailSenders emailSender =
emailSendersService.findOne("where instid = ? ", new Object[]{instId}, new int[]{Types.VARCHAR});
String credentials = PasswordReciprocal.getInstance().decoder(emailSender.getCredentials());
EmailConfig emailConfig =
new EmailConfig(
emailSender.getAccount(),
credentials,
emailSender.getSmtpHost(),
emailSender.getPort(),
ConstsBoolean.isTrue(emailSender.getSslSwitch()),
emailSender.getSender());
MailOtpAuthn mailOtpAuthn = new MailOtpAuthn(emailConfig);
mailOtpAuthn.setInterval(60 * 5);//5 minute
if(redisOptTokenStore != null) {
mailOtpAuthn.setOptTokenStore(redisOptTokenStore);
}
otpAuthn = mailOtpAuthn;
}
otpAuthnStore.put(instId, otpAuthn);
return otpAuthn;
}
public void setRedisOptTokenStore(RedisOtpTokenStore redisOptTokenStore) {
this.redisOptTokenStore = redisOptTokenStore;
}
}

View File

@@ -11,5 +11,6 @@ dependencies {
implementation project(":maxkey-persistence") implementation project(":maxkey-persistence")
implementation project(":maxkey-authentications:maxkey-authentication-core") implementation project(":maxkey-authentications:maxkey-authentication-core")
implementation project(":maxkey-authentications:maxkey-authentication-otp") implementation project(":maxkey-authentications:maxkey-authentication-otp")
implementation project(":maxkey-authentications:maxkey-authentication-sms")
} }

View File

@@ -31,7 +31,7 @@ import org.maxkey.constants.ConstsLoginType;
import org.maxkey.constants.ConstsStatus; import org.maxkey.constants.ConstsStatus;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.password.onetimepwd.AbstractOtpAuthn; import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.maxkey.password.onetimepwd.OtpAuthnService; import org.maxkey.password.onetimepwd.MailOtpAuthnService;
import org.maxkey.web.WebConstants; import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext; import org.maxkey.web.WebContext;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -67,7 +67,7 @@ public abstract class AbstractAuthenticationProvider {
protected AbstractOtpAuthn tfaOtpAuthn; protected AbstractOtpAuthn tfaOtpAuthn;
protected OtpAuthnService otpAuthnService; protected MailOtpAuthnService otpAuthnService;
protected SessionManager sessionManager; protected SessionManager sessionManager;

View File

@@ -25,7 +25,7 @@ import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.constants.ConstsLoginType; import org.maxkey.constants.ConstsLoginType;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.password.onetimepwd.AbstractOtpAuthn; import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.maxkey.password.onetimepwd.OtpAuthnService; import org.maxkey.password.sms.SmsOtpAuthnService;
import org.maxkey.web.WebConstants; import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext; import org.maxkey.web.WebContext;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -46,6 +46,8 @@ public class MobileAuthenticationProvider extends AbstractAuthenticationProvider
private static final Logger _logger = private static final Logger _logger =
LoggerFactory.getLogger(MobileAuthenticationProvider.class); LoggerFactory.getLogger(MobileAuthenticationProvider.class);
SmsOtpAuthnService smsOtpAuthnService;
public String getProviderName() { public String getProviderName() {
return "mobile" + PROVIDER_SUFFIX; return "mobile" + PROVIDER_SUFFIX;
} }
@@ -59,11 +61,11 @@ public class MobileAuthenticationProvider extends AbstractAuthenticationProvider
public MobileAuthenticationProvider( public MobileAuthenticationProvider(
AbstractAuthenticationRealm authenticationRealm, AbstractAuthenticationRealm authenticationRealm,
ApplicationConfig applicationConfig, ApplicationConfig applicationConfig,
OtpAuthnService otpAuthnService, SmsOtpAuthnService smsOtpAuthnService,
SessionManager sessionManager) { SessionManager sessionManager) {
this.authenticationRealm = authenticationRealm; this.authenticationRealm = authenticationRealm;
this.applicationConfig = applicationConfig; this.applicationConfig = applicationConfig;
this.otpAuthnService = otpAuthnService; this.smsOtpAuthnService = smsOtpAuthnService;
this.sessionManager = sessionManager; this.sessionManager = sessionManager;
} }
@@ -136,7 +138,7 @@ public class MobileAuthenticationProvider extends AbstractAuthenticationProvider
UserInfo validUserInfo = new UserInfo(); UserInfo validUserInfo = new UserInfo();
validUserInfo.setUsername(userInfo.getUsername()); validUserInfo.setUsername(userInfo.getUsername());
validUserInfo.setId(userInfo.getId()); validUserInfo.setId(userInfo.getId());
AbstractOtpAuthn smsOtpAuthn = otpAuthnService.getByInstId(userInfo.getInstId()); AbstractOtpAuthn smsOtpAuthn = smsOtpAuthnService.getByInstId(userInfo.getInstId());
if (password == null || !smsOtpAuthn.validate(validUserInfo, password)) { if (password == null || !smsOtpAuthn.validate(validUserInfo, password)) {
String message = WebContext.getI18nValue("login.error.captcha"); String message = WebContext.getI18nValue("login.error.captcha");
_logger.debug("login captcha valid error."); _logger.debug("login captcha valid error.");

View File

@@ -28,15 +28,10 @@ import org.maxkey.authn.session.SessionManager;
import org.maxkey.authn.support.rememberme.AbstractRemeberMeManager; import org.maxkey.authn.support.rememberme.AbstractRemeberMeManager;
import org.maxkey.authn.support.rememberme.JdbcRemeberMeManager; import org.maxkey.authn.support.rememberme.JdbcRemeberMeManager;
import org.maxkey.configuration.ApplicationConfig; import org.maxkey.configuration.ApplicationConfig;
import org.maxkey.constants.ConstsPersistence; import org.maxkey.password.sms.SmsOtpAuthnService;
import org.maxkey.password.onetimepwd.OtpAuthnService;
import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
import org.maxkey.persistence.redis.RedisConnectionFactory;
import org.maxkey.persistence.repository.LoginHistoryRepository; import org.maxkey.persistence.repository.LoginHistoryRepository;
import org.maxkey.persistence.repository.LoginRepository; import org.maxkey.persistence.repository.LoginRepository;
import org.maxkey.persistence.repository.PasswordPolicyValidator; import org.maxkey.persistence.repository.PasswordPolicyValidator;
import org.maxkey.persistence.service.EmailSendersService;
import org.maxkey.persistence.service.SmsProviderService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
@@ -86,14 +81,14 @@ public class AuthnProviderAutoConfiguration implements InitializingBean {
public AbstractAuthenticationProvider mobileAuthenticationProvider( public AbstractAuthenticationProvider mobileAuthenticationProvider(
AbstractAuthenticationRealm authenticationRealm, AbstractAuthenticationRealm authenticationRealm,
ApplicationConfig applicationConfig, ApplicationConfig applicationConfig,
OtpAuthnService otpAuthnService, SmsOtpAuthnService smsAuthnService,
SessionManager sessionManager SessionManager sessionManager
) { ) {
_logger.debug("init Mobile authentication Provider ."); _logger.debug("init Mobile authentication Provider .");
return new MobileAuthenticationProvider( return new MobileAuthenticationProvider(
authenticationRealm, authenticationRealm,
applicationConfig, applicationConfig,
otpAuthnService, smsAuthnService,
sessionManager sessionManager
); );
} }

View File

@@ -0,0 +1,14 @@
description = "maxkey-authentication-sms"
dependencies {
//local jars
implementation fileTree(dir: '../maxkey-lib/', include: '*/*.jar')
implementation project(":maxkey-common")
implementation project(":maxkey-core")
implementation project(":maxkey-persistence")
implementation project(":maxkey-authentications:maxkey-authentication-otp")
}

View File

@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Class-Path:

View File

@@ -0,0 +1,63 @@
/*
* Copyright [2022] [MaxKey of copyright http://www.maxkey.top]
*
* 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
*
* 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.maxkey.autoconfigure;
import org.maxkey.constants.ConstsPersistence;
import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
import org.maxkey.password.sms.SmsOtpAuthnService;
import org.maxkey.persistence.redis.RedisConnectionFactory;
import org.maxkey.persistence.service.EmailSendersService;
import org.maxkey.persistence.service.SmsProviderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
@AutoConfiguration
public class SmsAutoConfiguration implements InitializingBean {
private static final Logger _logger =
LoggerFactory.getLogger(SmsAutoConfiguration.class);
@Bean(name = "smsOtpAuthnService")
public SmsOtpAuthnService smsOtpAuthnService(
@Value("${maxkey.server.persistence}") int persistence,
SmsProviderService smsProviderService,
EmailSendersService emailSendersService,
RedisConnectionFactory redisConnFactory) {
SmsOtpAuthnService smsOtpAuthnService =
new SmsOtpAuthnService(smsProviderService,emailSendersService);
if (persistence == ConstsPersistence.REDIS) {
RedisOtpTokenStore redisOptTokenStore = new RedisOtpTokenStore(redisConnFactory);
smsOtpAuthnService.setRedisOptTokenStore(redisOptTokenStore);
}
_logger.debug("SmsOtpAuthnService {} inited." ,
persistence == ConstsPersistence.REDIS ? "Redis" : "InMemory");
return smsOtpAuthnService;
}
@Override
public void afterPropertiesSet() throws Exception {
}
}

View File

@@ -15,7 +15,7 @@
*/ */
package org.maxkey.password.onetimepwd.impl; package org.maxkey.password.sms;
import java.io.IOException; import java.io.IOException;

View File

@@ -15,7 +15,7 @@
*/ */
package org.maxkey.password.onetimepwd; package org.maxkey.password.sms;
import java.sql.Types; import java.sql.Types;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -25,20 +25,21 @@ import org.maxkey.constants.ConstsBoolean;
import org.maxkey.crypto.password.PasswordReciprocal; import org.maxkey.crypto.password.PasswordReciprocal;
import org.maxkey.entity.EmailSenders; import org.maxkey.entity.EmailSenders;
import org.maxkey.entity.SmsProvider; import org.maxkey.entity.SmsProvider;
import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.maxkey.password.onetimepwd.impl.MailOtpAuthn; import org.maxkey.password.onetimepwd.impl.MailOtpAuthn;
import org.maxkey.password.onetimepwd.impl.sms.SmsOtpAuthnAliyun;
import org.maxkey.password.onetimepwd.impl.sms.SmsOtpAuthnTencentCloud;
import org.maxkey.password.onetimepwd.impl.sms.SmsOtpAuthnYunxin;
import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore; import org.maxkey.password.onetimepwd.token.RedisOtpTokenStore;
import org.maxkey.password.sms.impl.SmsOtpAuthnAliyun;
import org.maxkey.password.sms.impl.SmsOtpAuthnTencentCloud;
import org.maxkey.password.sms.impl.SmsOtpAuthnYunxin;
import org.maxkey.persistence.service.EmailSendersService; import org.maxkey.persistence.service.EmailSendersService;
import org.maxkey.persistence.service.SmsProviderService; import org.maxkey.persistence.service.SmsProviderService;
import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine; import com.github.benmanes.caffeine.cache.Caffeine;
public class OtpAuthnService { public class SmsOtpAuthnService {
protected static final Cache<String, AbstractOtpAuthn> otpAuthnStore = protected static final Cache<String, AbstractOtpAuthn> smsAuthnStore =
Caffeine.newBuilder() Caffeine.newBuilder()
.expireAfterWrite(60, TimeUnit.MINUTES) .expireAfterWrite(60, TimeUnit.MINUTES)
.build(); .build();
@@ -49,18 +50,19 @@ public class OtpAuthnService {
RedisOtpTokenStore redisOptTokenStore; RedisOtpTokenStore redisOptTokenStore;
public OtpAuthnService(SmsProviderService smsProviderService, EmailSendersService emailSendersService) { public SmsOtpAuthnService(SmsProviderService smsProviderService, EmailSendersService emailSendersService) {
this.smsProviderService = smsProviderService; this.smsProviderService = smsProviderService;
this.emailSendersService = emailSendersService; this.emailSendersService = emailSendersService;
} }
public OtpAuthnService(SmsProviderService smsProviderService,RedisOtpTokenStore redisOptTokenStore) { public SmsOtpAuthnService(SmsProviderService smsProviderService,EmailSendersService emailSendersService,RedisOtpTokenStore redisOptTokenStore) {
this.smsProviderService = smsProviderService; this.smsProviderService = smsProviderService;
this.emailSendersService = emailSendersService;
this.redisOptTokenStore = redisOptTokenStore; this.redisOptTokenStore = redisOptTokenStore;
} }
public AbstractOtpAuthn getByInstId(String instId) { public AbstractOtpAuthn getByInstId(String instId) {
AbstractOtpAuthn otpAuthn = otpAuthnStore.getIfPresent(instId); AbstractOtpAuthn otpAuthn = smsAuthnStore.getIfPresent(instId);
if(otpAuthn == null) { if(otpAuthn == null) {
SmsProvider smsProvider = SmsProvider smsProvider =
smsProviderService.findOne("where instid = ? ", new Object[]{instId}, new int[]{Types.VARCHAR}); smsProviderService.findOne("where instid = ? ", new Object[]{instId}, new int[]{Types.VARCHAR});
@@ -119,38 +121,12 @@ public class OtpAuthnService {
otpAuthn = mailOtpAuthn; otpAuthn = mailOtpAuthn;
} }
otpAuthnStore.put(instId, otpAuthn); smsAuthnStore.put(instId, otpAuthn);
} }
} }
return otpAuthn; return otpAuthn;
} }
public AbstractOtpAuthn getMailOtpAuthn(String instId) {
AbstractOtpAuthn otpAuthn = otpAuthnStore.getIfPresent(instId);
if(otpAuthn == null) {
EmailSenders emailSender =
emailSendersService.findOne("where instid = ? ", new Object[]{instId}, new int[]{Types.VARCHAR});
String credentials = PasswordReciprocal.getInstance().decoder(emailSender.getCredentials());
EmailConfig emailConfig =
new EmailConfig(
emailSender.getAccount(),
credentials,
emailSender.getSmtpHost(),
emailSender.getPort(),
ConstsBoolean.isTrue(emailSender.getSslSwitch()),
emailSender.getSender());
MailOtpAuthn mailOtpAuthn = new MailOtpAuthn(emailConfig);
mailOtpAuthn.setInterval(60 * 5);//5 minute
if(redisOptTokenStore != null) {
mailOtpAuthn.setOptTokenStore(redisOptTokenStore);
}
otpAuthn = mailOtpAuthn;
}
otpAuthnStore.put(instId, otpAuthn);
return otpAuthn;
}
public void setRedisOptTokenStore(RedisOtpTokenStore redisOptTokenStore) { public void setRedisOptTokenStore(RedisOtpTokenStore redisOptTokenStore) {
this.redisOptTokenStore = redisOptTokenStore; this.redisOptTokenStore = redisOptTokenStore;
} }

View File

@@ -15,7 +15,7 @@
*/ */
package org.maxkey.password.onetimepwd.impl.sms; package org.maxkey.password.sms.impl;
import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse; import com.aliyuncs.CommonResponse;
@@ -25,7 +25,7 @@ import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.DefaultProfile;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.password.onetimepwd.impl.SmsOtpAuthn; import org.maxkey.password.sms.SmsOtpAuthn;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@@ -15,7 +15,7 @@
*/ */
package org.maxkey.password.onetimepwd.impl.sms; package org.maxkey.password.sms.impl;
import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.ClientProfile;
@@ -25,7 +25,7 @@ import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse; import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.password.onetimepwd.impl.SmsOtpAuthn; import org.maxkey.password.sms.SmsOtpAuthn;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@@ -15,7 +15,7 @@
*/ */
package org.maxkey.password.onetimepwd.impl.sms; package org.maxkey.password.sms.impl;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.ArrayList; import java.util.ArrayList;
@@ -30,7 +30,7 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair; import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.password.onetimepwd.impl.SmsOtpAuthn; import org.maxkey.password.sms.SmsOtpAuthn;
import org.maxkey.util.JsonUtils; import org.maxkey.util.JsonUtils;
import org.maxkey.util.StringGenerator; import org.maxkey.util.StringGenerator;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@@ -13,6 +13,7 @@ dependencies {
implementation project(":maxkey-authentications:maxkey-authentication-captcha") implementation project(":maxkey-authentications:maxkey-authentication-captcha")
implementation project(":maxkey-authentications:maxkey-authentication-otp") implementation project(":maxkey-authentications:maxkey-authentication-otp")
implementation project(":maxkey-authentications:maxkey-authentication-provider") implementation project(":maxkey-authentications:maxkey-authentication-provider")
implementation project(":maxkey-authentications:maxkey-authentication-sms")
implementation project(":maxkey-protocols:maxkey-protocol-authorize") implementation project(":maxkey-protocols:maxkey-protocol-authorize")
implementation project(":maxkey-protocols:maxkey-protocol-cas") implementation project(":maxkey-protocols:maxkey-protocol-cas")

View File

@@ -31,7 +31,7 @@ import org.maxkey.authn.support.kerberos.RemoteKerberosService;
import org.maxkey.configuration.EmailConfig; import org.maxkey.configuration.EmailConfig;
import org.maxkey.constants.ConstsPersistence; import org.maxkey.constants.ConstsPersistence;
import org.maxkey.password.onetimepwd.AbstractOtpAuthn; import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.maxkey.password.onetimepwd.OtpAuthnService; import org.maxkey.password.onetimepwd.MailOtpAuthnService;
import org.maxkey.password.onetimepwd.algorithm.OtpKeyUriFormat; import org.maxkey.password.onetimepwd.algorithm.OtpKeyUriFormat;
import org.maxkey.password.onetimepwd.impl.MailOtpAuthn; import org.maxkey.password.onetimepwd.impl.MailOtpAuthn;
import org.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn; import org.maxkey.password.onetimepwd.impl.TimeBasedOtpAuthn;
@@ -104,7 +104,7 @@ public class MaxKeyConfig implements InitializingBean {
LoginHistoryRepository loginHistoryService, LoginHistoryRepository loginHistoryService,
UserInfoService userInfoService, UserInfoService userInfoService,
JdbcTemplate jdbcTemplate, JdbcTemplate jdbcTemplate,
OtpAuthnService otpAuthnService, MailOtpAuthnService otpAuthnService,
LdapContextService ldapContextService) { LdapContextService ldapContextService) {
LdapAuthenticationRealmService ldapRealmService = new LdapAuthenticationRealmService(ldapContextService); LdapAuthenticationRealmService ldapRealmService = new LdapAuthenticationRealmService(ldapContextService);
JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm( JdbcAuthenticationRealm authenticationRealm = new JdbcAuthenticationRealm(

View File

@@ -26,12 +26,12 @@ import org.maxkey.entity.ChangePassword;
import org.maxkey.entity.Message; import org.maxkey.entity.Message;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.password.onetimepwd.AbstractOtpAuthn; import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.maxkey.password.onetimepwd.OtpAuthnService; import org.maxkey.password.onetimepwd.MailOtpAuthnService;
import org.maxkey.password.sms.SmsOtpAuthnService;
import org.maxkey.persistence.service.UserInfoService; import org.maxkey.persistence.service.UserInfoService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
@@ -74,8 +74,10 @@ public class ForgotPasswordContorller {
UserInfoService userInfoService; UserInfoService userInfoService;
@Autowired @Autowired
@Qualifier("otpAuthnService") MailOtpAuthnService mailOtpAuthnService;
OtpAuthnService otpAuthnService;
@Autowired
SmsOtpAuthnService smsOtpAuthnService;
@@ -100,7 +102,7 @@ public class ForgotPasswordContorller {
if(userInfo != null) { if(userInfo != null) {
change = new ChangePassword(userInfo); change = new ChangePassword(userInfo);
change.clearPassword(); change.clearPassword();
AbstractOtpAuthn smsOtpAuthn = otpAuthnService.getByInstId(userInfo.getInstId()); AbstractOtpAuthn smsOtpAuthn = smsOtpAuthnService.getByInstId(userInfo.getInstId());
smsOtpAuthn.produce(userInfo); smsOtpAuthn.produce(userInfo);
return new Message<ChangePassword>(change).buildResponse(); return new Message<ChangePassword>(change).buildResponse();
} }
@@ -127,7 +129,7 @@ public class ForgotPasswordContorller {
if(userInfo != null) { if(userInfo != null) {
change = new ChangePassword(userInfo); change = new ChangePassword(userInfo);
change.clearPassword(); change.clearPassword();
AbstractOtpAuthn mailOtpAuthn = otpAuthnService.getMailOtpAuthn(userInfo.getInstId()); AbstractOtpAuthn mailOtpAuthn = mailOtpAuthnService.getMailOtpAuthn(userInfo.getInstId());
mailOtpAuthn.produce(userInfo); mailOtpAuthn.produce(userInfo);
return new Message<ChangePassword>(change).buildResponse(); return new Message<ChangePassword>(change).buildResponse();
} }
@@ -146,8 +148,8 @@ public class ForgotPasswordContorller {
&& changePassword.getPassword().equals(changePassword.getConfirmPassword())) { && changePassword.getPassword().equals(changePassword.getConfirmPassword())) {
UserInfo loadedUserInfo = userInfoService.get(changePassword.getUserId()); UserInfo loadedUserInfo = userInfoService.get(changePassword.getUserId());
if(loadedUserInfo != null) { if(loadedUserInfo != null) {
AbstractOtpAuthn smsOtpAuthn = otpAuthnService.getByInstId(loadedUserInfo.getInstId()); AbstractOtpAuthn smsOtpAuthn = smsOtpAuthnService.getByInstId(loadedUserInfo.getInstId());
AbstractOtpAuthn mailOtpAuthn = otpAuthnService.getMailOtpAuthn(loadedUserInfo.getInstId()); AbstractOtpAuthn mailOtpAuthn = mailOtpAuthnService.getMailOtpAuthn(loadedUserInfo.getInstId());
if ( if (
(forgotType.equalsIgnoreCase("email") (forgotType.equalsIgnoreCase("email")
&& mailOtpAuthn !=null && mailOtpAuthn !=null

View File

@@ -38,7 +38,8 @@ import org.maxkey.entity.Institutions;
import org.maxkey.entity.Message; import org.maxkey.entity.Message;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.password.onetimepwd.AbstractOtpAuthn; import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.maxkey.password.onetimepwd.OtpAuthnService; import org.maxkey.password.onetimepwd.MailOtpAuthnService;
import org.maxkey.password.sms.SmsOtpAuthnService;
import org.maxkey.persistence.service.UserInfoService; import org.maxkey.persistence.service.UserInfoService;
import org.maxkey.web.WebConstants; import org.maxkey.web.WebConstants;
import org.maxkey.web.WebContext; import org.maxkey.web.WebContext;
@@ -91,7 +92,9 @@ public class LoginEntryPoint {
AbstractOtpAuthn tfaOtpAuthn; AbstractOtpAuthn tfaOtpAuthn;
@Autowired @Autowired
OtpAuthnService otpAuthnService; SmsOtpAuthnService smsAuthnService;
@Autowired @Autowired
AbstractRemeberMeManager remeberMeManager; AbstractRemeberMeManager remeberMeManager;
@@ -156,7 +159,7 @@ public class LoginEntryPoint {
public ResponseEntity<?> produceOtp(@PathVariable("mobile") String mobile) { public ResponseEntity<?> produceOtp(@PathVariable("mobile") String mobile) {
UserInfo userInfo=userInfoService.findByEmailMobile(mobile); UserInfo userInfo=userInfoService.findByEmailMobile(mobile);
if(userInfo != null) { if(userInfo != null) {
otpAuthnService.getByInstId(WebContext.getInst().getId()).produce(userInfo); smsAuthnService.getByInstId(WebContext.getInst().getId()).produce(userInfo);
return new Message<AuthJwt>(Message.SUCCESS).buildResponse(); return new Message<AuthJwt>(Message.SUCCESS).buildResponse();
} }

View File

@@ -29,14 +29,13 @@ import org.maxkey.crypto.password.PasswordReciprocal;
import org.maxkey.entity.Message; import org.maxkey.entity.Message;
import org.maxkey.entity.UserInfo; import org.maxkey.entity.UserInfo;
import org.maxkey.password.onetimepwd.AbstractOtpAuthn; import org.maxkey.password.onetimepwd.AbstractOtpAuthn;
import org.maxkey.password.onetimepwd.OtpAuthnService; import org.maxkey.password.sms.SmsOtpAuthnService;
import org.maxkey.persistence.service.UserInfoService; import org.maxkey.persistence.service.UserInfoService;
import org.maxkey.util.StringUtils; import org.maxkey.util.StringUtils;
import org.maxkey.web.WebContext; import org.maxkey.web.WebContext;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
@@ -64,8 +63,7 @@ public class RegisterController {
private UserInfoService userInfoService; private UserInfoService userInfoService;
@Autowired @Autowired
@Qualifier("otpAuthnService") SmsOtpAuthnService smsOtpAuthnService;
OtpAuthnService otpAuthnService;
@Autowired @Autowired
private PasswordEncoder passwordEncoder; private PasswordEncoder passwordEncoder;
@@ -81,7 +79,7 @@ public class RegisterController {
UserInfo userInfo = new UserInfo(); UserInfo userInfo = new UserInfo();
userInfo.setUsername(mobile); userInfo.setUsername(mobile);
userInfo.setMobile(mobile); userInfo.setMobile(mobile);
AbstractOtpAuthn smsOtpAuthn = otpAuthnService.getByInstId(WebContext.getInst().getId()); AbstractOtpAuthn smsOtpAuthn = smsOtpAuthnService.getByInstId(WebContext.getInst().getId());
smsOtpAuthn.produce(userInfo); smsOtpAuthn.produce(userInfo);
return new Message<UserInfo>(userInfo).buildResponse(); return new Message<UserInfo>(userInfo).buildResponse();
} }
@@ -98,7 +96,7 @@ public class RegisterController {
UserInfo validateUserInfo = new UserInfo(); UserInfo validateUserInfo = new UserInfo();
validateUserInfo.setUsername(userInfo.getMobile()); validateUserInfo.setUsername(userInfo.getMobile());
validateUserInfo.setMobile(userInfo.getMobile()); validateUserInfo.setMobile(userInfo.getMobile());
AbstractOtpAuthn smsOtpAuthn = otpAuthnService.getByInstId(WebContext.getInst().getId()); AbstractOtpAuthn smsOtpAuthn = smsOtpAuthnService.getByInstId(WebContext.getInst().getId());
if (smsOtpAuthn !=null if (smsOtpAuthn !=null
&& smsOtpAuthn.validate(validateUserInfo, captcha)){ && smsOtpAuthn.validate(validateUserInfo, captcha)){
UserInfo temp = userInfoService.findByEmailMobile(userInfo.getEmail()); UserInfo temp = userInfoService.findByEmailMobile(userInfo.getEmail());

View File

@@ -5,6 +5,7 @@ org.maxkey.autoconfigure.RedisAutoConfiguration
org.maxkey.autoconfigure.AuthnProviderAutoConfiguration org.maxkey.autoconfigure.AuthnProviderAutoConfiguration
org.maxkey.autoconfigure.JwtAuthnAutoConfiguration org.maxkey.autoconfigure.JwtAuthnAutoConfiguration
org.maxkey.autoconfigure.OneTimePasswordAutoConfiguration org.maxkey.autoconfigure.OneTimePasswordAutoConfiguration
org.maxkey.autoconfigure.SmsAutoConfiguration
org.maxkey.autoconfigure.SessionAutoConfiguration org.maxkey.autoconfigure.SessionAutoConfiguration
org.maxkey.autoconfigure.TokenAutoConfiguration org.maxkey.autoconfigure.TokenAutoConfiguration
org.maxkey.autoconfigure.CasAutoConfiguration org.maxkey.autoconfigure.CasAutoConfiguration

View File

@@ -12,6 +12,7 @@ dependencies {
implementation project(":maxkey-authentications:maxkey-authentication-captcha") implementation project(":maxkey-authentications:maxkey-authentication-captcha")
implementation project(":maxkey-authentications:maxkey-authentication-otp") implementation project(":maxkey-authentications:maxkey-authentication-otp")
implementation project(":maxkey-authentications:maxkey-authentication-provider") implementation project(":maxkey-authentications:maxkey-authentication-provider")
implementation project(":maxkey-authentications:maxkey-authentication-sms")
implementation project(":maxkey-protocols:maxkey-protocol-oauth-2.0") implementation project(":maxkey-protocols:maxkey-protocol-oauth-2.0")
implementation project(":maxkey-protocols:maxkey-protocol-saml-2.0") implementation project(":maxkey-protocols:maxkey-protocol-saml-2.0")

View File

@@ -5,6 +5,7 @@ org.maxkey.autoconfigure.JwtAuthnAutoConfiguration
org.maxkey.autoconfigure.RedisAutoConfiguration org.maxkey.autoconfigure.RedisAutoConfiguration
org.maxkey.autoconfigure.AuthnProviderAutoConfiguration org.maxkey.autoconfigure.AuthnProviderAutoConfiguration
org.maxkey.autoconfigure.OneTimePasswordAutoConfiguration org.maxkey.autoconfigure.OneTimePasswordAutoConfiguration
org.maxkey.autoconfigure.SmsAutoConfiguration
org.maxkey.autoconfigure.SessionAutoConfiguration org.maxkey.autoconfigure.SessionAutoConfiguration
org.maxkey.autoconfigure.TokenAutoConfiguration org.maxkey.autoconfigure.TokenAutoConfiguration
org.maxkey.autoconfigure.SynchronizerAutoConfiguration org.maxkey.autoconfigure.SynchronizerAutoConfiguration

View File

@@ -31,6 +31,7 @@ include (
'maxkey-authentications:maxkey-authentication-social', 'maxkey-authentications:maxkey-authentication-social',
'maxkey-authentications:maxkey-authentication-otp', 'maxkey-authentications:maxkey-authentication-otp',
'maxkey-authentications:maxkey-authentication-provider', 'maxkey-authentications:maxkey-authentication-provider',
'maxkey-authentications:maxkey-authentication-sms',
//identity //identity
'maxkey-identitys:maxkey-identity-scim', 'maxkey-identitys:maxkey-identity-scim',
'maxkey-identitys:maxkey-identity-rest', 'maxkey-identitys:maxkey-identity-rest',