| /* |
| * Copyright 2017 The Mifos Initiative. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package io.mifos.anubis.token; |
| |
| import io.mifos.anubis.api.v1.TokenConstants; |
| import org.junit.Assert; |
| import org.junit.BeforeClass; |
| import org.junit.Test; |
| |
| import java.time.LocalDateTime; |
| import java.time.ZoneId; |
| import java.time.ZoneOffset; |
| |
| import io.jsonwebtoken.Claims; |
| import io.jsonwebtoken.Header; |
| import io.jsonwebtoken.Jwt; |
| import io.jsonwebtoken.Jwts; |
| import io.mifos.core.lang.security.RsaKeyPairFactory; |
| |
| /** |
| * @author Myrle Krantz, Markus Geiss |
| */ |
| public class SystemAccessTokenSerializerTest { |
| |
| |
| |
| public SystemAccessTokenSerializerTest() { |
| super(); |
| } |
| |
| private static final String APPLICATION_NAME = "mifosio-core"; |
| private static final String TEST_TENANT = "token_test"; |
| private static final String ROLE = "y"; |
| private static final int SECONDS_TO_LIVE = 15; |
| private static RsaKeyPairFactory.KeyPairHolder keyPairHolder; |
| |
| @BeforeClass |
| public static void initialize() |
| { |
| keyPairHolder = RsaKeyPairFactory.createKeyPair(); |
| } |
| |
| @SuppressWarnings({"unchecked"}) |
| @Test |
| public void shouldCreateValidSeshatToken() throws Exception { |
| |
| final SystemAccessTokenSerializer.Specification specification |
| = new SystemAccessTokenSerializer.Specification() |
| .setTargetApplicationName(APPLICATION_NAME) |
| .setPrivateKey(keyPairHolder.privateKey()) |
| .setRole(ROLE) |
| .setSecondsToLive(SECONDS_TO_LIVE) |
| .setTenant(TEST_TENANT); |
| |
| final SystemAccessTokenSerializer testSubject = new SystemAccessTokenSerializer(); |
| |
| final LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC")); |
| final TokenSerializationResult seshatToken = testSubject.build(specification); |
| |
| Assert.assertNotNull(seshatToken); |
| |
| final LocalDateTime expiration = seshatToken.getExpiration(); |
| final long diff = expiration.toInstant(ZoneOffset.ofHours(0)).getEpochSecond() |
| - now.toInstant(ZoneOffset.ofHours(0)).getEpochSecond(); |
| |
| Assert.assertTrue("The expiration should be 15(+/- 1) seconds from now. Instead it is: " + diff, |
| Math.abs(diff - SECONDS_TO_LIVE) <= 1); |
| |
| final Jwt<Header, Claims> parsedToken = Jwts |
| .parser() |
| .setSigningKey(keyPairHolder.publicKey()) |
| .parse(seshatToken.getToken().substring("Bearer ".length()).trim()); |
| |
| |
| Assert.assertNotNull(parsedToken); |
| Assert.assertEquals(TokenType.SYSTEM.getIssuer(), parsedToken.getBody().get("iss")); |
| Assert.assertEquals(APPLICATION_NAME, parsedToken.getBody().get("aud")); |
| Assert.assertEquals(TEST_TENANT, parsedToken.getBody().get("sub")); |
| final Integer issued = (Integer) parsedToken.getBody().get("iat"); |
| Assert.assertNotNull(issued); |
| final Integer expires = (Integer) parsedToken.getBody().get("exp"); |
| Assert.assertNotNull(expires); |
| Assert.assertTrue(expires > issued); |
| final String version = parsedToken.getBody().get(TokenConstants.JWT_VERSION_CLAIM, String.class); |
| Assert.assertEquals("1", version); |
| } |
| |
| @Test(expected = IllegalArgumentException.class) |
| public void invalidSecondsToLiveCausesException() |
| { |
| final SystemAccessTokenSerializer.Specification specification |
| = new SystemAccessTokenSerializer.Specification() |
| .setTargetApplicationName(APPLICATION_NAME) |
| .setPrivateKey(keyPairHolder.privateKey()) |
| .setRole("y") |
| .setSecondsToLive(0) |
| .setTenant(TEST_TENANT); |
| |
| final SystemAccessTokenSerializer testSubject = new SystemAccessTokenSerializer(); |
| |
| testSubject.build(specification); |
| |
| } |
| } |