This repository was archived by the owner on Jan 14, 2025. It is now read-only.
forked from lesstif/php-jira-rest-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthService.php
More file actions
183 lines (156 loc) · 4.73 KB
/
Copy pathAuthService.php
File metadata and controls
183 lines (156 loc) · 4.73 KB
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
<?php
namespace JiraRestApi\Auth;
use JiraRestApi\Configuration\ConfigurationInterface;
use Psr\Log\LoggerInterface;
class AuthService extends \JiraRestApi\JiraClient
{
private $auth_api_uri = '/rest/auth/1';
private $uri = 'session';
/**
* @var string|null
*/
private $_sessionCookieName;
/**
* @var string|null
*/
private $_sessionCookieValue;
/**
* @var bool
*/
private $_authInProgress;
public function isAuthorized()
{
return !empty($this->_sessionCookieName) && !empty($this->_sessionCookieValue);
}
/**
* used to prevent infinite recursion when cookie authorization requested and performed.
*
* @return bool
*/
public function isAuthInProgress()
{
return $this->_authInProgress;
}
/**
* For internal usage. Performs login and saves session information for far cookie session authorization.
*
* @param null|string $username
* @param null|string $password
*
* @throws \JiraRestApi\JiraException
* @throws \JsonMapper_Exception
*/
public function authorizeWithCookie($username = null, $password = null)
{
$this->_authInProgress = true;
$session = $this->login($username, $password);
$this->_sessionCookieName = $session->session->name;
$this->_sessionCookieValue = $session->session->value;
$this->_authInProgress = false;
}
public function getSessionCookieName()
{
return $this->_sessionCookieName;
}
public function getSessionCookieValue()
{
return $this->_sessionCookieValue;
}
/**
* AuthService constructor.
*
* @param ConfigurationInterface|null $configuration
* @param \Psr\Log\LoggerInterface|null $logger
* @param string $path
*
* @throws \Exception
* @throws \JiraRestApi\JiraException
*/
public function __construct(?ConfigurationInterface $configuration = null, ?LoggerInterface $logger = null, $path = './')
{
parent::__construct($configuration, $logger, $path);
$this->setAPIUri($this->auth_api_uri);
}
/**
* Returns information about the currently authenticated user's session.
* If the caller is not authenticated they will get a 401 Unauthorized status code.
*
* @see https://docs.atlassian.com/software/jira/docs/api/REST/latest/#auth/1/session-currentUser Jira Reference
*
* @throws \JiraRestApi\JiraException
* @throws \JsonMapper_Exception
*
* @return CurrentUser
*/
public function getCurrentUser()
{
$ret = $this->exec($this->uri);
$user = $this->json_mapper->map(
json_decode($ret),
new CurrentUser()
);
return $user;
}
/**
* Logs the current user out of JIRA, destroying the existing session, if any.
*
* @see https://docs.atlassian.com/software/jira/docs/api/REST/latest/#auth/1/session-logout Jira Reference
*
* @throws \JiraRestApi\JiraException
* @throws \Exception
*
* @return bool
*/
public function logout()
{
$this->exec($this->uri, '', 'DELETE');
$this->_sessionCookieName = null;
$this->_sessionCookieValue = null;
return true;
}
/**
* Logs the current user out of JIRA, destroying the existing session, if any.
*
* @see https://docs.atlassian.com/software/jira/docs/api/REST/latest/#auth/1/session-logout Jira Reference
*
* @param string|null $username If null - takes username from configuration.
* @param string|null $password If null - takes password from configuration.
*
* @throws \JiraRestApi\JiraException
* @throws \JsonMapper_Exception
*
* @return AuthSession
*/
public function login($username = null, $password = null)
{
if (!$username) {
$username = $this->getConfiguration()->getJiraUser();
}
if (!$password) {
$password = $this->getConfiguration()->getJiraPassword();
}
$ret = $this->exec($this->uri, json_encode([
'username' => $username,
'password' => $password,
]), 'POST');
$session = $this->json_mapper->map(
json_decode($ret),
new AuthSession()
);
return $session;
}
/**
* This method invalidates the any current WebSudo session.
*
* @see https://docs.atlassian.com/software/jira/docs/api/REST/latest/#auth/1/websudo-release Jira Reference
*
* @throws \JiraRestApi\JiraException
*
* @return bool
*/
public function release()
{
$this->exec('websudo', '', 'DELETE');
return true;
}
}