-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
280 lines (267 loc) · 10.2 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
// This page must be configured as the Callback URL when setting up Application Links
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('settings.php');
require_once('atlassianapi.php');
require_once('utils.php');
$error = '';
$notice = '';
$testResults = '';
$japiConfluence = new AtlassianAPI(ATLASSIAN_SERVER, 'Confluence', CONSUMER_KEY, PRIVATE_KEY_PATH, OAUTH_TOKENS_PATH_CONFLUENCE, ERROR_LOG_FILE, ERROR_LOG_SENDER, ERROR_LOG_RECIPIENT, ERROR_LOG_SUBJECT, TEST_MODE);
$japiJIRA = new AtlassianAPI(ATLASSIAN_SERVER, 'JIRA', CONSUMER_KEY, PRIVATE_KEY_PATH, OAUTH_TOKENS_PATH_JIRA, ERROR_LOG_FILE, ERROR_LOG_SENDER, ERROR_LOG_RECIPIENT, ERROR_LOG_SUBJECT, TEST_MODE);
if ($japiConfluence->LastErrorCode == 0 && $japiJIRA->LastErrorCode == 0) {
$deleteAccessToken = array_key_exists('deleteAccessToken', $_POST);
$getAccessToken = array_key_exists('oauth_token', $_GET);
$getRequestToken = array_key_exists('getRequestToken', $_POST);
$testAccessToken = array_key_exists('testAccessToken', $_POST);
if (array_key_exists('targetApp', $_POST)) {
$targetApp = $_POST['targetApp'];
switch ($targetApp) {
case 'Confluence':
$japi = $japiConfluence;
$testUrl = CONFLUENCE_BASE_URL . SAMPLE_PAGE_ID;
break;
case 'JIRA':
$japi = $japiJIRA;
$testUrl = JIRA_TEST_URL;
break;
default:
$japi = null;
break;
}
} else
$targetApp = null;
if ($getAccessToken) {
$reqToken = $_GET['oauth_token'];
if ($reqToken == $japiConfluence->RequestToken) {
$japi = $japiConfluence;
$targetApp = 'Confluence';
} elseif ($reqToken == $japiJIRA->RequestToken) {
$japi = $japiJIRA;
$targetApp = 'JIRA';
} else
$error = "The received Request Token doesn't match any we have!";
}
if (TEST_MODE)
echo ('<pre style=text-align:left>');
if ($deleteAccessToken) {
$japi->DeleteAccessToken();
$notice = $targetApp . ' Access Token deleted.';
}
if ($getRequestToken) {
if ($japi->GetRequestToken()) {
$notice = "Got a Request Token for $targetApp, but if you're seeing this, we didn't automatically attempt to Authorize.<br>";
$notice .= $japi->AuthorizeRequest(!TEST_MODE); // The Consumer Callback URL in JIRA must be set to this page!
} else {
$error = "Error ({$japi->LastErrorCode}) getting initial Request Token for $targetApp:<br>{$japi->LastErrorMessage}";
}
}
if ($getAccessToken) {
if ($japi->GetAccessToken())
$notice = "Access Token obtained for $targetApp.";
else
$error = "Error ({$japi->LastErrorCode}) getting Access Token for $targetApp:<br>{$japi->LastErrorMessage}";
}
if ($testAccessToken) {
//$args = array('status' => 'any');
$results = $japi->SendRequest($testUrl);//, $args);
if ($results !== FALSE) {
$notice = "Test Passed for $targetApp!";
$testResults = "<p>Test Results:</p><p><a href=\"$testUrl\">$testUrl</a></p>";
$testResults .= "<pre style=text-align:left>" . print_r($results, TRUE) . "</pre>";
} else {
$errMsg = $japi->LastErrorMessage;
$errJson = json_decode($errMsg);
if ($errJson == null)
$smartError = $errMsg;
else {
if ($errJson->statusCode == 403 && $errJson->message == 'Parent page view is restricted')
$smartError = $errMsg . "<br><br>Are you certain that the Application Link is setup in $targetApp?";
}
$error = "Error ({$japi->LastErrorCode}) testing Access Token:<br>$smartError";
}
}
if(TEST_MODE)
echo ('</pre>');
} else { // if ($japiConfluence->LastErrorCode != 0 || $japiJIRA->LastErrorCode != 0)
$error = '';
if ($japiConfluence->LastErrorCode != 0)
$error = "Error instantiating \$japiConfluence ({$japiConfluence->LastErrorCode}):<br>" .
"{$japiConfluence->LastErrorMessage}<br>\n";
if ($japiJIRA->LastErrorCode != 0)
$error .= "Error instantiating \$japiJIRA ({$japiJIRA->LastErrorCode}):<br>" .
"{$japiJIRA->LastErrorMessage}\n";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Atlassian OAuth Configuration</title>
<style>
html { box-sizing: border-box; } /* SO much better! */
*, *:before, *:after { box-sizing: inherit; }
a { text-decoration: none; }
a:hover { text-decoration: underline; }
body { font-family: sans-serif; text-align: center; }
button { font-size: x-large; }
div { padding: 10px 0; }
fieldset { margin: 10px; }
pre { white-space:pre-wrap; }
.alert { color: red; }
.centerbox {
display: flex;
flex-wrap: wrap; /* optional. only if you want the items to wrap */
justify-content: center; /* for horizontal alignment */
align-items: center; /* for vertical alignment */
}
.column {
border: 1px solid silver;
float: left;
width: 50%;
}
#error {
background-color: Red;
color: White;
text-align: left;
}
.good { color: green; }
#notice {
background-color: LightGreen;
color: DarkGreen;
}
</style>
</head>
<body>
<?php if ($notice != '') { ?>
<div id="notice"><?php echo $notice; ?></div>
<?php } ?>
<?php if ($error != '') { ?>
<div id="error"><?php echo $error; ?></div>
<?php } ?>
<h1>Atlassian OAuth Configuration</h1>
<div>
<div class="column">
<h2>Confluence</h2>
<?php
if ($japiConfluence->HasAccessToken()) {
?>
<p><b class='good'>An Access Token is present.</b></p>
<table style="margin: 0 auto;">
<tr>
<td style="text-align: right;">Expires:</td>
<td style="text-align: left;"><?php echo $japiConfluence->AccessTokenExpirationDate()->format('Y-m-d') . ' (' . $japiConfluence->AccessTokenValidFor() . ')'; ?></td>
</tr>
<tr>
<td>Auth Expires:</td>
<td style="text-align: left;"><?php echo $japiConfluence->AccessTokenAuthExpirationDate()->format('Y-m-d') . ' (' . $japiConfluence->AccessTokenAuthValidFor() . ')'; ?></td>
</tr>
</table>
<fieldset>
<form action="./" method="post">
<input type="hidden" name="targetApp" value="Confluence"/>
<p><button type="submit" name="deleteAccessToken">Delete Token</button></p>
<p>Deleting the Access Token will disable the job until a new Access Token is obtained.<br>
This may also be necessary if the current Token has become expired or invalidated.</p>
<p>Note that deleting an Access Token will not revoke its authorization in JIRA.<br>
<a href="<?php echo (ATLASSIAN_SERVER); ?>/wiki/users/revokeoauthtokens.action"
target="_blank">View or Revoke Access Tokens</a>
</p>
</form>
</fieldset>
<fieldset>
<form action="./" method="post">
<input type="hidden" name="targetApp" value="Confluence"/>
<p><button type="submit" name="testAccessToken">Test Access</button></p>
</form>
</fieldset>
<?php
} else {
?>
<b><span class='alert'>Access Token not present.</span><br>
The job will be unable to access app until an Access Token is available.</b>
<p>Obtaining an Token will require an interactive user login:</p>
<div class="centerbox">
<ol style="text-align: left;">
<li title="Opens in new window/tab">
<a href="<?php echo (ATLASSIAN_SERVER); ?>/secure/Logout!default.jspa"
target="_blank">Logout of your normal JIRA user account</a> ⧉
</li>
<li>Login with a service JIRA user account<br>
because the token shouldn't be linked with regular user accounts</li>
<li>Come back to this page and click the button below:</li>
</ol>
</div>
<form action="./" method="post">
<input type="hidden" name="targetApp" value="Confluence"/>
<p><button type="submit" name="getRequestToken">Get Token</button></p>
</form>
<?php
}
?>
</div>
<div class="column">
<h2>JIRA</h2>
<?php
if ($japiJIRA->HasAccessToken()) {
?>
<p><b class='good'>An Access Token is present.</b></p>
<table style="margin: 0 auto;">
<tr>
<td style="text-align: right;">Expires:</td>
<td style="text-align: left;"><?php echo $japiJIRA->AccessTokenExpirationDate()->format('Y-m-d') . ' (' . $japiJIRA->AccessTokenValidFor() . ')'; ?></td>
</tr>
<tr>
<td>Auth Expires:</td>
<td style="text-align: left;"><?php echo $japiJIRA->AccessTokenAuthExpirationDate()->format('Y-m-d') . ' (' . $japiJIRA->AccessTokenAuthValidFor() . ')'; ?></td>
</tr>
</table>
<fieldset>
<form action="./" method="post">
<input type="hidden" name="targetApp" value="JIRA"/>
<p><button type="submit" name="deleteAccessToken">Delete Token</button></p>
<p>Deleting the Access Token will disable the job until a new Access Token is obtained.<br>
This may also be necessary if the current Token has become expired or invalidated.</p>
<p>Note that deleting an Access Token will not revoke its authorization in JIRA.<br>
<a href="<?php echo (ATLASSIAN_SERVER); ?>/plugins/servlet/oauth/users/access-tokens"
target="_blank">View or Revoke Access Tokens</a>
</p>
</form>
</fieldset>
<fieldset>
<form action="./" method="post">
<input type="hidden" name="targetApp" value="JIRA"/>
<p><button type="submit" name="testAccessToken">Test Access</button></p>
</form>
</fieldset>
<?php
} else {
?>
<b><span class='alert'>Access Token not present.</span><br>
The job will be unable to access app until an Access Token is available.</b>
<p>Obtaining an Token will require an interactive user login:</p>
<div class="centerbox">
<ol style="text-align: left;">
<li title="Opens in new window/tab">
<a href="<?php echo (ATLASSIAN_SERVER); ?>/secure/Logout!default.jspa"
target="_blank">Logout of your normal JIRA user account</a> ⧉
</li>
<li>Login with a service JIRA user account<br>
because the token shouldn't be linked with regular user accounts</li>
<li>Come back to this page and click the button below:</li>
</ol>
</div>
<form action="./" method="post">
<input type="hidden" name="targetApp" value="JIRA"/>
<p><button type="submit" name="getRequestToken">Get Token</button></p>
</form>
<?php
}
?>
</div>
</div>
<div>
<p style="clear:both;"><?php echo $testResults; ?></p>
</div>
</body>
</html>