cn.herodotus.engine.access.sms.processor.PhoneNumberAccessHandler Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2020-2030 郑庚伟 ZHENGGENGWEI (码匠君), Licensed under the AGPL License
*
* This file is part of Herodotus Engine.
*
* Herodotus Engine is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Herodotus Engine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package cn.herodotus.engine.access.sms.processor;
import cn.herodotus.engine.access.core.definition.AccessHandler;
import cn.herodotus.engine.access.core.definition.AccessResponse;
import cn.herodotus.engine.access.core.definition.AccessUserDetails;
import cn.herodotus.engine.access.core.exception.AccessIdentityVerificationFailedException;
import cn.herodotus.engine.access.sms.stamp.VerificationCodeStampManager;
import cn.herodotus.engine.assistant.definition.domain.oauth2.AccessPrincipal;
import cn.herodotus.engine.assistant.definition.constants.BaseConstants;
import cn.herodotus.engine.cache.core.exception.StampHasExpiredException;
import cn.herodotus.engine.cache.core.exception.StampMismatchException;
import cn.herodotus.engine.cache.core.exception.StampParameterIllegalException;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import java.util.LinkedHashMap;
/**
* Description: 手机短信接入处理器
*
* @author : gengwei.zheng
* @date : 2022/1/26 11:46
*/
public class PhoneNumberAccessHandler implements AccessHandler {
private final VerificationCodeStampManager verificationCodeStampManager;
public PhoneNumberAccessHandler(VerificationCodeStampManager verificationCodeStampManager) {
this.verificationCodeStampManager = verificationCodeStampManager;
}
@Override
public AccessResponse preProcess(String phone, String... params) {
String code = verificationCodeStampManager.create(phone);
boolean result;
if (verificationCodeStampManager.getSandbox()) {
result = true;
} else {
SmsBlend smsBlend = SmsFactory.getSmsBlend();
LinkedHashMap message = new LinkedHashMap<>();
message.put(BaseConstants.CODE, code);
SmsResponse smsResponse = smsBlend.sendMessage(phone, verificationCodeStampManager.getVerificationCodeTemplateId(), message);
result = smsResponse.isSuccess();
}
AccessResponse accessResponse = new AccessResponse();
accessResponse.setSuccess(result);
return accessResponse;
}
@Override
public AccessUserDetails loadUserDetails(String source, AccessPrincipal accessPrincipal) {
try {
verificationCodeStampManager.check(accessPrincipal.getMobile(), accessPrincipal.getCode());
AccessUserDetails accessUserDetails = new AccessUserDetails();
accessUserDetails.setUuid(accessPrincipal.getMobile());
accessUserDetails.setPhoneNumber(accessPrincipal.getMobile());
accessUserDetails.setUsername(accessPrincipal.getMobile());
accessUserDetails.setSource(source);
verificationCodeStampManager.delete(accessPrincipal.getMobile());
return accessUserDetails;
} catch (StampParameterIllegalException | StampMismatchException | StampHasExpiredException e) {
throw new AccessIdentityVerificationFailedException("Phone Verification Code Error!");
}
}
}