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

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

There is a newer version: 3.3.4.0
Show newest version
/*
 * 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 Dante Engine.
 *
 * Dante 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.
 *
 * Dante 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.oauth2.data.jpa.service;

import cn.herodotus.engine.data.core.repository.BaseRepository;
import cn.herodotus.engine.data.core.service.BaseService;
import cn.herodotus.engine.oauth2.data.jpa.entity.HerodotusAuthorization;
import cn.herodotus.engine.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 BaseService { private static final Logger log = LoggerFactory.getLogger(HerodotusAuthorizationService.class); private final HerodotusAuthorizationRepository herodotusAuthorizationRepository; @Autowired public HerodotusAuthorizationService(HerodotusAuthorizationRepository herodotusAuthorizationRepository) { this.herodotusAuthorizationRepository = herodotusAuthorizationRepository; } @Override public BaseRepository 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 - 2024 Weber Informatics LLC | Privacy Policy