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.application.service;
019
020
021import org.slf4j.Logger;
022import org.slf4j.LoggerFactory;
023import tech.aroma.thrift.authentication.ApplicationToken;
024import tech.aroma.thrift.authentication.service.AuthenticationService;
025import tech.aroma.thrift.authentication.service.VerifyTokenRequest;
026import tech.aroma.thrift.authentication.service.VerifyTokenResponse;
027import tech.aroma.thrift.exceptions.InvalidArgumentException;
028import tech.aroma.thrift.exceptions.InvalidTokenException;
029import tech.sirwellington.alchemy.annotations.access.Internal;
030import tech.sirwellington.alchemy.annotations.access.NonInstantiable;
031import tech.sirwellington.alchemy.annotations.arguments.Required;
032import tech.sirwellington.alchemy.arguments.AlchemyAssertion;
033import tech.sirwellington.alchemy.arguments.ExceptionMapper;
034import tech.sirwellington.alchemy.arguments.FailedAssertionException;
035
036import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
037import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
038import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString;
039
040/**
041 *
042 * @author SirWellington
043 */
044@NonInstantiable
045@Internal
046public final class ApplicationAssertions 
047{
048    private final static Logger LOG = LoggerFactory.getLogger(ApplicationAssertions.class);
049    
050    private ApplicationAssertions() throws IllegalAccessException
051    {
052        throw new IllegalAccessException("cannot instantiate");
053    }
054
055    public static AlchemyAssertion<ApplicationToken> validTokenIn(@Required AuthenticationService.Iface authenticationService) 
056    {
057        checkThat(authenticationService)
058            .usingMessage("authentication service is null")
059            .is(notNull());
060        
061        return token ->
062        {
063            checkThat(token)
064                .usingMessage("token is null")
065                .is(notNull());
066            
067            checkThat(token.tokenId)
068                .usingMessage("tokenId is missing")
069                .is(nonEmptyString());
070            
071            VerifyTokenRequest request = new VerifyTokenRequest()
072                .setTokenId(token.getTokenId())
073                .setOwnerId(token.applicationId);
074            
075            VerifyTokenResponse response;
076            try
077            {
078                response = authenticationService.verifyToken(request);
079            }
080            catch (InvalidTokenException ex)
081            {
082                throw new FailedAssertionException("Token is not valid");
083            }
084            catch (Exception ex)
085            {
086                throw new FailedAssertionException("Could not contact Authentication Service", ex);
087            }
088        };
089    }
090    
091    
092    public static ExceptionMapper<InvalidArgumentException> withMessage(String message)
093    {
094        return ex -> new InvalidArgumentException(message);
095    }
096}