blob: f87a809548fc2923a01e5a1aad4b59a5a7749af3 [file] [log] [blame]
/*
* Copyright (c) 2016 Manumitting Technologies Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Manumitting Technologies Inc - initial API and implementation
*/
package org.eclipse.userstorage.sdk.browser;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.userstorage.internal.util.StringUtil;
public class OAuthClientDetailsDialog extends TrayDialog {
private URI origAuthorizationURI;
private String origClientId;
private String origClientSecret;
private String[] origScopes;
private URI origExpectedCallbackURI;
private Text authorizationURIText;
private Text clientIdText;
private Text clientSecretText;
private Text scopesText;
private Text expectedCallbackURIText;
/**
* Create the dialog.
*
* @param parentShell
*/
public OAuthClientDetailsDialog(Shell parentShell, URI authorizationURI, String clientId, String clientSecret,
String[] scopes, URI expectedCallbackURI) {
super(parentShell);
origAuthorizationURI = authorizationURI;
origClientId = clientId;
origClientSecret = clientSecret;
origScopes = scopes;
origExpectedCallbackURI = expectedCallbackURI;
}
@Override
public void create() {
super.create();
// validation and restore must be done after dialog contents are created
validate();
}
@Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
container.setLayout(new GridLayout(2, false));
Label lblAuthorizationURI = new Label(container, SWT.NONE);
lblAuthorizationURI.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblAuthorizationURI.setText("Authorization URI:");
authorizationURIText = new Text(container, SWT.BORDER);
authorizationURIText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
authorizationURIText.setText(origAuthorizationURI.toString());
Label lblClientId = new Label(container, SWT.NONE);
lblClientId.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblClientId.setText("Client ID:");
clientIdText = new Text(container, SWT.BORDER);
clientIdText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
clientIdText.setText(origClientId);
Label lblClientSecret = new Label(container, SWT.NONE);
lblClientSecret.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblClientSecret.setText("Client Secret:");
clientSecretText = new Text(container, SWT.BORDER);
clientSecretText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
clientSecretText.setText(origClientSecret);
Label lblScopes = new Label(container, SWT.NONE);
lblScopes.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblScopes.setToolTipText("Space or comma-separated");
lblScopes.setText("Scopes:");
scopesText = new Text(container, SWT.BORDER);
scopesText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
scopesText.setText(StringUtil.join(", ", origScopes));
Label lblCallback = new Label(container, SWT.NONE);
lblCallback.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
lblCallback.setText("Callback URI:");
expectedCallbackURIText = new Text(container, SWT.BORDER);
expectedCallbackURIText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
expectedCallbackURIText.setText(origExpectedCallbackURI.toString());
ModifyListener modifyListener = new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
validate();
}
};
authorizationURIText.addModifyListener(modifyListener);
clientIdText.addModifyListener(modifyListener);
clientSecretText.addModifyListener(modifyListener);
scopesText.addModifyListener(modifyListener);
expectedCallbackURIText.addModifyListener(modifyListener);
return container;
}
private void validate() {
getButton(IDialogConstants.OK_ID).setEnabled(isValid());
}
private boolean isValid() {
try {
// URL is less forgiving than URI
new URL(authorizationURIText.getText());
origAuthorizationURI = new URI(authorizationURIText.getText());
new URL(expectedCallbackURIText.getText());
origExpectedCallbackURI = new URI(expectedCallbackURIText.getText());
} catch (MalformedURLException e) {
return false;
} catch (URISyntaxException e) {
return false;
}
origClientId = clientIdText.getText().trim();
if (!origClientId.matches("\\w+")) {
return false;
}
origClientSecret = clientSecretText.getText().trim();
if (!origClientSecret.matches("\\w+")) {
return false;
}
String scopes = scopesText.getText().trim();
if (!scopes.matches("(\\w+,?\\s*)+")) {
return false;
}
origScopes = scopesText.getText().trim().split("[,\\s]\\s*");
return true;
}
/**
* Create contents of the button bar.
*
* @param parent
*/
@Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
}
/**
* Return the initial size of the dialog.
*/
@Override
protected Point getInitialSize() {
return new Point(450, 300);
}
public URI getAuthorizationURI() {
return origAuthorizationURI;
}
public String getClientId() {
return origClientId;
}
public String getClientSecret() {
return origClientSecret;
}
public String[] getScopes() {
return origScopes;
}
public URI getExpectedCallbackURIText() {
return origExpectedCallbackURI;
}
}