001package org.apache.archiva.redback.authentication; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.Serializable; 023import java.time.Duration; 024import java.time.Instant; 025import java.util.Date; 026 027/** 028 * 029 * Simple Token information class that contains a username and a lifetime. 030 * 031 * The class is not able to detect time manipulations. It is assumed that the 032 * current time of the system is correct. 033 * 034 * This class is immutable. 035 * 036 * Created by Martin Stockhammer on 03.02.17. 037 */ 038public final class SimpleTokenData implements Serializable, TokenData { 039 040 041 private static final long serialVersionUID = 5907745449771921813L; 042 043 private final String user; 044 private final Instant created; 045 private final Instant validBefore; 046 private final long nonce; 047 048 /** 049 * Creates a new token info instance for the given user. 050 * The lifetime in milliseconds defines the invalidation date by 051 * adding the lifetime to the current time of instantiation. 052 * 053 * @param user The user name 054 * @param lifetime The number of milliseconds after that the token is invalid 055 * @param nonce Should be a random number and different for each instance. 056 */ 057 public SimpleTokenData(final String user, final long lifetime, final long nonce) { 058 this.user=user; 059 this.created = Instant.now( ); 060 this.validBefore = created.plus( Duration.ofMillis( lifetime ) ); 061 this.nonce = nonce; 062 } 063 064 /** 065 * Creates a new token info instance for the given user. 066 * The lifetime in milliseconds defines the invalidation date by 067 * adding the lifetime to the current time of instantiation. 068 * 069 * @param user The user name 070 * @param lifetime The number of milliseconds after that the token is invalid 071 * @param nonce Should be a random number and different for each instance. 072 */ 073 public SimpleTokenData(final String user, final Duration lifetime, final long nonce) { 074 this.user=user; 075 this.created = Instant.now( ); 076 this.validBefore = created.plus( lifetime ); 077 this.nonce = nonce; 078 } 079 080 @Override 081 public final String getUser() { 082 return user; 083 } 084 085 @Override 086 public final Instant created() { 087 return created; 088 } 089 090 @Override 091 public final Instant validBefore() { 092 return validBefore; 093 } 094 095 @Override 096 public final long getNonce() { 097 return nonce; 098 } 099 100 @Override 101 public boolean isValid() { 102 return Instant.now( ).isBefore( validBefore ); 103 } 104 105}