public class Oauth2

  1. Object
  2. Oauth2
Deprecated. Use OidcClient for new code.

Legacy OAuth2 authentication helper. Deprecated as of Codename One 8.0; new code should use OidcClient instead, which:

  • Drives sign-in via the system browser (SystemBrowser) instead of an in-app WebView. Modern identity providers (Google, Apple, Microsoft, Facebook) refuse to render their sign-in pages inside an embedded WebView and will block this class.
  • Performs PKCE on every authorization-code flow (mandatory now on most providers).
  • Parses the OpenID Connect discovery document so you do not have to hard-code the authorization / token endpoints.
  • Verifies the state and nonce parameters returned by the server.

This class is preserved as-is for source compatibility but no new functionality will be added. See the Authentication and Identity chapter of the developer guide for a migration recipe.

Nested types

class Oauth2.RefreshTokenRequest

Fields

public static final String TOKEN = "access_token"

Constructors

public Oauth2(String oauth2URL, String clientId, String redirectURI)Simple constructor
public Oauth2(String oauth2URL, String clientId, String redirectURI, String scope)Simple constructor
public Oauth2(String oauth2URL, String clientId, String redirectURI, String scope, String tokenRequestURL, String clientSecret)Simple constructor
public Oauth2(String oauth2URL, String clientId, String redirectURI, String scope, String tokenRequestURL, String clientSecret, Hashtable additionalParams)Simple constructor

Methods

public static boolean isBackToParent()Enables going back to the parent form after login is completed
public static void setBackToParent(boolean aBackToParent)Enables going back to the parent form after login is completed
public static Oauth2 fetchSerializedOauth2Request()
public static String getExpires()Returns the expiry for the token received via oauth
public static boolean handleRedirect(ActionListener callback)When using the #setUseRedirectForWeb(boolean) option you should call this method at the beginning of your app’s start() method.
public String authenticate()Deprecated This method preforms the actual authentication, this method is a blocking method that will display the user the html authentication pages.
public boolean isUseBrowserWindow()Checks if this component will use an external web browser window for the login process.
public void setUseBrowserWindow(boolean useBrowserWindow)Set this OAuth2 object to use a BrowserWindow for the login process.
public boolean isUseRedirectForWeb()Checks wither this Oauth component is configured to use a redirect for Oauth login when running on the web.
public void setUseRedirectForWeb(boolean redirect)Sets thisOAuth2 object to use a redirect for login instead of an iframe when running on the Web (via the Javascript port).
public Component createAuthComponent(ActionListener al)This method creates a component which can authenticate.
public void showAuthentication(ActionListener<ActionEvent> al)This method shows an authentication for login form
protected void handleTokenRequestResponse(Map map)Processes token request responses that are formatted as a JSON object.
protected void handleTokenRequestResponse(String t)Processes token request responses that are formatted as HTTP query strings.
protected void handleRedirectURLParams(Map params)Method that can be overridden by subclasses to intercept parameters extracted from the redirect URL when the login flow reaches the redirect URL.
public Oauth2.RefreshTokenRequest refreshToken(String refreshToken)

Inherited methods

Field details

TOKEN

public static final String TOKEN = "access_token"

Constructor details

Oauth2

public Oauth2(String oauth2URL, String clientId, String redirectURI)
Simple constructor

Parameters

oauth2URL String
the authentication url of the service
clientId String
the client id that would like to use the service
redirectURI String
the redirect uri

Oauth2

public Oauth2(String oauth2URL, String clientId, String redirectURI, String scope)
Simple constructor

Parameters

oauth2URL String
the authentication url of the service
clientId String
the client id that would like to use the service
redirectURI String
the redirect uri
scope String
the authentication scope

Oauth2

public Oauth2(String oauth2URL, String clientId, String redirectURI, String scope, String tokenRequestURL, String clientSecret)
Simple constructor

Parameters

oauth2URL String
the authentication url of the service
clientId String
the client id that would like to use the service
redirectURI String
the redirect uri
scope String
the authentication scope
tokenRequestURL String
Not documented.
clientSecret String
the client secret

Oauth2

public Oauth2(String oauth2URL, String clientId, String redirectURI, String scope, String tokenRequestURL, String clientSecret, Hashtable additionalParams)
Simple constructor

Parameters

oauth2URL String
the authentication url of the service
clientId String
the client id that would like to use the service
redirectURI String
the redirect uri
scope String
the authentication scope
tokenRequestURL String
Not documented.
clientSecret String
the client secret
additionalParams Hashtable
hashtable of additional parameters to the authentication request

Method details

isBackToParent

