From c01dedf704522bd84991acda1425ce290d25bdfd Mon Sep 17 00:00:00 2001 From: Ilko Kacharov Date: Tue, 20 Nov 2018 16:50:57 +0200 Subject: [PATCH] Added QR code request to authy api client --- lib/Authy/AuthyApi.php | 21 ++++++++++++++++++++- tests/Authy/ApiTest.php | 22 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/Authy/AuthyApi.php b/lib/Authy/AuthyApi.php index 2a86bce..be6a4f7 100644 --- a/lib/Authy/AuthyApi.php +++ b/lib/Authy/AuthyApi.php @@ -180,6 +180,25 @@ public function userStatus($authy_id) return new AuthyResponse($resp); } + /** + * Request QR code link. + * + * @param string $authy_id User's id stored in your database + * @param array $opts Array of options, for example: array("qr_size" => 300) + * + * @return AuthyResponse the server response + */ + public function qrCode($authy_id, $opts = []) + { + $authy_id = urlencode($authy_id); + $resp = $this->rest->post("protected/json/users/{$authy_id}/secret", array_merge( + $this->default_options, + ['query' => $opts] + )); + + return new AuthyResponse($resp); + } + /** * Starts phone verification. (Sends token to user via sms or call). * @@ -275,7 +294,7 @@ public function phoneInfo($phone_number, $country_code) public function createApprovalRequest($authy_id, $message, $opts = []) { $opts['message'] = $message; - + $authy_id = urlencode($authy_id); $resp = $this->rest->post("onetouch/json/users/{$authy_id}/approval_requests", array_merge( $this->default_options, diff --git a/tests/Authy/ApiTest.php b/tests/Authy/ApiTest.php index dcc65da..c9c16cb 100644 --- a/tests/Authy/ApiTest.php +++ b/tests/Authy/ApiTest.php @@ -240,6 +240,28 @@ public function testPhonceCallWithValidUser() $this->assertRegExp('/Call started/i', $call->message()); } + public function testQrCodeWithInvalidUser() + { + $mock_client = $this->mockClient([[404, '{"errors": {"message": "User not found."}}']]); + $call = $mock_client->qrCode(0, []); + + $this->assertEquals(false, $call->ok()); + $this->assertEquals("User not found.", $call->errors()->message); + } + + public function testQrCodeWithValidUser() + { + $mock_client = $this->mockClient([ + [200, '{ "user": { "id": 2 } }'], + [200, '{ "qr_code": "https://example.com/qr.png" }'] + ]); + $user = $mock_client->registerUser('user@example.com', '305-456-2345', 1); + $call = $mock_client->qrCode($user->id(), []); + + $this->assertEquals(true, $call->ok()); + $this->assertNotNull($call->bodyvar('qr_code')); + } + public function testDeleteUserWithInvalidUser() { $mock_client = $this->mockClient([[404, '{"errors": {"message": "User not found."}}']]);