-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhardwhere.patch
142 lines (138 loc) · 5.04 KB
/
hardwhere.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
diff --git a/app/Http/Controllers/HardWhereController.php b/app/Http/Controllers/HardWhereController.php
new file mode 100644
index 000000000..9d3d8ea9e
--- /dev/null
+++ b/app/Http/Controllers/HardWhereController.php
@@ -0,0 +1,93 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Support\Facades\Auth;
+
+use App\Models\Setting;
+use App\Helpers\Helper;
+use Illuminate\Support\Facades\Http;
+use Illuminate\Support\Str;
+
+/**
+ * This class controls all actions related to qr codes for assets
+ * the Snipe-IT Asset Management application.
+ *
+ * @version v0.3
+ * @author Aron Heinecke
+ */
+class HardWhereController extends Controller
+{
+ protected $qrCodeDimensions = array('height' => 3.5, 'width' => 3.5);
+ protected $barCodeDimensions = array('height' => 2, 'width' => 22);
+
+ public function __construct()
+ {
+ parent::__construct();
+ }
+
+
+ /**
+ * Return login json data as string
+ *
+ * @author Aron Heinecke
+ * @return Response
+ */
+ private function getLoginJson()
+ {
+ $id = Auth::id();
+ $user = Auth::user();
+ $user->tokens->each(function($token, $key) {
+ if ($token->name == 'HardWhere-App') {
+ $token->delete();
+ }
+ });
+ $token = $user->createToken('HardWhere-App')->accessToken;
+ $url = \App::make('url')->to('/');
+
+ return json_encode(["url" => $url, "token" => $token ]);
+ }
+
+ /**
+ * Return a QR code for the asset
+ *
+ * @author Aron Heinecke
+ * @return Response
+ */
+ public function getQRCode()
+ {
+ $data = $this->getLoginJson();
+
+ $barcode = new \Com\Tecnick\Barcode\Barcode();
+ $barcode_obj = $barcode->getBarcodeObj('QRCODE', $data, 921,921, 'black', array(-5, -5, 0, -5));
+ return response($barcode_obj->getSvgCode())->header('Content-type', 'image/svg+xml');
+ }
+
+ public function showWebView()
+ {
+ $id = Auth::id();
+ $user = Auth::user();
+ $user->tokens->each(function($token, $key) {
+ if ($token->name == 'HardWhere-WebView') {
+ $token->delete();
+ }
+ });
+ $token = $user->createToken('HardWhere-WebView')->accessToken;
+ $login_token = Str::random(40);
+// $response = Http::post('http://localhost:8000/HardWhere/internal/autologin', [
+// 'api_token' => $token,
+// 'login_token' => $login_token,
+// ]);
+ $ch = curl_init('http://localhost:8000/HardWhere/internal/autologin');
+ curl_setopt($ch, CURLOPT_POST, 1);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
+ 'api_token' => $token,
+ 'login_token' => $login_token,
+ ]));
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
+ $result = curl_exec($ch);
+ curl_close($ch);
+ return redirect('/HardWhere/autologin/' . $login_token);
+ }
+}
diff --git a/resources/views/layouts/default.blade.php b/resources/views/layouts/default.blade.php
index 37fdcc2e1..b6f67f9b8 100644
--- a/resources/views/layouts/default.blade.php
+++ b/resources/views/layouts/default.blade.php
@@ -351,6 +351,16 @@
<i class="fa fa-user-secret fa-fw" aria-hidden="true"></i> Manage API Keys
</a>
</li>
+ <li>
+ <a href="{{ route('user.hardwhere.login') }}">
+ <i class="fa fa-user-secret fa-fw"></i> HardWhere App Login
+ </a>
+ </li>
+ <li>
+ <a href="{{ route('user.hardwhere.webview') }}">
+ <i class="fa fa-user-secret fa-fw"></i> HardWhere Webview
+ </a>
+ </li>
@endcan
<li class="divider"></li>
<li>
diff --git a/routes/web.php b/routes/web.php
index fc26be38c..662f09a05 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -244,6 +244,10 @@ Route::group([ 'prefix' => 'account', 'middleware' => ['auth']], function () {
Route::post('password', [ 'uses' => 'ProfileController@passwordSave' ]);
Route::get('api', [ 'as' => 'user.api', 'uses' => 'ProfileController@api' ]);
+
+
+ Route::get('hardwhere/login', ['as' => 'user.hardwhere.login', 'uses' => 'HardWhereController@getQRCode']);
+ Route::get('hardwhere/webview', ['as' => 'user.hardwhere.webview', 'uses' => 'HardWhereController@showWebView']);
# View Assets
Route::get('view-assets', [ 'as' => 'view-assets', 'uses' => 'ViewAssetsController@getIndex' ]);
@@ -468,4 +472,4 @@ Route::group(['middleware' => 'web'], function () {
Auth::routes();
-Route::get('/health', [ 'as' => 'health', 'uses' => 'HealthController@get']);
\ No newline at end of file
+Route::get('/health', [ 'as' => 'health', 'uses' => 'HealthController@get']);