org.openstreetmap.josm.gui.oauth
Class OsmOAuthAuthorizationClient

java.lang.Object
  extended by org.openstreetmap.josm.gui.oauth.OsmOAuthAuthorizationClient

public class OsmOAuthAuthorizationClient
extends java.lang.Object

An OAuth 1.0 authorization client.

Since:
2746

Nested Class Summary
private static class OsmOAuthAuthorizationClient.SessionId
           
 
Field Summary
private  boolean canceled
           
private  java.net.HttpURLConnection connection
           
private  OAuthConsumer consumer
           
private  OAuthParameters oauthProviderParameters
           
private  OAuthProvider provider
           
 
Constructor Summary
OsmOAuthAuthorizationClient()
          Creates a new authorisation client with default OAuth parameters
OsmOAuthAuthorizationClient(OAuthParameters parameters)
          Creates a new authorisation client with the parameters parameters.
OsmOAuthAuthorizationClient(OAuthParameters parameters, OAuthToken requestToken)
          Creates a new authorisation client with the parameters parameters and an already known Request Token.
 
Method Summary
protected  void authenticateOsmSession(OsmOAuthAuthorizationClient.SessionId sessionId, java.lang.String userName, java.lang.String password)
           
 void authorise(OAuthToken requestToken, java.lang.String osmUserName, java.lang.String osmPassword, OsmPrivileges privileges, ProgressMonitor monitor)
          Automatically authorises a request token for a set of privileges.
 java.lang.String buildOsmLoginUrl()
          Derives the OSM login URL from the OAuth Authorization Website URL
protected  java.lang.String buildOsmLogoutUrl()
          Derives the OSM logout URL from the OAuth Authorization Website URL
protected  java.lang.String buildPostRequest(java.util.Map<java.lang.String,java.lang.String> parameters)
           
 void cancel()
          Cancels the current OAuth operation.
protected  OsmOAuthAuthorizationClient.SessionId extractOsmSession(java.net.HttpURLConnection connection)
           
protected  java.lang.String extractToken(java.net.HttpURLConnection connection)
           
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.
 java.lang.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)
           
protected  void setHttpRequestParameters(java.net.HttpURLConnection connection)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

oauthProviderParameters

private final OAuthParameters oauthProviderParameters

consumer

private final OAuthConsumer consumer

provider

private final OAuthProvider provider

canceled

private boolean canceled

connection

private java.net.HttpURLConnection connection
Constructor Detail

OsmOAuthAuthorizationClient

public OsmOAuthAuthorizationClient()
Creates a new authorisation client with default OAuth parameters


OsmOAuthAuthorizationClient

public OsmOAuthAuthorizationClient(OAuthParameters parameters)
                            throws java.lang.IllegalArgumentException
Creates a new authorisation client with the parameters parameters.

Parameters:
parameters - the OAuth parameters. Must not be null.
Throws:
java.lang.IllegalArgumentException - if parameters is null

OsmOAuthAuthorizationClient

public OsmOAuthAuthorizationClient(OAuthParameters parameters,
                                   OAuthToken requestToken)
                            throws java.lang.IllegalArgumentException
Creates a new authorisation client with the parameters parameters and an already known Request Token.

Parameters:
parameters - the OAuth parameters. Must not be null.
requestToken - the request token. Must not be null.
Throws:
java.lang.IllegalArgumentException - if parameters is null
java.lang.IllegalArgumentException - 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 to NullProgressMonitor.INSTANCE if null
Returns:
the OAuth Request Token
Throws:
OsmOAuthAuthorizationException - if something goes wrong when retrieving the request token
OsmTransferCanceledException - 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 using getRequestToken(ProgressMonitor) first.

Parameters:
monitor - a progress monitor. Defaults to NullProgressMonitor.INSTANCE if null
Returns:
the OAuth Access Token
Throws:
OsmOAuthAuthorizationException - if something goes wrong when retrieving the request token
OsmTransferCanceledException - if the user canceled the request
See Also:
getRequestToken(ProgressMonitor)

getAuthoriseUrl

public java.lang.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 java.lang.String extractToken(java.net.HttpURLConnection connection)

extractOsmSession

protected OsmOAuthAuthorizationClient.SessionId extractOsmSession(java.net.HttpURLConnection connection)

buildPostRequest

protected java.lang.String buildPostRequest(java.util.Map<java.lang.String,java.lang.String> parameters)
                                     throws OsmOAuthAuthorizationException
Throws:
OsmOAuthAuthorizationException

buildOsmLoginUrl

public java.lang.String buildOsmLoginUrl()
                                  throws OsmOAuthAuthorizationException
Derives the OSM login URL from the OAuth Authorization Website URL

Returns:
the OSM login URL
Throws:
OsmOAuthAuthorizationException - if something went wrong, in particular if the URLs are malformed

buildOsmLogoutUrl

protected java.lang.String buildOsmLogoutUrl()
                                      throws OsmOAuthAuthorizationException
Derives the OSM logout URL from the OAuth Authorization Website URL

Returns:
the OSM logout URL
Throws:
OsmOAuthAuthorizationException - if something went wrong, in particular if the URLs are malformed

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.

Throws:
OsmOAuthAuthorizationException - if something went wrong

authenticateOsmSession

protected void authenticateOsmSession(OsmOAuthAuthorizationClient.SessionId sessionId,
                                      java.lang.String userName,
                                      java.lang.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

setHttpRequestParameters

protected void setHttpRequestParameters(java.net.HttpURLConnection connection)

authorise

public void authorise(OAuthToken requestToken,
                      java.lang.String osmUserName,
                      java.lang.String osmPassword,
                      OsmPrivileges privileges,
                      ProgressMonitor monitor)
               throws java.lang.IllegalArgumentException,
                      OsmOAuthAuthorizationException,
                      OsmTransferCanceledException
Automatically authorises a request token for a set of privileges.

Parameters:
requestToken - the request token. Must not be null.
osmUserName - the OSM user name. Must not be null.
osmPassword - the OSM password. Must not be null.
privileges - the set of privileges. Must not be null.
monitor - a progress monitor. Defaults to NullProgressMonitor.INSTANCE if null
Throws:
java.lang.IllegalArgumentException - if requestToken is null
java.lang.IllegalArgumentException - if osmUserName is null
java.lang.IllegalArgumentException - if osmPassword is null
java.lang.IllegalArgumentException - if privileges is null
OsmOAuthAuthorizationException - if the authorisation fails
OsmTransferCanceledException - if the task is canceled by the user


JOSM