forked from Kyon147/laravel-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthController.php
125 lines (111 loc) · 4.2 KB
/
AuthController.php
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
<?php
namespace Osiset\ShopifyApp\Traits;
use Illuminate\Contracts\View\View as ViewView;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Osiset\ShopifyApp\Actions\AuthenticateShop;
use Osiset\ShopifyApp\Exceptions\MissingAuthUrlException;
use Osiset\ShopifyApp\Exceptions\MissingShopDomainException;
use Osiset\ShopifyApp\Exceptions\SignatureVerificationException;
use Osiset\ShopifyApp\Messaging\Events\ShopAuthenticatedEvent;
use Osiset\ShopifyApp\Objects\Values\ShopDomain;
use Osiset\ShopifyApp\Util;
/**
* Responsible for authenticating the shop.
*/
trait AuthController
{
/**
* Installing/authenticating a shop.
*
* @throws MissingShopDomainException if both shop parameter and authenticated user are missing
*
* @return ViewView|RedirectResponse
*/
public function authenticate(Request $request, AuthenticateShop $authShop)
{
if ($request->missing('shop') && !$request->user()) {
// One or the other is required to authenticate a shop
throw new MissingShopDomainException('No authenticated user or shop domain');
}
// Get the shop domain
$shopDomain = $request->has('shop')
? ShopDomain::fromNative($request->get('shop'))
: $request->user()->getDomain();
// If the domain is obtained from $request->user()
if ($request->missing('shop')) {
$request['shop'] = $shopDomain->toNative();
}
// Run the action
[$result, $status] = $authShop($request);
if ($status === null) {
// Show exception, something is wrong
throw new SignatureVerificationException('Invalid HMAC verification');
} elseif ($status === false) {
if (!$result['url']) {
throw new MissingAuthUrlException('Missing auth url');
}
$shopDomain = $shopDomain->toNative();
$shopOrigin = $shopDomain ?? $request->user()->name;
event(new ShopAuthenticatedEvent($result['shop_id']));
return View::make(
'shopify-app::auth.fullpage_redirect',
[
'apiKey' => Util::getShopifyConfig('api_key', $shopOrigin),
'url' => $result['url'],
'host' => $request->get('host'),
'shopDomain' => $shopDomain,
'locale' => $request->get('locale'),
]
);
} else {
// Go to home route
return Redirect::route(
Util::getShopifyConfig('route_names.home'),
[
'shop' => $shopDomain->toNative(),
'host' => $request->get('host'),
'locale' => $request->get('locale'),
]
);
}
}
/**
* Get session token for a shop.
*
* @return ViewView
*/
public function token(Request $request)
{
$request->session()->reflash();
$shopDomain = ShopDomain::fromRequest($request);
$target = $request->query('target');
$query = parse_url($target, PHP_URL_QUERY);
$cleanTarget = $target;
if ($query) {
// remove "token" from the target's query string
$params = Util::parseQueryString($query);
$params['shop'] = $params['shop'] ?? $shopDomain->toNative() ?? '';
$params['host'] = $request->get('host');
$params['locale'] = $request->get('locale');
unset($params['token']);
$cleanTarget = trim(explode('?', $target)[0].'?'.http_build_query($params), '?');
} else {
$params = [
'shop' => $shopDomain->toNative() ?? '',
'host' => $request->get('host'),
'locale' => $request->get('locale'),
];
$cleanTarget = trim(explode('?', $target)[0].'?'.http_build_query($params), '?');
}
return View::make(
'shopify-app::auth.token',
[
'shopDomain' => $shopDomain->toNative(),
'target' => $cleanTarget,
]
);
}
}