Commit eb9237eb authored by Matija Obreza's avatar Matija Obreza
Browse files

Added dependency org.genesys2.client-api

Implemented verifier dialog and obtaining tokens
parent 87987e2f
......@@ -150,6 +150,12 @@
<version>20090211</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.genesys2</groupId>
<artifactId>client-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package org.genesys2.anno.gui;
import java.awt.Color;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.value.IObservableValue;
......@@ -18,7 +22,12 @@ import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.genesys2.anno.model.OAuthSettings;
import org.genesys2.anno.model.Settings;
import org.genesys2.client.oauth.GenesysApiException;
import org.genesys2.client.oauth.GenesysClient;
import org.genesys2.client.oauth.OAuthAuthenticationException;
import org.genesys2.client.oauth.PleaseRetryException;
import org.omg.CORBA.COMM_FAILURE;
import swing2swt.layout.BorderLayout;
......@@ -26,8 +35,10 @@ import swing2swt.layout.BorderLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.wb.swt.SWTResourceManager;
public class SettingsDialog extends Dialog {
private static final Logger _log = Logger.getLogger(SettingsDialog.class);
private DataBindingContext m_bindingContext;
private Settings settings;
......@@ -39,6 +50,8 @@ public class SettingsDialog extends Dialog {
private Text txtApiUrl;
private Text txtAccessToken;
private Text txtRefreshToken;
private Text txtClientKey;
private Text txtClientSecret;
/**
* Create the dialog.
......@@ -85,14 +98,12 @@ public class SettingsDialog extends Dialog {
TabItem tbtmGenesysApi = new TabItem(tabFolder, SWT.NONE);
tbtmGenesysApi.setText("Genesys API");
ScrolledComposite scrolledComposite = new ScrolledComposite(tabFolder, SWT.V_SCROLL);
scrolledComposite.setShowFocusedControl(true);
tbtmGenesysApi.setControl(scrolledComposite);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
Composite composite = new Composite(scrolledComposite, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
......@@ -135,6 +146,22 @@ public class SettingsDialog extends Dialog {
txtApiUrl = new Text(grpGenesysApiConfiguration, SWT.BORDER);
txtApiUrl.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblClientApiKey = new Label(grpGenesysApiConfiguration, SWT.NONE);
lblClientApiKey.setAlignment(SWT.RIGHT);
lblClientApiKey.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblClientApiKey.setText("Client API key");
txtClientKey = new Text(grpGenesysApiConfiguration, SWT.BORDER);
txtClientKey.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Label lblClientSecret = new Label(grpGenesysApiConfiguration, SWT.NONE);
lblClientSecret.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblClientSecret.setText("Client secret");
lblClientSecret.setAlignment(SWT.RIGHT);
txtClientSecret = new Text(grpGenesysApiConfiguration, SWT.BORDER);
txtClientSecret.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
Group grpOauthTokens = new Group(composite, SWT.NONE);
grpOauthTokens.setLayout(new GridLayout(2, false));
grpOauthTokens.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
......@@ -184,18 +211,44 @@ public class SettingsDialog extends Dialog {
composite.pack();
scrolledComposite.setContent(composite);
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
TabItem tbtmTodo = new TabItem(tabFolder, SWT.NONE);
tbtmTodo.setText("TODO");
m_bindingContext = initDataBindings();
}
protected void doAuthenticate() {
// TODO
GenesysClient genesysClient = new GenesysClient();
OAuthSettings oauthSettings = settings.getOauthSettings();
genesysClient.setBaseUrl(oauthSettings.getServerUrl());
genesysClient.setAccessToken(oauthSettings.getAccessToken());
genesysClient.setRefreshToken(oauthSettings.getRefreshToken());
try {
genesysClient.connect(oauthSettings.getClientKey(), oauthSettings.getClientSecret(), "oob");
} catch (IllegalArgumentException e) {
txtClientKey.forceFocus();
return;
}
try {
genesysClient.me();
} catch (OAuthAuthenticationException e) {
_log.warn(e.getMessage());
String authorizationUrl = genesysClient.getAuthorizationUrl(null);
VerifierDialog vd = new VerifierDialog(getParent(), SWT.NONE);
String verifierCode = vd.open(authorizationUrl);
_log.info("Got verifier code: " + verifierCode);
if (StringUtils.isNotBlank(verifierCode)) {
genesysClient.authenticate(verifierCode);
oauthSettings.setAccessToken(genesysClient.getAccessToken().getToken());
oauthSettings.setRefreshToken(genesysClient.getRefreshToken().getToken());
}
} catch (PleaseRetryException e) {
_log.error("Please retry", e);
} catch (GenesysApiException e) {
_log.error("Please retry", e);
}
}
protected DataBindingContext initDataBindings() {
......@@ -225,6 +278,14 @@ public class SettingsDialog extends Dialog {
IObservableValue oauthSettingsrefreshTokenSettingsObserveValue = BeanProperties.value("oauthSettings.refreshToken").observe(settings);
bindingContext.bindValue(observeTextTxtRefreshTokenObserveWidget, oauthSettingsrefreshTokenSettingsObserveValue, null, null);
//
IObservableValue observeTextTxtClientKeyObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtClientKey);
IObservableValue oauthSettingsclientKeySettingsObserveValue = BeanProperties.value("oauthSettings.clientKey").observe(settings);
bindingContext.bindValue(observeTextTxtClientKeyObserveWidget, oauthSettingsclientKeySettingsObserveValue, null, null);
//
IObservableValue observeTextTxtClientSecretObserveWidget = WidgetProperties.text(SWT.Modify).observe(txtClientSecret);
IObservableValue oauthSettingsclientSecretSettingsObserveValue = BeanProperties.value("oauthSettings.clientSecret").observe(settings);
bindingContext.bindValue(observeTextTxtClientSecretObserveWidget, oauthSettingsclientSecretSettingsObserveValue, null, null);
//
return bindingContext;
}
}
package org.genesys2.anno.gui;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
public class VerifierDialog extends Dialog {
protected Shell shlEstablishLinkTo;
private Text txtVerifier;
private String verifierCode;
private String authorizationUrl;
/**
* Create the dialog.
*
* @param parent
* @param style
*/
public VerifierDialog(Shell parent, int style) {
super(parent, SWT.DIALOG_TRIM);
setText("SWT Dialog");
}
/**
* Open the dialog.
*
* @param authorizationUrl
* @return the result
*/
public String open(String authorizationUrl) {
this.authorizationUrl=authorizationUrl;
createContents();
shlEstablishLinkTo.open();
shlEstablishLinkTo.layout();
Display display = getParent().getDisplay();
while (!shlEstablishLinkTo.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return verifierCode;
}
/**
* Create contents of the dialog.
*/
private void createContents() {
shlEstablishLinkTo = new Shell(getParent(), getStyle());
shlEstablishLinkTo.setSize(491, 199);
shlEstablishLinkTo.setText("Establish link to server");
GridLayout gl_shlEstablishLinkTo = new GridLayout(2, false);
gl_shlEstablishLinkTo.marginRight = 10;
gl_shlEstablishLinkTo.marginLeft = 10;
shlEstablishLinkTo.setLayout(gl_shlEstablishLinkTo);
Label lblLoginWithGenesys = new Label(shlEstablishLinkTo, SWT.NONE);
lblLoginWithGenesys.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
lblLoginWithGenesys.setText("Login with Genesys PGR server and authorize the request.");
new Label(shlEstablishLinkTo, SWT.NONE);
Composite composite_1 = new Composite(shlEstablishLinkTo, SWT.NONE);
composite_1.setLayout(new RowLayout(SWT.HORIZONTAL));
Button btnOpenLink = new Button(composite_1, SWT.NONE);
btnOpenLink.setToolTipText("This will open a browser window!");
btnOpenLink.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
org.eclipse.swt.program.Program.launch(authorizationUrl);
}
});
btnOpenLink.setText("Open link in browser");
Button btnCopyUrlTo = new Button(composite_1, SWT.NONE);
btnCopyUrlTo.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Clipboard clipboard=new Clipboard(getParent().getDisplay());
clipboard.setContents(new Object[] { authorizationUrl }, new Transfer[] { TextTransfer.getInstance() });
}
});
btnCopyUrlTo.setText("Copy URL to clipboard");
Label lblAfterAuthorizingThe = new Label(shlEstablishLinkTo, SWT.NONE);
lblAfterAuthorizingThe.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
lblAfterAuthorizingThe.setText("After authorizing the request, copy the verifier code and paste it here:");
Label lblVerifierCode = new Label(shlEstablishLinkTo, SWT.NONE);
lblVerifierCode.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblVerifierCode.setText("Verifier code");
txtVerifier = new Text(shlEstablishLinkTo, SWT.BORDER);
txtVerifier.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
verifierCode = txtVerifier.getText();
}
});
txtVerifier.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
new Label(shlEstablishLinkTo, SWT.NONE);
Composite composite = new Composite(shlEstablishLinkTo, SWT.NONE);
composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
composite.setLayout(new RowLayout(SWT.HORIZONTAL));
Button btnOk = new Button(composite, SWT.NONE);
btnOk.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
shlEstablishLinkTo.dispose();
}
});
btnOk.setText("OK");
}
}
......@@ -3,12 +3,14 @@ package org.genesys2.anno.model;
import org.genesys2.anno.gui.AbstractModelObject;
public class OAuthSettings extends AbstractModelObject {
private String serverUrl = "http://localhost:8080/";
private String serverUrl = "http://localhost:8080";
private String authorizationEndpoint = "http://localhost:8080/oauth/authorize";
private String tokenEndpoint = "http://localhost:8080/oauth/token";
private String apiUrl = "http://localhost:8080/api/v0";
private String accessToken = "";
private String refreshToken = "";
private String clientKey;
private String clientSecret;
public String getServerUrl() {
return serverUrl;
......@@ -72,4 +74,22 @@ public class OAuthSettings extends AbstractModelObject {
setRefreshToken("");
}
public String getClientKey() {
return clientKey;
}
public void setClientKey(String clientKey) {
this.clientKey = clientKey;
firePropertyChange("clientKey", null, this.clientKey);
}
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
firePropertyChange("clientSecret", null, this.clientSecret);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment