it.infn.mw.iam.core.IamTokenService.java Source code

Java tutorial

Introduction

Here is the source code for it.infn.mw.iam.core.IamTokenService.java

Source

/**
 * Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2018
 *
 * 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 it.infn.mw.iam.core;

import java.util.Date;
import java.util.Set;

import org.mitre.oauth2.model.OAuth2AccessTokenEntity;
import org.mitre.oauth2.model.OAuth2RefreshTokenEntity;
import org.mitre.oauth2.service.impl.DefaultOAuth2ProviderTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.google.common.collect.Sets;

import it.infn.mw.iam.persistence.repository.IamOAuthAccessTokenRepository;
import it.infn.mw.iam.persistence.repository.IamOAuthRefreshTokenRepository;

@Service("defaultOAuth2ProviderTokenService")
@Primary
public class IamTokenService extends DefaultOAuth2ProviderTokenService {

    public static final Logger LOG = LoggerFactory.getLogger(IamTokenService.class);

    private final IamOAuthAccessTokenRepository accessTokenRepo;
    private final IamOAuthRefreshTokenRepository refreshTokenRepo;

    @Autowired
    public IamTokenService(IamOAuthAccessTokenRepository atRepo, IamOAuthRefreshTokenRepository rtRepo) {

        this.accessTokenRepo = atRepo;
        this.refreshTokenRepo = rtRepo;
    }

    @Override
    public Set<OAuth2AccessTokenEntity> getAllAccessTokensForUser(String id) {

        Set<OAuth2AccessTokenEntity> results = Sets.newLinkedHashSet();
        results.addAll(accessTokenRepo.findValidAccessTokensForUser(id, new Date()));
        return results;
    }

    @Override
    public Set<OAuth2RefreshTokenEntity> getAllRefreshTokensForUser(String id) {
        Set<OAuth2RefreshTokenEntity> results = Sets.newLinkedHashSet();
        results.addAll(refreshTokenRepo.findValidRefreshTokensForUser(id, new Date()));
        return results;
    }

    @Override
    public void revokeAccessToken(OAuth2AccessTokenEntity accessToken) {
        accessTokenRepo.delete(accessToken);
    }

    @Override
    public void revokeRefreshToken(OAuth2RefreshTokenEntity refreshToken) {
        refreshTokenRepo.delete(refreshToken);
    }

}