Commit 33cf46a37222934266698669dd01a6a54c1d79c1

Authored by oji
1 parent 78ccfa2000
Exists in master

change code variable

Showing 2 changed files with 1 additions and 1 deletions Inline Diff

No preview for this file type
src/SsoClientLibrary.php
1 <?php 1 <?php
2 2
3 namespace SsoRiau; 3 namespace SsoRiau;
4 4
5 /* 5 /*
6 * Nama : File SSO Riau client library 6 * Nama : File SSO Riau client library
7 * Tujuan : File ini dibuat dalam bentuk class objek sehingga dapat di integrasikan 7 * Tujuan : File ini dibuat dalam bentuk class objek sehingga dapat di integrasikan
8 * dengan aplikasi pihak ketiga yang ingin terhubung menggunakan aplikasi SSO 8 * dengan aplikasi pihak ketiga yang ingin terhubung menggunakan aplikasi SSO
9 * Deskripsi Variable : 9 * Deskripsi Variable :
10 * $clientId 10 * $clientId
11 * - merupakan identitas unik ID yang diperoleh saat mendaftarkan 11 * - merupakan identitas unik ID yang diperoleh saat mendaftarkan
12 * aplikasi pihak ketiga pada aplikasi SSO 12 * aplikasi pihak ketiga pada aplikasi SSO
13 * $clientSecret 13 * $clientSecret
14 * - merupakan identitas unik Secret (rahasia) yang digunakan saat akses 14 * - merupakan identitas unik Secret (rahasia) yang digunakan saat akses
15 * kepada aplikasi SSO sehingga diizinkan 15 * kepada aplikasi SSO sehingga diizinkan
16 * $redirectUri 16 * $redirectUri
17 * - merupakan alamat url yang akan mengelola hasil informasi login SSO 17 * - merupakan alamat url yang akan mengelola hasil informasi login SSO
18 * $targetUri 18 * $targetUri
19 * - adalah alamat website aplikasi SSO 19 * - adalah alamat website aplikasi SSO
20 * 20 *
21 */ 21 */
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 = ''; // 3rd Party client Id 31 $this->clientId = ''; // 3rd Party client Id
32 $this->clientSecret = ''; // 3rd Party client secret 32 $this->clientSecret = ''; // 3rd Party client secret
33 $this->redirectUri = ''; // 3rd Party url redirect or url to handle callback 33 $this->redirectUri = ''; // 3rd Party url redirect or url to handle callback
34 $this->targetUri = 'https://sso.riau.go.id/'; // SSO Riau portal 34 $this->targetUri = 'https://sso.riau.go.id/'; // 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 $url = parse_url($this->redirectUri); 54 $url = parse_url($this->redirectUri);
55 $code = str_replace($url['path'] . '?code=','',$_SERVER['REQUEST_URI']); 55 $code = str_replace($url['path'] . '?code=','',$_SERVER['REQUEST_URI']);
56 $code = explode('&',$code); 56 $code = explode('&',$code);
57 $code = $code[0] ?? ''; 57 $code = $code[0] ?? '';
58 if ($code != '') { 58 if ($code != '') {
59 $_access_token = ''; 59 $_access_token = '';
60 $_errors = ''; 60 $_errors = '';
61 61
62 $_posts = [ 62 $_posts = [
63 'grant_type' => 'authorization_code', 63 'grant_type' => 'authorization_code',
64 'client_id' => $this->clientId, 64 'client_id' => $this->clientId,
65 'client_secret' => $this->clientSecret, 65 'client_secret' => $this->clientSecret,
66 'redirect_uri' => $this->redirectUri, 66 'redirect_uri' => $this->redirectUri,
67 'code' => $_GET['code'], 67 'code' => $code,
68 ]; 68 ];
69 $arr_token = $this->__runCurl('POST', $this->targetUri."oauth/token", $_posts); 69 $arr_token = $this->__runCurl('POST', $this->targetUri."oauth/token", $_posts);
70 return $arr_token; 70 return $arr_token;
71 } 71 }
72 die('Something went wrong, please trace back your action!'); 72 die('Something went wrong, please trace back your action!');
73 } 73 }
74 74
75 public function ssoUserInfo($access_token) { 75 public function ssoUserInfo($access_token) {
76 if ($access_token != '') { 76 if ($access_token != '') {
77 $header = [ 77 $header = [
78 'Content-Type: application/json', 78 'Content-Type: application/json',
79 'Authorization: Bearer '.$access_token, 79 'Authorization: Bearer '.$access_token,
80 ]; 80 ];
81 $user_info = $this->__runCurl('GET', $this->targetUri."api/userInfo", [], $header); 81 $user_info = $this->__runCurl('GET', $this->targetUri."api/userInfo", [], $header);
82 return $user_info; 82 return $user_info;
83 } 83 }
84 die('Something went wrong, please trace back your action!'); 84 die('Something went wrong, please trace back your action!');
85 } 85 }
86 86
87 public function ssoAsnInfo($access_token, $nip) { 87 public function ssoAsnInfo($access_token, $nip) {
88 if ($access_token != '' && $nip != '') { 88 if ($access_token != '' && $nip != '') {
89 $header = [ 89 $header = [
90 'Content-Type: application/json', 90 'Content-Type: application/json',
91 'Authorization: Bearer '.$access_token, 91 'Authorization: Bearer '.$access_token,
92 ]; 92 ];
93 93
94 $query = http_build_query([ 94 $query = http_build_query([
95 'nip' => $nip 95 'nip' => $nip
96 ]); 96 ]);
97 $asn_info = $this->__runCurl('POST', $this->targetUri."api/userData?".$query, [], $header); 97 $asn_info = $this->__runCurl('POST', $this->targetUri."api/userData?".$query, [], $header);
98 return $asn_info; 98 return $asn_info;
99 } 99 }
100 die('Something went wrong, please trace back your action!'); 100 die('Something went wrong, please trace back your action!');
101 } 101 }
102 102
103 private function __runCurl($method = 'GET', $url, $data = [], $header = []) { 103 private function __runCurl($method = 'GET', $url, $data = [], $header = []) {
104 $error = ''; 104 $error = '';
105 $ch = curl_init($url); 105 $ch = curl_init($url);
106 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 106 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
107 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 107 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
108 if ($method == 'POST') { 108 if ($method == 'POST') {
109 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 109 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
110 } 110 }
111 if (!empty($header)) { 111 if (!empty($header)) {
112 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 112 curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
113 } 113 }
114 //curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds 114 //curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
115 //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 115 //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
116 //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 116 //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
117 117
118 $response = curl_exec($ch); 118 $response = curl_exec($ch);
119 if (curl_error($ch)) { 119 if (curl_error($ch)) {
120 $error = curl_error($ch); 120 $error = curl_error($ch);
121 } 121 }
122 curl_close($ch); 122 curl_close($ch);
123 123
124 if ($error == '' && !empty($response)) { 124 if ($error == '' && !empty($response)) {
125 return $response; 125 return $response;
126 } else { 126 } else {
127 return $error; 127 return $error;
128 } 128 }
129 die('Something went wrong, please trace back your action!'); 129 die('Something went wrong, please trace back your action!');
130 } 130 }
131 131
132 public function ssoLogout($url_back = '') { 132 public function ssoLogout($url_back = '') {
133 $query = http_build_query([ 133 $query = http_build_query([
134 'url_back' => $url_back, 134 'url_back' => $url_back,
135 ]); 135 ]);
136 136
137 $_url = $this->targetUri ."sso/logout?" . $query; 137 $_url = $this->targetUri ."sso/logout?" . $query;
138 header("Location: " . $_url); 138 header("Location: " . $_url);
139 die(); 139 die();
140 } 140 }
141 141
142 } 142 }