Skip to content

Commit ba13680

Browse files
committed
+oauth2 through cloud api
1 parent e31fe89 commit ba13680

File tree

11 files changed

+195
-96
lines changed

11 files changed

+195
-96
lines changed

accounts.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
}
1616

1717
// Save credentials to cookie
18-
$auth = $username . ':' . $_POST['pass'];
19-
do_login($url, $auth);
18+
$auth = 'Basic ' . base64_encode($username . ':' . $_POST['pass']);
19+
do_login($url, $auth, $username);
2020

2121
// Save user to local db for preferences
2222
try {
@@ -45,7 +45,7 @@
4545
unset($accounts[$index]);
4646
array_unshift($accounts, $account);
4747

48-
do_login('', '', $accounts);
48+
do_session($accounts);
4949

5050
return do_redirect('accounts');
5151
}
@@ -59,7 +59,7 @@
5959
}
6060

6161
unset($accounts[$index]);
62-
do_login('', '', $accounts);
62+
do_session($accounts);
6363

6464
return do_redirect('accounts');
6565
}
@@ -88,7 +88,7 @@
8888
}
8989

9090
// Reset cookies
91-
do_login('', '');
91+
do_session(get_accounts());
9292

9393
$_title = 'Accounts';
9494
include 'tpl.header.php';
@@ -108,10 +108,9 @@
108108

109109
<ul>
110110
<?foreach ($accounts as $i => $account):
111-
$_url = parse_url($account->url);
112111
?>
113112
<li class="<?if ($account->active):?>active-account<?endif?>">
114-
<?= $account->user ?> @ <?= $_url['host'] ?>
113+
<?= html($account->getLabel()) ?>
115114
<?if (!$account->active):?>
116115
(<a href="?switch=<?= $i ?>">switch</a>)
117116
(<a href="?unlink=<?= $i ?>">x</a>)
@@ -191,7 +190,6 @@
191190
<h2>Add account</h2>
192191

193192
<?php
194-
$_COOKIE['JIRA_URL'] = '';
195193
include 'tpl.login.php';
196194
?>
197195

auth.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
echo '<p>You are:</p>';
1111
echo '<pre>';
1212

13+
$info = ['unauth_ok' => true];
1314
$account = jira_get('/rest/api/3/myself', null, $error, $info);
1415
print_r($account);
15-
$error and print_r($info['error']);
16+
if ($error) {
17+
echo $info['response'] . "\n";
18+
print_r($info);
19+
exit;
20+
}
1621

1722
print_r($user);
1823

@@ -59,7 +64,7 @@
5964
// $db->update('users', array('jira_timezone' => $jiraUsername->timeZone), array('id' => $user->id));
6065

6166
// Save credentials to cookie
62-
do_login($url, $info['JIRA_AUTH']);
67+
do_login($url, $info['JIRA_AUTH'], $username);
6368

6469
return do_redirect('index');
6570
}
@@ -73,3 +78,5 @@
7378
<? include 'tpl.login.php' ?>
7479

7580
<p>Make a <em>Personal API token</em> on <a href="https://id.atlassian.com/manage/api-tokens">https://id.atlassian.com/manage/api-tokens</a>.</p>
81+
82+
<p><a href="oauth2-start.php">Or try the brandnew Cloud OAuth2 experience!</a></p>

env.php.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ const DB_PATH = 'db/jira.sqlite3';
55
const SECRET = "Don't tell anyone, but my favorite color is PINK! Also known as #FFC0CB.";
66

77
const DEBUG = false;
8+
9+
const OAUTH_CLENT_ID = 'askjdhkdfjhsldasddffdsdfjlasdjfh';
10+
const OAUTH_CLENT_SECRET = 'kjghJHGJHFHgfjhhlKJLKJkjgjhfgjhFGKUJHkjhLKJlkjKJGBjhgfJHgkjHLJtt';

inc.account.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
class Account {
4+
5+
public function __construct(
6+
public string $apiUrl,
7+
public string $auth,
8+
public string $username,
9+
public string $server,
10+
public bool $active,
11+
) {}
12+
13+
public function getServerLabel() : string {
14+
return parse_url($this->server, PHP_URL_HOST);
15+
}
16+
17+
public function getLabel() : string {
18+
return $this->username . ' @ ' . $this->getServerLabel();
19+
}
20+
21+
public function pack() : array {
22+
return [$this->apiUrl, $this->auth, $this->username, $this->server];
23+
}
24+
25+
static public function unpackAll( array $infos ) : array {
26+
$accounts = [];
27+
foreach ( $infos AS $i => $info ) {
28+
$accounts[] = static::unpackOne($info, $i == 0);
29+
}
30+
return $accounts;
31+
}
32+
33+
static public function fromLogin( string $apiUrl, string $auth, string $username, string $server ) : self {
34+
return new static($apiUrl, $auth, $username, $server);
35+
}
36+
37+
static public function unpackOne( array $info, bool $active ) : self {
38+
return new static($info[0], $info[1], $info[2], $info[3], $active);
39+
}
40+
41+
}

inc.bootstrap.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// Always. UTF-8. Everywhere. Always.
66
header('Content-type: text/html; charset=utf-8');
77

8+
const JIRA_COOKIE_NAME = 'JIRA_AUTH_2';
9+
810
// Some app constants
911
define('FORCE_JIRA_USER_SYNC', 600); // 10m
1012
define('FORCE_AUTO_VARS_SYNC', 14400); // 4h
@@ -23,6 +25,7 @@
2325
$db->ensureSchema(require 'inc.schema.php');
2426

2527
// Classes
28+
require 'inc.account.php';
2629
require 'inc.user.php';
2730
require 'inc.issue.php';
2831

@@ -35,24 +38,27 @@
3538
// Request constants
3639
define('IS_AJAX', strtolower(@$_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
3740

41+
define('OAUTH_REDIRECT_URL', 'https://' . $_SERVER['HTTP_HOST'] . '/oauth2-callback.php');
42+
3843
// Init Jira request history
3944
$jira_history = array();
4045

4146
// Current session
4247
$user = null;
43-
if ( isset($_COOKIE['JIRA_URL'], $_COOKIE['JIRA_AUTH']) && ($accounts = get_accounts()) ) {
48+
if ( isset($_COOKIE[JIRA_COOKIE_NAME]) && count($accounts = get_accounts()) ) {
4449
$account = $accounts[0];
4550

46-
define('JIRA_URL', $account->url);
47-
define('JIRA_USER', $account->user);
51+
define('JIRA_URL', $account->apiUrl);
4852
define('JIRA_AUTH', $account->auth);
53+
define('JIRA_USER', $account->username);
54+
define('JIRA_SERVER', $account->server);
4955

5056
define('XSRF_TOKEN', md5(date('Y-m-d H') . ':' . JIRA_URL . ':' . JIRA_AUTH));
5157

52-
$url = parse_url(JIRA_URL);
58+
$url = parse_url(JIRA_SERVER);
5359
define('JIRA_ORIGIN', $url['scheme'] . '://' . $url['host']);
5460

55-
$user = User::load(JIRA_URL, JIRA_USER);
61+
$user = User::load(JIRA_SERVER, JIRA_USER);
5662
if ( $user && $user->jira_timezone ) {
5763
date_default_timezone_set($user->jira_timezone);
5864
}

0 commit comments

Comments
 (0)