public static boolean isBackToParent()
Enables going back to the parent form after login is completed

Returns

the backToParent

setBackToParent

public static void setBackToParent(boolean aBackToParent)
Enables going back to the parent form after login is completed

Parameters

aBackToParent boolean
the backToParent to set

fetchSerializedOauth2Request

public static Oauth2 fetchSerializedOauth2Request()

getExpires

public static String getExpires()
Returns the expiry for the token received via oauth

Returns

the expires argument for the token

handleRedirect

public static boolean handleRedirect(ActionListener callback)
When using the #setUseRedirectForWeb(boolean) option you should call this method at the beginning of your app’s start() method. If the app was loaded as a result of redirecting from an Oauth login, then this method will handle the login and will call the callback method on complete.

Parameters

callback ActionListener
a listener that will receive at its source either a token for the service or an exception in case of a failure

Returns

True the redirect was handled. False if it was not handled. If this returns true, then you should just return from the start() method, and instead handle control flow in your callback.

authenticate

public String authenticate()
Deprecated. use createAuthComponent or showAuthentication which work asynchronously and adapt better to different platforms
This method preforms the actual authentication, this method is a blocking method that will display the user the html authentication pages.

Returns

the method if passes authentication will return the access token or null if authentication failed.

Throws

IOException
the method will throw an IOException if something went wrong in the communication.

isUseBrowserWindow

public boolean isUseBrowserWindow()
Checks if this component will use an external web browser window for the login process.

Returns

True if this component will use an external web browser window.

setUseBrowserWindow

public void setUseBrowserWindow(boolean useBrowserWindow)

Set this OAuth2 object to use a BrowserWindow for the login process. You can set the global default via the “oauth2.useBrowserWindow” display property with either a “true” or “false” value.

When this property is set, the login prompt will be displayed in a separate Window containing a web browser on the desktop. Platforms that don’t have windows (e.g. iOS/Android) will fall back to a separate Form with a webview).

Parameters

useBrowserWindow boolean
True to use a browser window for the login process.

isUseRedirectForWeb

public boolean isUseRedirectForWeb()
Checks wither this Oauth component is configured to use a redirect for Oauth login when running on the web.

Returns

True if this component will use a redirect for Oauth login.

setUseRedirectForWeb

public void setUseRedirectForWeb(boolean redirect)

Sets thisOAuth2 object to use a redirect for login instead of an iframe when running on the Web (via the Javascript port). Some Oauth providers won’t work inside an iframe.

Using this option will cause the browser to navigate away from the app to go to the login page. The Oauth login will redirect back to the app after login is complete.

Warning: If the user has unsaved changes in the app, navigating away from the app may cause them to lose their changes. You should provide a warning, or confirmation prompt for the user in such cases. The usual onbeforeunload handler is disabled when using this action so the user won’t receive any warnings other than what you explicitly prompt.

Parameters

redirect boolean
Set to true to use a redirect for Oauth login instead of an iframe when running on the web.

createAuthComponent

public Component createAuthComponent(ActionListener al)
This method creates a component which can authenticate. You will receive either the authentication key or an Exception object within the ActionListener callback method.

Parameters

al ActionListener
a listener that will receive at its source either a token for the service or an exception in case of a failure

Returns

a component that should be displayed to the user in order to perform the authentication

showAuthentication

public void showAuthentication(ActionListener<ActionEvent> al)
This method shows an authentication for login form

Parameters

al ActionListener<ActionEvent>
a listener that will receive at its source either a token for the service or an exception in case of a failure

handleTokenRequestResponse

protected void handleTokenRequestResponse(Map map)
Processes token request responses that are formatted as a JSON object. May be overridden by subclasses, but subclass implementations should call super.handleTokenRequestResponse() so that the default implementation can parse out the token, expires, and refreshToken fields.

Parameters

map Map
Parsed JSON object of response.

handleTokenRequestResponse

protected void handleTokenRequestResponse(String t)
Processes token request responses that are formatted as HTTP query strings. May be overridden by subclass, but subclass implementations should call super.handleTokenRequestResponse() so that the default implementation can parse out the token, expires, and refreshToken fields.

Parameters

t String
The query string.

handleRedirectURLParams

protected void handleRedirectURLParams(Map params)
Method that can be overridden by subclasses to intercept parameters extracted from the redirect URL when the login flow reaches the redirect URL. This will give subclasses an opportunity to parse out special information that the OAuth2 service provides in the callback.

Parameters

params Map
Parsed query parameters passed to the redirect URL.

refreshToken

public Oauth2.RefreshTokenRequest refreshToken(String refreshToken)