Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F56505142
PhabricatorAuthenticationToken.java
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Mar 28, 17:45
Size
3 KB
Mime Type
text/x-java
Expires
Sat, Mar 30, 17:45 (2 d)
Engine
blob
Format
Raw Data
Handle
16722037
Attached To
rPHOAUTH phab-oauth-jenkins
PhabricatorAuthenticationToken.java
View Options
/**
The MIT License
Copyright (c) 2011 Michael O'Cleirigh
Copyright (c) 2016 Jean-Baptiste Aubort
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package
org.jenkinsci.plugins
;
import
hudson.security.SecurityRealm
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.logging.Logger
;
import
java.util.logging.Level
;
import
jenkins.model.Jenkins
;
import
org.acegisecurity.GrantedAuthority
;
import
org.acegisecurity.providers.AbstractAuthenticationToken
;
import
org.acegisecurity.BadCredentialsException
;
import
org.json.JSONObject
;
import
org.json.JSONException
;
public
class
PhabricatorAuthenticationToken
extends
AbstractAuthenticationToken
{
private
static
final
long
serialVersionUID
=
1L
;
private
final
String
accessToken
;
private
PhabricatorUser
user
;
private
final
String
userName
;
private
PhabricatorSecurityRealm
myRealm
=
null
;
private
final
List
<
GrantedAuthority
>
authorities
=
new
ArrayList
<
GrantedAuthority
>();
public
PhabricatorAuthenticationToken
(
String
accessToken
)
throws
IOException
{
super
(
new
GrantedAuthority
[]
{});
this
.
accessToken
=
accessToken
;
authorities
.
add
(
SecurityRealm
.
AUTHENTICATED_AUTHORITY
);
if
(
Jenkins
.
getInstance
().
getSecurityRealm
()
instanceof
PhabricatorSecurityRealm
)
{
if
(
myRealm
==
null
)
{
myRealm
=
(
PhabricatorSecurityRealm
)
Jenkins
.
getInstance
()
.
getSecurityRealm
();
}
}
user
=
authUsingToken
();
if
(
user
==
null
)
{
throw
new
BadCredentialsException
(
"Unexpected authentication type"
);
}
userName
=
user
.
getUsername
();
setAuthenticated
(
true
);
}
protected
PhabricatorUser
getUser
()
{
if
(
user
==
null
&&
accessToken
!=
null
)
{
try
{
user
=
authUsingToken
();
}
catch
(
IOException
e
)
{
LOGGER
.
log
(
Level
.
WARNING
,
e
.
getMessage
());
}
}
return
user
;
}
protected
PhabricatorUser
authUsingToken
()
throws
IOException
{
String
serverURL
=
myRealm
.
getServerURL
();
String
result
=
myRealm
.
getUrlContent
(
serverURL
+
PhabricatorSecurityRealm
.
PHAB_API
+
"?access_token="
+
accessToken
);
PhabricatorUser
user
=
null
;
try
{
JSONObject
jsonObject
=
new
JSONObject
(
result
);
JSONObject
jsonResult
=
jsonObject
.
getJSONObject
(
"result"
);
user
=
new
PhabricatorUser
(
jsonResult
.
getString
(
"userName"
),
jsonResult
.
getString
(
"realName"
),
jsonResult
.
getString
(
"primaryEmail"
),
jsonResult
.
getString
(
"image"
));
}
catch
(
JSONException
e
)
{
LOGGER
.
log
(
Level
.
WARNING
,
e
.
getMessage
());
}
return
user
;
}
public
String
getAccessToken
()
{
return
accessToken
;
}
public
Object
getCredentials
()
{
return
""
;
// do not expose the credential
}
public
String
getPrincipal
()
{
return
this
.
userName
;
}
private
static
final
Logger
LOGGER
=
Logger
.
getLogger
(
PhabricatorAuthenticationToken
.
class
.
getName
());
}
Event Timeline
Log In to Comment