|
| 1 | +/* |
| 2 | + * Copyright 2020 LINE Corporation |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.linecorp.bot.client; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.URL; |
| 23 | +import java.security.KeyFactory; |
| 24 | +import java.security.PrivateKey; |
| 25 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 26 | +import java.time.Duration; |
| 27 | +import java.time.Instant; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import org.junit.Assume; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.slf4j.bridge.SLF4JBridgeHandler; |
| 34 | +import org.yaml.snakeyaml.Yaml; |
| 35 | + |
| 36 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 37 | +import com.google.common.collect.ImmutableMap; |
| 38 | +import com.google.common.io.BaseEncoding; |
| 39 | + |
| 40 | +import com.linecorp.bot.model.oauth.IssueChannelAccessTokenResponse; |
| 41 | + |
| 42 | +import io.jsonwebtoken.Jwts; |
| 43 | +import io.jsonwebtoken.SignatureAlgorithm; |
| 44 | +import io.jsonwebtoken.jackson.io.JacksonSerializer; |
| 45 | +import lombok.extern.slf4j.Slf4j; |
| 46 | + |
| 47 | +@Slf4j |
| 48 | +public class LineOAuthClientIntegrationTest { |
| 49 | + private static final URL TEST_RESOURCE = ClassLoader.getSystemResource("integration_test_settings.yml"); |
| 50 | + |
| 51 | + private LineOAuthClient target; |
| 52 | + private String endpoint; |
| 53 | + private String pemPrivateKey; |
| 54 | + private String channelId; |
| 55 | + private String channelSecret; |
| 56 | + private String kid; |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setUp() throws IOException { |
| 60 | + Assume.assumeTrue(TEST_RESOURCE != null); |
| 61 | + |
| 62 | + final Map<?, ?> map = new ObjectMapper() |
| 63 | + .convertValue(new Yaml().load(TEST_RESOURCE.openStream()), Map.class); |
| 64 | + |
| 65 | + endpoint = (String) map.get("endpoint"); |
| 66 | + target = LineOAuthClient |
| 67 | + .builder() |
| 68 | + .apiEndPoint(endpoint) |
| 69 | + .build(); |
| 70 | + |
| 71 | + pemPrivateKey = ((String) map.get("pemPrivateKey")).replaceAll("\n", ""); |
| 72 | + kid = (String) map.get("kid"); |
| 73 | + channelId = String.valueOf(map.get("channelId")); |
| 74 | + channelSecret = (String) map.get("channelSecret"); |
| 75 | + } |
| 76 | + |
| 77 | + static { |
| 78 | + SLF4JBridgeHandler.removeHandlersForRootLogger(); |
| 79 | + SLF4JBridgeHandler.install(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void gwtTokenIntegrationTest() throws Exception { |
| 84 | + final Map<String, Object> header = ImmutableMap.of( |
| 85 | + "alg", "RS256", |
| 86 | + "typ", "JWT", |
| 87 | + "kid", kid |
| 88 | + ); |
| 89 | + |
| 90 | + final Map<String, Object> body = ImmutableMap.of( |
| 91 | + "iss", channelId, |
| 92 | + "sub", channelId, |
| 93 | + "aud", endpoint, |
| 94 | + "exp", Instant.now().plusSeconds(10).getEpochSecond(), |
| 95 | + "token_exp", Duration.ofMinutes(1).getSeconds() |
| 96 | + ); |
| 97 | + |
| 98 | + byte[] bytes = BaseEncoding.base64().decode(pemPrivateKey); |
| 99 | + |
| 100 | + KeyFactory kf = KeyFactory.getInstance("RSA"); |
| 101 | + PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(bytes)); |
| 102 | + |
| 103 | + String jws = Jwts.builder() |
| 104 | + .serializeToJsonWith(new JacksonSerializer(new ObjectMapper())) |
| 105 | + .setHeader(header) |
| 106 | + .setClaims(body) |
| 107 | + .signWith(privateKey, SignatureAlgorithm.RS256) |
| 108 | + .compact(); |
| 109 | + |
| 110 | + log.info("{}", jws); |
| 111 | + |
| 112 | + // Issue |
| 113 | + IssueChannelAccessTokenResponse issueChannelAccessTokenResponse = |
| 114 | + target.issueChannelTokenByJWT(jws).get(); |
| 115 | + |
| 116 | + log.info("{}", issueChannelAccessTokenResponse); |
| 117 | + assertThat(issueChannelAccessTokenResponse.getExpiresInSecs()).isEqualTo(60); |
| 118 | + |
| 119 | + // Revoke |
| 120 | + target.revokeChannelTokenByJWT( |
| 121 | + channelId, |
| 122 | + channelSecret, |
| 123 | + issueChannelAccessTokenResponse.getAccessToken()) |
| 124 | + .get(); |
| 125 | + } |
| 126 | +} |
0 commit comments