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; 019 020 021import org.slf4j.Logger; 022import org.slf4j.LoggerFactory; 023import tech.aroma.banana.authentication.service.data.TokenRepository; 024import tech.aroma.banana.thrift.authentication.service.AuthenticationToken; 025import tech.aroma.banana.thrift.exceptions.InvalidArgumentException; 026import tech.sirwellington.alchemy.annotations.access.Internal; 027import tech.sirwellington.alchemy.annotations.access.NonInstantiable; 028import tech.sirwellington.alchemy.annotations.arguments.NonNull; 029import tech.sirwellington.alchemy.arguments.AlchemyAssertion; 030import tech.sirwellington.alchemy.arguments.ExceptionMapper; 031import tech.sirwellington.alchemy.arguments.FailedAssertionException; 032 033import static tech.sirwellington.alchemy.arguments.Arguments.checkThat; 034import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull; 035 036/** 037 * 038 * @author SirWellington 039 */ 040@Internal 041@NonInstantiable 042public final class AuthenticationAssertions 043{ 044 private final static Logger LOG = LoggerFactory.getLogger(AuthenticationAssertions.class); 045 046 private AuthenticationAssertions() throws IllegalAccessException 047 { 048 throw new IllegalAccessException("cannot instantiate"); 049 } 050 051 public static void checkRequestNotNull(Object request) throws InvalidArgumentException 052 { 053 checkNotNull(request, "missing request"); 054 } 055 056 public static void checkNotNull(Object reference, String message) throws InvalidArgumentException 057 { 058 checkThat(reference) 059 .throwing(withMessage(message)) 060 .is(notNull()); 061 } 062 063 public static AlchemyAssertion<String> tokenInRepository(@NonNull TokenRepository repository) throws IllegalArgumentException 064 { 065 checkThat(repository) 066 .usingMessage("repository missing") 067 .is(notNull()); 068 069 return token -> 070 { 071 boolean exists; 072 try 073 { 074 exists = repository.doesTokenExist(token); 075 } 076 catch (Exception ex) 077 { 078 throw new FailedAssertionException("Could not check in repository", ex); 079 } 080 081 if (!exists) 082 { 083 throw new FailedAssertionException("Token does not exist: " + token); 084 } 085 }; 086 } 087 088 public static AlchemyAssertion<AuthenticationToken> legalToken() 089 { 090 return t -> 091 { 092 checkThat(t).is(notNull()); 093 094 if (!t.isSet()) 095 { 096 throw new FailedAssertionException("Token Has not been set: " + t); 097 } 098 }; 099 } 100 101 public static ExceptionMapper<InvalidArgumentException> withMessage(String message) 102 { 103 return ex -> new InvalidArgumentException(message); 104 } 105 106}