These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of the adlogix/guzzle-atlassian-connect package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | */ |
||
8 | |||
9 | require_once 'vendor/autoload.php'; |
||
10 | |||
11 | use Adlogix\GuzzleAtlassianConnect\Middleware\ConnectMiddleware; |
||
12 | use Adlogix\GuzzleAtlassianConnect\Security\QueryParamAuthentication; |
||
13 | use GuzzleHttp\Client; |
||
14 | use GuzzleHttp\HandlerStack; |
||
15 | use Silex\Application; |
||
16 | use Symfony\Component\HttpFoundation\Request; |
||
17 | |||
18 | /** |
||
19 | * See the 'installed' webhook on how to recover this payload. |
||
20 | * |
||
21 | * The sharedSecret is given by the application we installed the add-on to, |
||
22 | * this is needed to sign our request and to validate the requests from the application. |
||
23 | */ |
||
24 | $sharedSecret = ''; |
||
25 | $baseUrl = ''; |
||
26 | View Code Duplication | if (file_exists('payload.json')) { |
|
0 ignored issues
–
show
|
|||
27 | $payload = json_decode(file_get_contents('payload.json')); |
||
28 | $sharedSecret = $payload->sharedSecret; |
||
29 | $baseUrl = $payload->baseUrl; |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Here we create the middleware; |
||
34 | * for the authentication method we give the key we defined in our descriptor, |
||
35 | * and the second parameter is the sharedSecret given by atlassian when we installed the add-on. |
||
36 | * |
||
37 | * For more info on the descriptor, |
||
38 | * @see https://developer.atlassian.com/static/connect/docs/latest/modules/ |
||
39 | * |
||
40 | * For more info on how to get the sharedKey, you need to define the installed lifecycle in your descriptor. |
||
41 | * @see https://developer.atlassian.com/static/connect/docs/latest/modules/lifecycle.html |
||
42 | * |
||
43 | * The second parameter ro create the middleware is the full path to the application we want to connect to. |
||
44 | * For the demo we use Confluence which resides at http://atlassian-confluence.dev/confluence |
||
45 | * |
||
46 | * If your sharedSecret is empty, there's no need to try to contact the application, |
||
47 | * so be sure you received the 'enabled' webhook call before trying to contact it. |
||
48 | */ |
||
49 | $middleware = new ConnectMiddleware( |
||
50 | new QueryParamAuthentication('eu.adlogix.atlassian-connect', $sharedSecret), |
||
51 | $baseUrl |
||
52 | ); |
||
53 | |||
54 | |||
55 | /** |
||
56 | * We start to build ou Guzzle Client by defining the HandlerStack and pushing our middleware in it. |
||
57 | */ |
||
58 | $stack = HandlerStack::create(); |
||
59 | $stack->push($middleware); |
||
60 | |||
61 | /** |
||
62 | * And the Client creation |
||
63 | */ |
||
64 | $client = new Client( |
||
65 | [ |
||
66 | 'base_uri' => $baseUrl . '/rest/api/', |
||
67 | 'handler' => $stack, |
||
68 | 'debug' => true |
||
69 | ] |
||
70 | ); |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Since [name-your-app] needs to reach our application to post some information, like the sharedSecret, we have to |
||
75 | * define some routes. |
||
76 | * At time of writing Confluence refuses to contact us if the route contains .php so we need to prettify our URLS. |
||
77 | * Our sample is not the best way to do it, but it's just for the demo. |
||
78 | */ |
||
79 | |||
80 | |||
81 | $app = new Application(); |
||
82 | |||
83 | |||
84 | /** |
||
85 | * Our sample descriptor is available at http://atlassian-connect.dev/descriptor.json |
||
86 | * |
||
87 | * This is the bare minimal descriptor to be defined. |
||
88 | * |
||
89 | * You can validate your descriptor |
||
90 | * @see https://atlassian-connect-validator.herokuapp.com/validate |
||
91 | */ |
||
92 | $app->get('/descriptor.json', function (Request $request) { |
||
93 | |||
94 | /* |
||
95 | * We have to construct the correct URL in order to confluence be able to contact us |
||
96 | * And the scheme MUST be https in order to confluence accept it. |
||
97 | */ |
||
98 | $host = $request->getHttpHost(); |
||
99 | $scheme = $request->getScheme(); |
||
100 | |||
101 | if (preg_match('/\.ngrok\.io/', $host)) { |
||
102 | $scheme = 'https'; |
||
103 | } |
||
104 | |||
105 | |||
106 | return json_encode([ |
||
107 | 'authentication' => [ |
||
108 | 'type' => 'jwt' |
||
109 | ], |
||
110 | 'baseUrl' => $scheme . '://' . $host, |
||
111 | 'scopes' => [ |
||
112 | 'read' |
||
113 | ], |
||
114 | 'key' => 'ourKey', |
||
115 | 'lifecycle' => [ |
||
116 | 'installed' => '/installed', |
||
117 | 'enabled' => '/enabled' |
||
118 | ], |
||
119 | ]); |
||
120 | }); |
||
121 | |||
122 | /** |
||
123 | * When we install our add-on into any atlassian app, they will contact us at the URL we define in the 'installed' |
||
124 | * lifecycle. |
||
125 | * They will give us a payload containing the sharedSecret we'll need to use to sign our request. |
||
126 | * For the demo we just save the content to a file. |
||
127 | */ |
||
128 | $app->post('/installed', function (Request $request) { |
||
129 | |||
130 | $payload = $request->getContent(); |
||
131 | file_put_contents('payload.json', $payload); |
||
132 | |||
133 | /** |
||
134 | * Be sure to send a 200 OK response, or the app will tell you that your plugin can't be installed. |
||
135 | */ |
||
136 | return new \Symfony\Component\HttpFoundation\Response('OK', 200); |
||
137 | }); |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Even if the documentation tell's you the only needed webhook is the installed one, |
||
142 | * they won't let you enable the add-on unless you define the route to you 'enabled' webhook. |
||
143 | */ |
||
144 | $app->post('/enabled', function () { |
||
145 | /** |
||
146 | * Be sure to send a 200 OK response, or the app will tell you that your plugin can't be enabled. |
||
147 | */ |
||
148 | return new \Symfony\Component\HttpFoundation\Response('OK', 200); |
||
149 | }); |
||
150 | |||
151 | //Catch all route to run our test code |
||
152 | $app->match('{url}', function () use ($client) { |
||
153 | $response = $client->get('space'); |
||
154 | |||
155 | var_dump($response->getBody()->getContents()); |
||
0 ignored issues
–
show
|
|||
156 | })->assert('url', '.+'); |
||
157 | |||
158 | |||
159 | $app->run(); |
||
160 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.