From e74a9675ceffd4d11cd449da0c0627951e7c0429 Mon Sep 17 00:00:00 2001 From: shibafu Date: Sat, 17 Oct 2020 14:01:28 +0900 Subject: [PATCH] =?UTF-8?q?Ci-en:=20JWT=E3=81=8B=E3=82=89=E6=9C=89?= =?UTF-8?q?=E5=8A=B9=E6=9C=9F=E9=99=90=E3=82=92=E5=8F=96=E5=BE=97=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/MetadataResolver/CienResolver.php | 14 +- .../MetadataResolver/CienResolverTest.php | 12 +- tests/fixture/Cien/test.html | 314 +++++++++++------- 3 files changed, 208 insertions(+), 132 deletions(-) diff --git a/app/MetadataResolver/CienResolver.php b/app/MetadataResolver/CienResolver.php index a7d9d67..47d0e44 100644 --- a/app/MetadataResolver/CienResolver.php +++ b/app/MetadataResolver/CienResolver.php @@ -27,12 +27,18 @@ class CienResolver extends MetadataResolver $res = $this->client->get($url); $metadata = $this->ogpResolver->parse((string) $res->getBody()); - // 画像URLから有効期限の起点を拾う + // 画像URLのJWTから有効期限を拾う parse_str(parse_url($metadata->image, PHP_URL_QUERY), $params); - if (empty($params['px-time'])) { - throw new \RuntimeException('Parameter "px-time" not found. Image=' . $metadata->image . ' Source=' . $url); + if (empty($params['jwt'])) { + throw new \RuntimeException('Parameter "jwt" not found. Image=' . $metadata->image . ' Source=' . $url); } - $metadata->expires_at = Carbon::createFromTimestamp($params['px-time'])->addHour(1); + $parts = explode('.', $params['jwt']); + if (count($parts) !== 3) { + throw new \RuntimeException('Invalid jwt. Image=' . $metadata->image . ' Source=' . $url); + } + $payload = json_decode(base64_decode(str_replace(['-', '_'], ['+', '/'], $parts[1])), true); + + $metadata->expires_at = Carbon::createFromTimestamp($payload['exp']); return $metadata; } diff --git a/tests/Unit/MetadataResolver/CienResolverTest.php b/tests/Unit/MetadataResolver/CienResolverTest.php index 080ec7b..2395dc0 100644 --- a/tests/Unit/MetadataResolver/CienResolverTest.php +++ b/tests/Unit/MetadataResolver/CienResolverTest.php @@ -25,12 +25,12 @@ class CienResolverTest extends TestCase $this->createResolver(CienResolver::class, $responseText); $metadata = $this->resolver->resolve('https://ci-en.dlsite.com/creator/2462/article/87502'); - $this->assertSame('進捗とボツ立ち絵', $metadata->title); - $this->assertSame('ドット製2D ACTを製作しています。' . PHP_EOL . '恐ろしい存在に襲われる絶望感や、被虐的な官能がテーマです。', $metadata->description); - $this->assertStringStartsWith('https://media.ci-en.jp/private/attachment/creator/00002462/a7afd3b02a6d1caa6afe6a3bf5550fb6a42aefba686f17a0a2f63c97fd6867ab/image-800.jpg?px-time=', $metadata->image); + $this->assertSame('進捗とボツ立ち絵 - ねんない5 - Ci-en(シエン)', $metadata->title); + $this->assertSame('今日のサムネイルはストアページに掲載する予定のキャラクター紹介画像です。 ドットでない解像度の高いイラストは時間も体力も精神力もかかるので、こういうのを行うタスクを開発終盤に残さないでよかったと本気……', $metadata->description); + $this->assertStringStartsWith('https://media.ci-en.jp/private/attachment/creator/00002462/a7afd3b02a6d1caa6afe6a3bf5550fb6a42aefba686f17a0a2f63c97fd6867ab/image-800.jpg?jwt=', $metadata->image); if ($this->shouldUseMock()) { - $this->assertSame('https://media.ci-en.jp/private/attachment/creator/00002462/a7afd3b02a6d1caa6afe6a3bf5550fb6a42aefba686f17a0a2f63c97fd6867ab/image-800.jpg?px-time=1568231879&px-hash=70c57e9a73d5afb4ac5363d1f37a851af8e0cb1f', $metadata->image); - $this->assertSame(1568235479, $metadata->expires_at->timestamp); + $this->assertSame('https://media.ci-en.jp/private/attachment/creator/00002462/a7afd3b02a6d1caa6afe6a3bf5550fb6a42aefba686f17a0a2f63c97fd6867ab/image-800.jpg?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJrZXkiOiJqd3RhdXRoX3NlY18yMDIwT2N0IiwiaXNzIjoiaHR0cHM6XC9cL2NpLWVuLmRsc2l0ZS5jb21cLyIsInN1YiI6IjAwMDAwMDAwMDAwIiwiYXVkIjoiYTdhZmQzYjAyYTZkMWNhYTZhZmU2YTNiZjU1NTBmYjZhNDJhZWZiYTY4NmYxN2EwYTJmNjNjOTdmZDY4NjdhYiIsImV4cCI6MTYwMjk5NTIyMX0.bXUG2T6nXl4hdvsvt1wkIMvbbBdsKk-xbwB6SaxARZA', $metadata->image); + $this->assertSame(1602995221, $metadata->expires_at->timestamp); $this->assertSame('https://ci-en.dlsite.com/creator/2462/article/87502', (string) $this->handler->getLastRequest()->getUri()); } } @@ -41,7 +41,7 @@ class CienResolverTest extends TestCase $this->createResolver(CienResolver::class, $responseText); $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('Parameter "px-time" not found. Image=https://ci-en.dlsite.com/assets/img/common/logo_Ci-en_R18.svg Source=https://ci-en.dlsite.com/'); + $this->expectExceptionMessage('Parameter "jwt" not found. Image=https://ci-en.dlsite.com/assets/img/common/logo_Ci-en_R18.svg Source=https://ci-en.dlsite.com/'); $this->resolver->resolve('https://ci-en.dlsite.com/'); } diff --git a/tests/fixture/Cien/test.html b/tests/fixture/Cien/test.html index 04f73fa..74bd19f 100644 --- a/tests/fixture/Cien/test.html +++ b/tests/fixture/Cien/test.html @@ -1,38 +1,42 @@ - + - + - + - - - - - - - - - - + + + - - - + + + + + + + + + + + + + + + - 進捗とボツ立ち絵 - ねんない5 - Ci-en - + 進捗とボツ立ち絵 - ねんない5 - Ci-en(シエン) + - - + + + + - + + + @@ -80,7 +84,7 @@ creator-id="" > -
+
@@ -116,7 +120,7 @@ - +
@@ -191,31 +195,31 @@ - - + + @@ -233,7 +237,8 @@ - ねんない5 + ねんない5 @@ -242,7 +247,7 @@ ねんない5 @@ -254,7 +259,7 @@

進捗とボツ立ち絵

-
+

今日のサムネイルはストアページに掲載する予定のキャラクター紹介画像です。

ドットでない解像度の高いイラストは時間も体力も精神力もかかるので、こういうのを行うタスクを開発終盤に残さないでよかったと本気で思っています。






【作業進捗】

@@ -280,8 +285,6 @@
フォロワー以上限定無料
- -

開発に関する記事の観覧を行えます。

無料
@@ -302,20 +305,32 @@
\いいね・ツイートで応援!/
+ +
+ +
@@ -330,7 +345,7 @@
次の記事
- 言語設定の対応 + 言語設定の対応

言語設定の対応

@@ -341,7 +356,7 @@
前の記事
- 牢屋シーンも入ります + 牢屋シーンも入ります
@@ -369,7 +384,7 @@