001/* 002 * Copyright 2015 Aroma Tech. 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 */ 016 017package tech.aroma.banana.authentication.service.data; 018 019import java.time.Instant; 020import java.util.Objects; 021import tech.aroma.banana.thrift.authentication.ApplicationToken; 022import tech.aroma.banana.thrift.authentication.UserToken; 023import tech.aroma.banana.thrift.authentication.service.AuthenticationToken; 024import tech.aroma.banana.thrift.authentication.service.TokenType; 025import tech.sirwellington.alchemy.annotations.concurrency.Mutable; 026import tech.sirwellington.alchemy.annotations.objects.Pojo; 027 028import static tech.aroma.banana.thrift.authentication.service.TokenType.APPLICATION; 029import static tech.aroma.banana.thrift.authentication.service.TokenType.USER; 030import static tech.sirwellington.alchemy.arguments.Arguments.checkThat; 031import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull; 032 033/** 034 * 035 * @author SirWellington 036 */ 037@Mutable 038@Pojo 039public final class Token 040{ 041 042 private String tokenId; 043 private Instant timeOfCreation; 044 private Instant timeOfExpiration; 045 private String ownerId; 046 private TokenType tokenType; 047 048 public Token() 049 { 050 } 051 052 public String getTokenId() 053 { 054 return tokenId; 055 } 056 057 public void setTokenId(String tokenId) 058 { 059 this.tokenId = tokenId; 060 } 061 062 public Instant getTimeOfCreation() 063 { 064 return timeOfCreation; 065 } 066 067 public void setTimeOfCreation(Instant timeOfCreation) 068 { 069 this.timeOfCreation = timeOfCreation; 070 } 071 072 public Instant getTimeOfExpiration() 073 { 074 return timeOfExpiration; 075 } 076 077 public void setTimeOfExpiration(Instant timeOfExpiration) 078 { 079 this.timeOfExpiration = timeOfExpiration; 080 } 081 082 public String getOwnerId() 083 { 084 return ownerId; 085 } 086 087 public void setOwnerId(String ownerId) 088 { 089 this.ownerId = ownerId; 090 } 091 092 public TokenType getTokenType() 093 { 094 return tokenType; 095 } 096 097 public void setTokenType(TokenType tokenType) 098 { 099 this.tokenType = tokenType; 100 } 101 102 @Override 103 public String toString() 104 { 105 return "Token{" + "tokenId=" + tokenId + ", timeOfCreation=" + timeOfCreation + ", timeOfExpiration=" + timeOfExpiration + ", ownerId=" + ownerId + ", tokenType=" + tokenType + '}'; 106 } 107 108 @Override 109 public int hashCode() 110 { 111 int hash = 3; 112 hash = 59 * hash + Objects.hashCode(this.tokenId); 113 hash = 59 * hash + Objects.hashCode(this.timeOfCreation); 114 hash = 59 * hash + Objects.hashCode(this.timeOfExpiration); 115 hash = 59 * hash + Objects.hashCode(this.ownerId); 116 hash = 59 * hash + Objects.hashCode(this.tokenType); 117 return hash; 118 } 119 120 @Override 121 public boolean equals(Object obj) 122 { 123 if (this == obj) 124 { 125 return true; 126 } 127 if (obj == null) 128 { 129 return false; 130 } 131 if (getClass() != obj.getClass()) 132 { 133 return false; 134 } 135 final Token other = (Token) obj; 136 if (!Objects.equals(this.tokenId, other.tokenId)) 137 { 138 return false; 139 } 140 if (!Objects.equals(this.ownerId, other.ownerId)) 141 { 142 return false; 143 } 144 if (!Objects.equals(this.timeOfCreation, other.timeOfCreation)) 145 { 146 return false; 147 } 148 if (!Objects.equals(this.timeOfExpiration, other.timeOfExpiration)) 149 { 150 return false; 151 } 152 if (this.tokenType != other.tokenType) 153 { 154 return false; 155 } 156 return true; 157 } 158 159 public AuthenticationToken asAuthenticationToken() 160 { 161 checkThat(tokenType) 162 .usingMessage("missing Token Type") 163 .is(notNull()); 164 165 AuthenticationToken token = new AuthenticationToken(); 166 if (tokenType == APPLICATION) 167 { 168 token.setApplicationToken(asApplicationToken()); 169 } 170 else if (tokenType == USER) 171 { 172 token.setUserToken(asUserToken()); 173 } 174 175 return token; 176 } 177 178 public ApplicationToken asApplicationToken() 179 { 180 ApplicationToken token = new ApplicationToken(); 181 token.setTokenId(tokenId) 182 .setApplicationId(ownerId); 183 184 if (timeOfExpiration != null) 185 { 186 token.setTimeOfExpiration(timeOfExpiration.toEpochMilli()); 187 } 188 189 return token; 190 } 191 192 public UserToken asUserToken() 193 { 194 UserToken token = new UserToken() 195 .setTokenId(tokenId); 196 197 if (timeOfExpiration != null) 198 { 199 token.setTimeOfExpiration(timeOfExpiration.toEpochMilli()); 200 } 201 202 return token; 203 } 204 205}