line-app.php
69 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Social\Lib\Login; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | class Line_App { |
| 8 | |
| 9 | private $client_id; |
| 10 | |
| 11 | private $client_secret; |
| 12 | |
| 13 | public function __construct() { |
| 14 | $saved_settings = \WP_Social\App\Settings::get_login_settings_data(); |
| 15 | if (!empty($saved_settings['lineapp'])) { |
| 16 | $this->client_id = $saved_settings['lineapp']['id']; |
| 17 | $this->client_secret = $saved_settings['lineapp']['secret']; |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | public function get_auth_url($redirect_uri) { |
| 22 | return "https://access.line.me/oauth2/v2.1/authorize?response_type=code&client_id=" . $this->client_id . "&redirect_uri=" . $redirect_uri . "&state=12345abcde&scope=profile%20openid%20email"; |
| 23 | } |
| 24 | |
| 25 | public function get_id_token($code) { |
| 26 | $redirect_uri = get_home_url() . '/wp-json/wslu-social-login/type/lineapp'; |
| 27 | |
| 28 | $response = wp_remote_post( |
| 29 | 'https://api.line.me/oauth2/v2.1/token', |
| 30 | array( |
| 31 | 'method' => 'POST', |
| 32 | 'body' => array( |
| 33 | 'grant_type' => 'authorization_code', |
| 34 | 'code' => $code, |
| 35 | 'redirect_uri' => $redirect_uri, |
| 36 | 'client_id' => $this->client_id, |
| 37 | 'client_secret' => $this->client_secret, |
| 38 | ) |
| 39 | ) |
| 40 | ); |
| 41 | |
| 42 | if (is_wp_error($response)) { |
| 43 | return $response->get_error_message(); |
| 44 | } |
| 45 | return json_decode($response['body']); |
| 46 | } |
| 47 | |
| 48 | public function get_user_info($code) { |
| 49 | |
| 50 | $id_token = $this->get_id_token($code); |
| 51 | |
| 52 | $response = wp_remote_post( |
| 53 | 'https://api.line.me/oauth2/v2.1/verify', |
| 54 | array( |
| 55 | 'method' => 'POST', |
| 56 | 'body' => array( |
| 57 | 'client_id' => $this->client_id, |
| 58 | 'id_token' => $id_token->id_token, |
| 59 | ) |
| 60 | ) |
| 61 | ); |
| 62 | |
| 63 | if (is_wp_error($response)) { |
| 64 | return $response->get_error_message(); |
| 65 | } |
| 66 | return json_decode($response['body']); |
| 67 | } |
| 68 | } |
| 69 |