All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.herodotus.stirrup.oauth2.data.jpa.service.HerodotusAuthorizationService Maven / Gradle / Ivy

/*
 * Copyright (c) 2020-2030 郑庚伟 ZHENGGENGWEI (码匠君) ([email protected] & www.herodotus.cn)
 *
 * Dante Engine licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 
 *
 * 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 cn.herodotus.stirrup.oauth2.data.jpa.service;

import cn.herodotus.stirrup.data.crud.repository.BaseJpaRepository;
import cn.herodotus.stirrup.data.crud.service.AbstractJpaService;
import cn.herodotus.stirrup.oauth2.data.jpa.entity.HerodotusAuthorization;
import cn.herodotus.stirrup.oauth2.data.jpa.repository.HerodotusAuthorizationRepository;
import jakarta.persistence.criteria.Predicate;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 

Description: HerodotusAuthorizationService

*

* 这里命名没有按照统一的习惯,主要是为了防止与 spring-authorization-server 已有类的同名而导致Bean注入失败 * * @author : gengwei.zheng * @date : 2022/2/25 21:06 */ @Service public class HerodotusAuthorizationService extends AbstractJpaService { private static final Logger log = LoggerFactory.getLogger(HerodotusAuthorizationService.class); private final HerodotusAuthorizationRepository herodotusAuthorizationRepository; @Autowired public HerodotusAuthorizationService(HerodotusAuthorizationRepository herodotusAuthorizationRepository) { this.herodotusAuthorizationRepository = herodotusAuthorizationRepository; } @Override public BaseJpaRepository getRepository() { return this.herodotusAuthorizationRepository; } public Optional findByState(String state) { Optional result = this.herodotusAuthorizationRepository.findByState(state); log.debug("[Herodotus] |- HerodotusAuthorization Service findByState."); return result; } public Optional findByAuthorizationCode(String authorizationCode) { Optional result = this.herodotusAuthorizationRepository.findByAuthorizationCodeValue(authorizationCode); log.debug("[Herodotus] |- HerodotusAuthorization Service findByAuthorizationCode."); return result; } public Optional findByAccessToken(String accessToken) { Optional result = this.herodotusAuthorizationRepository.findByAccessTokenValue(accessToken); log.debug("[Herodotus] |- HerodotusAuthorization Service findByAccessToken."); return result; } public Optional findByRefreshToken(String refreshToken) { Optional result = this.herodotusAuthorizationRepository.findByRefreshTokenValue(refreshToken); log.debug("[Herodotus] |- HerodotusAuthorization Service findByRefreshToken."); return result; } public Optional findByOidcIdTokenValue(String idToken) { Optional result = this.herodotusAuthorizationRepository.findByOidcIdTokenValue(idToken); log.debug("[Herodotus] |- HerodotusAuthorization Service findByOidcIdTokenValue."); return result; } public Optional findByUserCodeValue(String userCode) { Optional result = this.herodotusAuthorizationRepository.findByUserCodeValue(userCode); log.debug("[Herodotus] |- HerodotusAuthorization Service findByUserCodeValue."); return result; } public Optional findByDeviceCodeValue(String deviceCode) { Optional result = this.herodotusAuthorizationRepository.findByDeviceCodeValue(deviceCode); log.debug("[Herodotus] |- HerodotusAuthorization Service findByDeviceCodeValue."); return result; } public Optional findByStateOrAuthorizationCodeValueOrAccessTokenValueOrRefreshTokenValueOrOidcIdTokenValueOrUserCodeValueOrDeviceCodeValue(String token) { Specification specification = (root, criteriaQuery, criteriaBuilder) -> { List predicates = new ArrayList<>(); predicates.add(criteriaBuilder.equal(root.get("state"), token)); predicates.add(criteriaBuilder.equal(root.get("authorizationCodeValue"), token)); predicates.add(criteriaBuilder.equal(root.get("accessTokenValue"), token)); predicates.add(criteriaBuilder.equal(root.get("refreshTokenValue"), token)); predicates.add(criteriaBuilder.equal(root.get("oidcIdTokenValue"), token)); predicates.add(criteriaBuilder.equal(root.get("userCodeValue"), token)); predicates.add(criteriaBuilder.equal(root.get("deviceCodeValue"), token)); Predicate[] predicateArray = new Predicate[predicates.size()]; criteriaQuery.where(criteriaBuilder.or(predicates.toArray(predicateArray))); return criteriaQuery.getRestriction(); }; Optional result = this.herodotusAuthorizationRepository.findOne(specification); log.trace("[Herodotus] |- HerodotusAuthorization Service findByDetection."); return result; } public void clearHistoryToken() { this.herodotusAuthorizationRepository.deleteByRefreshTokenExpiresAtBefore(LocalDateTime.now()); log.debug("[Herodotus] |- HerodotusAuthorization Service clearExpireAccessToken."); } public List findAvailableAuthorizations(String registeredClientId, String principalName) { List authorizations = this.herodotusAuthorizationRepository.findAllByRegisteredClientIdAndPrincipalNameAndAccessTokenExpiresAtAfter(registeredClientId, principalName, LocalDateTime.now()); log.debug("[Herodotus] |- HerodotusAuthorization Service findAvailableAuthorizations."); return authorizations; } public int findAuthorizationCount(String registeredClientId, String principalName) { List authorizations = findAvailableAuthorizations(registeredClientId, principalName); int count = 0; if (CollectionUtils.isNotEmpty(authorizations)) { count = authorizations.size(); } log.debug("[Herodotus] |- HerodotusAuthorization Service current authorization count is [{}].", count); return count; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy