org.apigw.authserver.svc.impl.ExpiringAuthorizationCodeServices.java Source code

Java tutorial

Introduction

Here is the source code for org.apigw.authserver.svc.impl.ExpiringAuthorizationCodeServices.java

Source

/**
 *   Copyright 2013 Stockholm County Council
 *
 *   This file is part of APIGW
 *
 *   APIGW is free software; you can redistribute it and/or modify
 *   it under the terms of version 2.1 of the GNU Lesser General Public
 *   License as published by the Free Software Foundation.
 *
 *   APIGW 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 Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with APIGW; if not, write to the
 *   Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 *   Boston, MA 02111-1307  USA
 *
 */
package org.apigw.authserver.svc.impl;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.provider.code.AuthorizationRequestHolder;
import org.springframework.security.oauth2.provider.code.RandomValueAuthorizationCodeServices;

import com.google.common.collect.MapMaker;

/**
 * Implementation of authorization code services that stores the codes and authentication in memory 
 * with an expiration time set
 * 
 * @author Albert rwall
 */
public class ExpiringAuthorizationCodeServices extends RandomValueAuthorizationCodeServices {

    private static final Logger log = LoggerFactory.getLogger(ExpiringAuthorizationCodeServices.class);
    protected final ConcurrentMap<String, AuthorizationRequestHolder> authorizationCodeStore;

    /**
     * 
     * @param timeToLive How long the authorization code will exist, specified in seconds
     */
    public ExpiringAuthorizationCodeServices(int timeToLive) {
        authorizationCodeStore = new MapMaker().expiration(timeToLive, TimeUnit.SECONDS).makeMap();
    }

    @Override
    protected void store(String code, AuthorizationRequestHolder authentication) {
        log.debug("Storing authorization code in memory...");
        this.authorizationCodeStore.put(code, authentication);
    }

    @Override
    public AuthorizationRequestHolder remove(String code) {
        log.debug("Removing authorization code from memory...");
        AuthorizationRequestHolder auth = this.authorizationCodeStore.remove(code);
        return auth;
    }
}