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
017
018package tech.aroma.banana.authentication.service.data;
019
020import java.util.List;
021import org.apache.thrift.TException;
022import tech.aroma.banana.thrift.exceptions.InvalidTokenException;
023import tech.sirwellington.alchemy.annotations.arguments.NonEmpty;
024import tech.sirwellington.alchemy.annotations.arguments.NonNull;
025import tech.sirwellington.alchemy.annotations.designs.patterns.StrategyPattern;
026
027import static tech.aroma.banana.authentication.service.AuthenticationAssertions.tokenInRepository;
028import static tech.sirwellington.alchemy.annotations.designs.patterns.StrategyPattern.Role.INTERFACE;
029import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
030import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
031import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString;
032
033
034/**
035 * This interface is responsible for the storage and retrieval of 
036 * Tokens.
037 * 
038 * @author SirWellington
039 */
040@StrategyPattern(role = INTERFACE)
041public interface TokenRepository 
042{
043
044    boolean doesTokenExist(@NonEmpty String tokenId) throws TException;
045
046    default boolean doesTokenBelongTo(@NonEmpty String tokenId, @NonEmpty String ownerId) throws InvalidTokenException, TException
047    {
048        checkThat(tokenId, ownerId)
049            .usingMessage("tokenId and ownerId are required")
050            .are(nonEmptyString());
051
052        checkThat(tokenId)
053            .throwing(ex -> new InvalidTokenException("tokenId does not exist in this repository"))
054            .is(tokenInRepository(this));
055
056        Token token = this.getToken(tokenId);
057
058        return ownerId.equals(token.getOwnerId());
059    }
060
061    Token getToken(@NonEmpty String tokenId) throws TException, InvalidTokenException;
062
063    void saveToken(@NonNull Token token) throws TException;
064
065    List<Token> getTokensBelongingTo(@NonEmpty String ownerId) throws TException;
066
067    void deleteToken(@NonEmpty String tokenId) throws TException;
068
069    default void deleteTokens(@NonNull List<String> tokenIds) throws TException
070    {
071        checkThat(tokenIds).is(notNull());
072
073        for (String token : tokenIds)
074        {
075            deleteToken(token);
076        }
077    }
078
079}