001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package core.tut.pori.utils;
017
018import org.apache.commons.lang3.RandomStringUtils;
019
020import core.tut.pori.context.ServiceInitializer;
021import core.tut.pori.properties.NonceProperties;
022
023/**
024 * Methods for generating nonce
025 *
026 */
027public final class NonceUtils {
028  /**
029   * 
030   * @return unique nonce string
031   */
032  public static Nonce generateNonce(){
033    NonceProperties np = ServiceInitializer.getPropertyHandler().getSystemProperties(NonceProperties.class);
034    return new Nonce(System.currentTimeMillis()+np.getNonceExpiresIn(), RandomStringUtils.randomAlphabetic(np.getNonceLength()));
035  }
036  
037  /**
038   * A class presenting a generated nonce.
039   *
040   */
041  public static class Nonce{
042    private long _expires = -1;
043    private String _nonce = null;
044    
045    /**
046     * 
047     * @param expires expiration date in unix time
048     * @param nonce
049     */
050    public Nonce(long expires, String nonce){
051      _nonce = nonce;
052      _expires = expires;
053    }
054
055    /**
056     * @return the nonce
057     */
058    public String getNonce() {
059      return _nonce;
060    }
061
062    /**
063     * @return the expires in unix time
064     */
065    public long getExpires() {
066      return _expires;
067    }
068  } // class Nonce
069}