Completed
Push — master ( 1197be...9384fc )
by Cedric
06:46
created
Severity

Upgrade to new PHP Analysis Engine

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')) {
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('ourKey', $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 define some routes.
75
 * At time of writing Confluence refuses to contact us if the route contains .php so we need to prettify our URLS.
76
 * Our sample is not the best way to do it, but it's just for the demo.
77
 */
78
79
80
$app = new Application();
81
82
83
/**
84
 * Our sample descriptor is available at http://atlassian-connect.dev/descriptor.json
85
 *
86
 * This is the bare minimal descriptor to be defined.
87
 *
88
 * You can validate your descriptor
89
 * @see https://atlassian-connect-validator.herokuapp.com/validate
90
 */
91
$app->get('/descriptor.json', function (Request $request) {
92
93
    /*
94
     * We have to construct the correct URL in order to confluence be able to contact us
95
     * And the scheme MUST be https in order to confluence accept it.
96
     */
97
    $host = $request->getHttpHost();
98
    $scheme = $request->getScheme();
99
100
    if (preg_match('/\.ngrok\.io/', $host)) {
101
        $scheme = 'https';
102
    }
103
104
105
    return json_encode([
106
        'authentication' => [
107
            'type' => 'jwt'
108
        ],
109
        'baseUrl'        => $scheme . '://' . $host,
110
        'scopes'         => [
111
            'read'
112
        ],
113
        'key'            => 'ourKey',
114
        'lifecycle'      => [
115
            'installed' => '/installed',
116
            'enabled'   => '/enabled'
117
        ],
118
    ]);
119
});
120
121
/**
122
 * When we install our add-on into any atlassian app, they will contact us at the URL we define in the 'installed' lifecycle.
123
 * They will give us a payload containing the sharedSecret we'll need to use to sign our request.
124
 * For the demo we just save the content to a file.
125
 */
126
$app->post('/installed', function (Request $request) {
127
128
    $payload = $request->getContent();
129
    file_put_contents('payload.json', $payload);
130
131
    /**
132
     * Be sure to send a 200 OK response, or the app will tell you that your plugin can't be installed.
133
     */
134
    return new \Symfony\Component\HttpFoundation\Response('OK', 200);
135
});
136
137
138
/**
139
 * Even if the documentation tell's you the only needed webhook is the installed one,
140
 * they won't let you enable the add-on unless you define the route to you 'enabled' webhook.
141
 */
142
$app->post('/enabled', function () {
143
    /**
144
     * Be sure to send a 200 OK response, or the app will tell you that your plugin can't be enabled.
145
     */
146
    return new \Symfony\Component\HttpFoundation\Response('OK', 200);
147
});
148
149
//Catch all route to run our test code
150
$app->match('{url}', function ($url) use ($client) {
0 ignored issues
show
The parameter $url is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
151
    $response = $client->get('space');
152
153
    var_dump($response->getBody()->getContents());
154
})->assert('url', '.+');
155
156
157
$app->run();
158