Package org.openstreetmap.josm.gui.oauth
Class OsmOAuthAuthorizationClient
- java.lang.Object
-
- org.openstreetmap.josm.gui.oauth.OsmOAuthAuthorizationClient
-
public class OsmOAuthAuthorizationClient extends Object
An OAuth 1.0 authorization client.- Since:
- 2746
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
OsmOAuthAuthorizationClient.SessionId
-
Field Summary
Fields Modifier and Type Field Description private boolean
canceled
private HttpClient
connection
private oauth.signpost.OAuthConsumer
consumer
private OAuthParameters
oauthProviderParameters
private oauth.signpost.OAuthProvider
provider
-
Constructor Summary
Constructors Constructor Description OsmOAuthAuthorizationClient(OAuthParameters parameters)
Creates a new authorisation client with the parametersparameters
.OsmOAuthAuthorizationClient(OAuthParameters parameters, OAuthToken requestToken)
Creates a new authorisation client with the parametersparameters
and an already known Request Token.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
authenticateOsmSession(OsmOAuthAuthorizationClient.SessionId sessionId, String userName, String password)
void
authorise(OAuthToken requestToken, String userName, String password, OsmPrivileges privileges, ProgressMonitor monitor)
Automatically authorises a request token for a set of privileges.private static String
booleanParam(boolean param)
protected static String
buildPostRequest(Map<String,String> parameters)
void
cancel()
Cancels the current OAuth operation.protected OsmOAuthAuthorizationClient.SessionId
extractOsmSession()
protected String
extractToken()
protected void
fetchOAuthToken(OsmOAuthAuthorizationClient.SessionId sessionId, OAuthToken requestToken)
Submits a request to the OSM website for a OAuth form.protected OsmOAuthAuthorizationClient.SessionId
fetchOsmWebsiteSessionId()
Submits a request to the OSM website for a login form.OAuthToken
getAccessToken(ProgressMonitor monitor)
Submits a request for an Access Token to the Access Token Endpoint Url of the OAuth Service Provider and replies the request token.String
getAuthoriseUrl(OAuthToken requestToken)
Builds the authorise URL for a given Request Token.OAuthToken
getRequestToken(ProgressMonitor monitor)
Submits a request for a Request Token to the Request Token Endpoint Url of the OAuth Service Provider and replies the request token.protected void
logoutOsmSession(OsmOAuthAuthorizationClient.SessionId sessionId)
protected void
sendAuthorisationRequest(OsmOAuthAuthorizationClient.SessionId sessionId, OAuthToken requestToken, OsmPrivileges privileges)
-
-
-
Field Detail
-
oauthProviderParameters
private final OAuthParameters oauthProviderParameters
-
consumer
private final oauth.signpost.OAuthConsumer consumer
-
provider
private final oauth.signpost.OAuthProvider provider
-
canceled
private boolean canceled
-
connection
private HttpClient connection
-
-
Constructor Detail
-
OsmOAuthAuthorizationClient
public OsmOAuthAuthorizationClient(OAuthParameters parameters)
Creates a new authorisation client with the parametersparameters
.- Parameters:
parameters
- the OAuth parameters. Must not be null.- Throws:
IllegalArgumentException
- if parameters is null
-
OsmOAuthAuthorizationClient
public OsmOAuthAuthorizationClient(OAuthParameters parameters, OAuthToken requestToken)
Creates a new authorisation client with the parametersparameters
and an already known Request Token.- Parameters:
parameters
- the OAuth parameters. Must not be null.requestToken
- the request token. Must not be null.- Throws:
IllegalArgumentException
- if parameters is nullIllegalArgumentException
- if requestToken is null
-
-
Method Detail
-
cancel
public void cancel()
Cancels the current OAuth operation.
-
getRequestToken
public OAuthToken getRequestToken(ProgressMonitor monitor) throws OsmOAuthAuthorizationException, OsmTransferCanceledException
Submits a request for a Request Token to the Request Token Endpoint Url of the OAuth Service Provider and replies the request token.- Parameters:
monitor
- a progress monitor. Defaults toNullProgressMonitor.INSTANCE
if null- Returns:
- the OAuth Request Token
- Throws:
OsmOAuthAuthorizationException
- if something goes wrong when retrieving the request tokenOsmTransferCanceledException
- if the user canceled the request
-
getAccessToken
public OAuthToken getAccessToken(ProgressMonitor monitor) throws OsmOAuthAuthorizationException, OsmTransferCanceledException
Submits a request for an Access Token to the Access Token Endpoint Url of the OAuth Service Provider and replies the request token. You must have requested a Request Token usinggetRequestToken(ProgressMonitor)
first.- Parameters:
monitor
- a progress monitor. Defaults toNullProgressMonitor.INSTANCE
if null- Returns:
- the OAuth Access Token
- Throws:
OsmOAuthAuthorizationException
- if something goes wrong when retrieving the request tokenOsmTransferCanceledException
- if the user canceled the request- See Also:
getRequestToken(ProgressMonitor)
-
getAuthoriseUrl
public String getAuthoriseUrl(OAuthToken requestToken)
Builds the authorise URL for a given Request Token. Users can be redirected to this URL. There they can login to OSM and authorise the request.- Parameters:
requestToken
- the request token- Returns:
- the authorise URL for this request
-
extractToken
protected String extractToken()
-
extractOsmSession
protected OsmOAuthAuthorizationClient.SessionId extractOsmSession() throws IOException, URISyntaxException
- Throws:
IOException
URISyntaxException
-
buildPostRequest
protected static String buildPostRequest(Map<String,String> parameters)
-
fetchOsmWebsiteSessionId
protected OsmOAuthAuthorizationClient.SessionId fetchOsmWebsiteSessionId() throws OsmOAuthAuthorizationException
Submits a request to the OSM website for a login form. The OSM website replies a session ID in a cookie.- Returns:
- the session ID structure
- Throws:
OsmOAuthAuthorizationException
- if something went wrong
-
fetchOAuthToken
protected void fetchOAuthToken(OsmOAuthAuthorizationClient.SessionId sessionId, OAuthToken requestToken) throws OsmOAuthAuthorizationException
Submits a request to the OSM website for a OAuth form. The OSM website replies a session token in a hidden parameter.- Parameters:
sessionId
- session idrequestToken
- request token- Throws:
OsmOAuthAuthorizationException
- if something went wrong
-
authenticateOsmSession
protected void authenticateOsmSession(OsmOAuthAuthorizationClient.SessionId sessionId, String userName, String password) throws OsmLoginFailedException
- Throws:
OsmLoginFailedException
-
logoutOsmSession
protected void logoutOsmSession(OsmOAuthAuthorizationClient.SessionId sessionId) throws OsmOAuthAuthorizationException
- Throws:
OsmOAuthAuthorizationException
-
sendAuthorisationRequest
protected void sendAuthorisationRequest(OsmOAuthAuthorizationClient.SessionId sessionId, OAuthToken requestToken, OsmPrivileges privileges) throws OsmOAuthAuthorizationException
- Throws:
OsmOAuthAuthorizationException
-
booleanParam
private static String booleanParam(boolean param)
-
authorise
public void authorise(OAuthToken requestToken, String userName, String password, OsmPrivileges privileges, ProgressMonitor monitor) throws OsmOAuthAuthorizationException, OsmTransferCanceledException
Automatically authorises a request token for a set of privileges.- Parameters:
requestToken
- the request token. Must not be null.userName
- the OSM user name. Must not be null.password
- the OSM password. Must not be null.privileges
- the set of privileges. Must not be null.monitor
- a progress monitor. Defaults toNullProgressMonitor.INSTANCE
if null- Throws:
IllegalArgumentException
- if requestToken is nullIllegalArgumentException
- if osmUserName is nullIllegalArgumentException
- if osmPassword is nullIllegalArgumentException
- if privileges is nullOsmOAuthAuthorizationException
- if the authorisation failsOsmTransferCanceledException
- if the task is canceled by the user
-
-