com.epam.reportportal.auth.store.OAuth2MongoTokenStore.java Source code

Java tutorial

Introduction

Here is the source code for com.epam.reportportal.auth.store.OAuth2MongoTokenStore.java

Source

/*
 * Copyright 2016 EPAM Systems
 *
 *
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/service-authorization
 *
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Report Portal 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.epam.reportportal.auth.store;

import com.epam.reportportal.auth.store.entity.OAuth2AccessTokenEntity;
import com.epam.reportportal.auth.store.entity.OAuth2RefreshTokenEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.common.util.SerializationUtils;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.AuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
import org.springframework.security.oauth2.provider.token.TokenStore;

import java.util.Collection;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Andrei Varabyeu
 */
public class OAuth2MongoTokenStore implements TokenStore {

    @Autowired
    private OAuth2AccessTokenRepository oAuth2AccessTokenRepository;

    @Autowired
    private OAuth2RefreshTokenRepository oAuth2RefreshTokenRepository;

    private AuthenticationKeyGenerator authenticationKeyGenerator = new DefaultAuthenticationKeyGenerator();

    @Override
    public OAuth2Authentication readAuthentication(OAuth2AccessToken token) {
        return readAuthentication(token.getValue());
    }

    @Override
    public OAuth2Authentication readAuthentication(String tokenId) {
        return SerializationUtils
                .deserialize(oAuth2AccessTokenRepository.findByTokenId(tokenId).getAuthentication());
    }

    @Override
    public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
        OAuth2AccessTokenEntity tokenEntity = new OAuth2AccessTokenEntity();
        tokenEntity.setTokenId(token.getValue());
        tokenEntity.setToken(SerializationUtils.serialize(token));
        tokenEntity.setAuthentication(SerializationUtils.serialize(authentication));
        tokenEntity.setAuthenticationId(authenticationKeyGenerator.extractKey(authentication));
        tokenEntity.setUserName(authentication.isClientOnly() ? null : authentication.getName());
        tokenEntity.setRefreshToken(null == token.getRefreshToken() ? null : token.getRefreshToken().getValue());
        tokenEntity.setClientId(authentication.getOAuth2Request().getClientId());

        oAuth2AccessTokenRepository.save(tokenEntity);
    }

    @Override
    public OAuth2AccessToken readAccessToken(String tokenValue) {
        OAuth2AccessTokenEntity token = oAuth2AccessTokenRepository.findByTokenId(tokenValue);
        if (token == null) {
            return null; //let spring security handle the invalid token
        }
        return SerializationUtils.deserialize(token.getToken());
    }

    @Override
    public void removeAccessToken(OAuth2AccessToken token) {
        OAuth2AccessTokenEntity accessToken = oAuth2AccessTokenRepository.findByTokenId(token.getValue());
        if (accessToken != null) {
            oAuth2AccessTokenRepository.delete(accessToken);
        }
    }

    @Override
    public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
        OAuth2RefreshTokenEntity refreshEntity = new OAuth2RefreshTokenEntity();
        refreshEntity.setAuthentication(SerializationUtils.serialize(authentication));
        refreshEntity.setTokenId(refreshToken.getValue());
        refreshEntity.setoAuth2RefreshToken(SerializationUtils.serialize(refreshToken));
        oAuth2RefreshTokenRepository.save(refreshEntity);
    }

    @Override
    public OAuth2RefreshToken readRefreshToken(String tokenValue) {
        return Optional.ofNullable(oAuth2RefreshTokenRepository.findByTokenId(tokenValue))
                .map(OAuth2RefreshTokenEntity::getoAuth2RefreshToken)
                .map(SerializationUtils::<OAuth2RefreshToken>deserialize).orElse(null);
    }

    @Override
    public OAuth2Authentication readAuthenticationForRefreshToken(OAuth2RefreshToken token) {
        return Optional.ofNullable(oAuth2RefreshTokenRepository.findByTokenId(token.getValue()))
                .map(OAuth2RefreshTokenEntity::getoAuth2RefreshToken)
                .map(SerializationUtils::<OAuth2Authentication>deserialize).orElse(null);
    }

    @Override
    public void removeRefreshToken(OAuth2RefreshToken token) {
        if (null != token && null != token.getValue()) {
            oAuth2RefreshTokenRepository.delete(token.getValue());
        }
    }

    @Override
    public void removeAccessTokenUsingRefreshToken(OAuth2RefreshToken refreshToken) {
        oAuth2AccessTokenRepository.delete(oAuth2AccessTokenRepository.findByRefreshToken(refreshToken.getValue()));
    }

    @Override
    public OAuth2AccessToken getAccessToken(OAuth2Authentication authentication) {
        OAuth2AccessTokenEntity token = oAuth2AccessTokenRepository
                .findByAuthenticationId(authenticationKeyGenerator.extractKey(authentication));
        return token == null ? null : SerializationUtils.deserialize(token.getToken());
    }

    @Override
    public Collection<OAuth2AccessToken> findTokensByClientId(String clientId) {
        return oAuth2AccessTokenRepository.findByClientId(clientId).map(this::extractAccessToken)
                .collect(Collectors.toList());
    }

    @Override
    public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
        return oAuth2AccessTokenRepository.findByClientIdAndUserName(clientId, userName)
                .map(this::extractAccessToken).collect(Collectors.toList());
    }

    private OAuth2AccessToken extractAccessToken(OAuth2AccessTokenEntity token) {
        return (OAuth2AccessToken) SerializationUtils.deserialize(token.getToken());
    }
}