Commit cb01abeff1b22c078831328ccb0785f7eec9d1f7
1 parent
068a658418
Exists in
master
bug fixes
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
app/SsoClientLibrary.php
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Nama : File SSO client library | 4 | * Nama : File SSO client library |
5 | * Tujuan : File ini dibuat dalam bentuk class objek sehingga dapat di integrasikan | 5 | * Tujuan : File ini dibuat dalam bentuk class objek sehingga dapat di integrasikan |
6 | * dengan aplikasi pihak ketiga yang ingin terhubung menggunakan aplikasi SSO | 6 | * dengan aplikasi pihak ketiga yang ingin terhubung menggunakan aplikasi SSO |
7 | * Deskripsi Variable : | 7 | * Deskripsi Variable : |
8 | * $clientId | 8 | * $clientId |
9 | * - merupakan identitas unik ID yang diperoleh saat mendaftarkan | 9 | * - merupakan identitas unik ID yang diperoleh saat mendaftarkan |
10 | * aplikasi pihak ketiga pada aplikasi SSO | 10 | * aplikasi pihak ketiga pada aplikasi SSO |
11 | * $clientSecret | 11 | * $clientSecret |
12 | * - merupakan identitas unik Secret (rahasia) yang digunakan saat akses | 12 | * - merupakan identitas unik Secret (rahasia) yang digunakan saat akses |
13 | * kepada aplikasi SSO sehingga diizinkan | 13 | * kepada aplikasi SSO sehingga diizinkan |
14 | * $redirectUri | 14 | * $redirectUri |
15 | * - merupakan alamat url yang akan mengelola hasil informasi login SSO | 15 | * - merupakan alamat url yang akan mengelola hasil informasi login SSO |
16 | * $targetUri | 16 | * $targetUri |
17 | * - adalah alamat website aplikasi SSO | 17 | * - adalah alamat website aplikasi SSO |
18 | * | 18 | * |
19 | */ | 19 | */ |
20 | 20 | ||
21 | namespace App; | 21 | namespace App; |
22 | 22 | ||
23 | class SsoClientLibrary { | 23 | class SsoClientLibrary { |
24 | 24 | ||
25 | protected $clientId; | 25 | protected $clientId; |
26 | protected $clientSecret; | 26 | protected $clientSecret; |
27 | protected $redirectUri; | 27 | protected $redirectUri; |
28 | protected $targetUri; | 28 | protected $targetUri; |
29 | 29 | ||
30 | public function __construct() { | 30 | public function __construct() { |
31 | $this->clientId = config('master.sso.client_id'); // 3rd Party client Id | 31 | $this->clientId = config('master.sso.client_id'); // 3rd Party client Id |
32 | $this->clientSecret = config('master.sso.client_secreat'); // 3rd Party client secret | 32 | $this->clientSecret = config('master.sso.client_secret'); // 3rd Party client secret |
33 | $this->redirectUri = config('master.sso.redirect_uri'); // 3rd Party url redirect or url to handle callback | 33 | $this->redirectUri = config('master.sso.redirect_uri'); // 3rd Party url redirect or url to handle callback |
34 | $this->targetUri = config('master.sso.target_uri'); // SSO Riau portal | 34 | $this->targetUri = config('master.sso.target_uri'); // SSO Riau portal |
35 | } | 35 | } |
36 | 36 | ||
37 | public function ssoRequest() { | 37 | public function ssoRequest() { |
38 | $state = base64_encode(random_bytes(40)); | 38 | $state = base64_encode(random_bytes(40)); |
39 | $query = http_build_query([ | 39 | $query = http_build_query([ |
40 | 'client_id' => $this->clientId, | 40 | 'client_id' => $this->clientId, |
41 | 'redirect_uri' => $this->redirectUri, | 41 | 'redirect_uri' => $this->redirectUri, |
42 | 'response_type' => 'code', | 42 | 'response_type' => 'code', |
43 | 'scope' => '', | 43 | 'scope' => '', |
44 | 'state' => $state, | 44 | 'state' => $state, |
45 | ]); | 45 | ]); |
46 | 46 | ||
47 | $_url = $this->targetUri ."oauth/authorize?" . $query; | 47 | $_url = $this->targetUri ."oauth/authorize?" . $query; |
48 | echo '<pre>';print_r($_url); | 48 | echo '<pre>';print_r($_url); |
49 | header("Location: " . $_url); | 49 | header("Location: " . $_url); |
50 | die(); | 50 | die(); |
51 | } | 51 | } |
52 | 52 | ||
53 | public function ssoCallback() { | 53 | public function ssoCallback() { |
54 | if (isset($_GET['code']) && !empty(($_GET['code']))) { | 54 | if (isset($_GET['code']) && !empty(($_GET['code']))) { |
55 | $_access_token = ''; | 55 | $_access_token = ''; |
56 | $_errors = ''; | 56 | $_errors = ''; |
57 | 57 | ||
58 | $_posts = [ | 58 | $_posts = [ |
59 | 'grant_type' => 'authorization_code', | 59 | 'grant_type' => 'authorization_code', |
60 | 'client_id' => $this->clientId, | 60 | 'client_id' => $this->clientId, |
61 | 'client_secret' => $this->clientSecret, | 61 | 'client_secret' => $this->clientSecret, |
62 | 'redirect_uri' => $this->redirectUri, | 62 | 'redirect_uri' => $this->redirectUri, |
63 | 'code' => $_GET['code'], | 63 | 'code' => $_GET['code'], |
64 | ]; | 64 | ]; |
65 | $arr_token = $this->__runCurl('POST', $this->targetUri."oauth/token", $_posts); | 65 | $arr_token = $this->__runCurl('POST', $this->targetUri."oauth/token", $_posts); |
66 | return $arr_token; | 66 | return $arr_token; |
67 | } | 67 | } |
68 | die('Something went wrong, please trace back your action!'); | 68 | die('Something went wrong, please trace back your action!'); |
69 | } | 69 | } |
70 | 70 | ||
71 | public function ssoUserInfo($access_token) { | 71 | public function ssoUserInfo($access_token) { |
72 | if ($access_token != '') { | 72 | if ($access_token != '') { |
73 | $header = [ | 73 | $header = [ |
74 | 'Content-Type: application/json', | 74 | 'Content-Type: application/json', |
75 | 'Authorization: Bearer '.$access_token, | 75 | 'Authorization: Bearer '.$access_token, |
76 | ]; | 76 | ]; |
77 | $user_info = $this->__runCurl('GET', $this->targetUri."api/userInfo", [], $header); | 77 | $user_info = $this->__runCurl('GET', $this->targetUri."api/userInfo", [], $header); |
78 | return $user_info; | 78 | return $user_info; |
79 | } | 79 | } |
80 | die('Something went wrong, please trace back your action!'); | 80 | die('Something went wrong, please trace back your action!'); |
81 | } | 81 | } |
82 | 82 | ||
83 | public function ssoAsnInfo($access_token, $nip) { | 83 | public function ssoAsnInfo($access_token, $nip) { |
84 | if ($access_token != '' && $nip != '') { | 84 | if ($access_token != '' && $nip != '') { |
85 | $header = [ | 85 | $header = [ |
86 | 'Content-Type: application/json', | 86 | 'Content-Type: application/json', |
87 | 'Authorization: Bearer '.$access_token, | 87 | 'Authorization: Bearer '.$access_token, |
88 | ]; | 88 | ]; |
89 | 89 | ||
90 | $query = http_build_query([ | 90 | $query = http_build_query([ |
91 | 'nip' => $nip | 91 | 'nip' => $nip |
92 | ]); | 92 | ]); |
93 | $asn_info = $this->__runCurl('POST', $this->targetUri."api/userData?".$query, [], $header); | 93 | $asn_info = $this->__runCurl('POST', $this->targetUri."api/userData?".$query, [], $header); |
94 | return $asn_info; | 94 | return $asn_info; |
95 | } | 95 | } |
96 | die('Something went wrong, please trace back your action!'); | 96 | die('Something went wrong, please trace back your action!'); |
97 | } | 97 | } |
98 | 98 | ||
99 | private function __runCurl($method = 'GET', $url, $data = [], $header = []) { | 99 | private function __runCurl($method = 'GET', $url, $data = [], $header = []) { |
100 | $error = ''; | 100 | $error = ''; |
101 | $ch = curl_init($url); | 101 | $ch = curl_init($url); |
102 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 102 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
103 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | 103 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
104 | if ($method == 'POST') { | 104 | if ($method == 'POST') { |
105 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | 105 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
106 | } | 106 | } |
107 | if (!empty($header)) { | 107 | if (!empty($header)) { |
108 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); | 108 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
109 | } | 109 | } |
110 | //curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds | 110 | //curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds |
111 | //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); | 111 | //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
112 | //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); | 112 | //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
113 | 113 | ||
114 | $response = curl_exec($ch); | 114 | $response = curl_exec($ch); |
115 | if (curl_error($ch)) { | 115 | if (curl_error($ch)) { |
116 | $error = curl_error($ch); | 116 | $error = curl_error($ch); |
117 | } | 117 | } |
118 | curl_close($ch); | 118 | curl_close($ch); |
119 | 119 | ||
120 | if ($error == '' && !empty($response)) { | 120 | if ($error == '' && !empty($response)) { |
121 | return $response; | 121 | return $response; |
122 | } else { | 122 | } else { |
123 | return $error; | 123 | return $error; |
124 | } | 124 | } |
125 | die('Something went wrong, please trace back your action!'); | 125 | die('Something went wrong, please trace back your action!'); |
126 | } | 126 | } |
127 | 127 | ||
128 | } | 128 | } |
129 | 129 | ||
130 | ?> | 130 | ?> |
131 | 131 |