Skip to content

OpenShift.io token provider

Aurélien Pupier edited this page Oct 26, 2017 · 4 revisions

JBossTools OpenShift now provides (as of 4.5.1.Final) a new module that allows external services to integrate with OpenShift.io and get a token for API access.

Here is a general description of the functionality and how to use it.

Functionality

The module simply export an extension point that allows to get a token for API access. The module will stored a single account with the access and refresh tokens and their expiry dates. If the module gets called when there is no account stored, a login against OpenShift.io will be processed and returned tokens will be saved. If an account is stored in the database, then tokens are verified and returned if they are still valid. If not, a refresh is performed if the refresh token is still valid. Otherwise, a login is performed again to get new tokens. As there is currently support for a single account, if you want to switch to another account, you need to reset the database. This is possible through the Eclipse preferences, the node is JBoss Tools -> Openshift.io

Use

Loose coupling

In this mode, you don't need extra import or dependencies in our code but you just need to retrieve the òrg.jboss.tools.openshift.io.core.tokenprovider' extension point and cast it to java.util.function.Function<IResource, String> Javadoc

An example code how to the retrieve the extension point is given here:

private Function<IResource, String> getProvider() {
	IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor("org.jboss.tools.openshift.io.core.tokenProvider");
	for(IConfigurationElement element : elements) {
		try {
			return (Function<IResource, String>) element.createExecutableExtension("class");
		} catch (CoreException e) {
			Fabric8AnalysisLSActivator.getDefault().getLog().log(new Status(IStatus.ERROR, Fabric8AnalysisLSActivator.getDefault().getBundle().getSymbolicName(), e.getLocalizedMessage(), e));
		}
	}
	return null;
}

then on the returned object, call the apply function to get a token:

String token = provider.apply(null);

Please note that the parameter of the Function object is here for context passing but is currently not used so passing null is fine.

Strong coupling

The main difference is that the type of the extension point is not a generic Function object but rather a org.jboss.tools.openshift.io.core.TokenProvider instance. The main difference is that you can now call the getToken method instead of the Function apply method:

String token = provider.getToken(null);

In order to use the org.jboss.tools.openshift.io.core.TokenProvider, your bundle must import the org.jboss.tools.openshift.io.core package or have a dependency on theorg.jboss.tools.openshift.io.core bundle.