Search in sources :

Example 1 with MdwSecurityException

use of com.centurylink.mdw.auth.MdwSecurityException in project mdw-designer by CenturyLinkCloud.

the class LoginDialog method createButtonsSection.

private void createButtonsSection(Composite parent) {
    // spacer
    new Label(parent, SWT.NONE);
    // ok
    okButton = new Button(parent, SWT.PUSH);
    okButton.setText("OK");
    GridData data = new GridData(SWT.NONE, SWT.NONE, false, false);
    data.widthHint = BUTTON_WIDTH;
    data.verticalIndent = 10;
    okButton.setLayoutData(data);
    okButton.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent e) {
            BusyIndicator.showWhile(getShell().getDisplay(), new Runnable() {

                public void run() {
                    try {
                        String user = userNameText.getText().trim();
                        String password = passwordText.getText().trim();
                        workflowProject.authenticate(user, password, saveCredentialsCheckbox.getSelection());
                        setReturnCode(OK);
                        close();
                    } catch (MdwSecurityException ex) {
                        messageLabel.setText(ex.getMessage());
                    }
                }
            });
        }
    });
    // cancel
    cancelButton = new Button(parent, SWT.PUSH);
    cancelButton.setText("Cancel");
    data = new GridData(SWT.NONE, SWT.NONE, false, false);
    data.widthHint = BUTTON_WIDTH;
    data.verticalIndent = 10;
    cancelButton.setLayoutData(data);
    cancelButton.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent e) {
            setReturnCode(CANCEL);
            close();
        }
    });
}
Also used : Button(org.eclipse.swt.widgets.Button) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Label(org.eclipse.swt.widgets.Label) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException)

Example 2 with MdwSecurityException

use of com.centurylink.mdw.auth.MdwSecurityException in project mdw-designer by CenturyLinkCloud.

the class WorkflowProjectManager method authenticate.

/**
 * Authenticates using the designated authenticator impl.
 */
public void authenticate(Authenticator authenticator, String user, String password, boolean saveInSecureStore) throws MdwSecurityException {
    String key = authenticator.getClass().getName() + "_" + authenticator.getKey();
    authenticatedUsers.remove(key);
    try {
        authenticator.authenticate(user, password);
        if (saveInSecureStore) {
            try {
                ISecurePreferences securePrefs = SecurePreferencesFactory.getDefault();
                securePrefs.put(PreferenceConstants.PREFS_MDW_USER + "_" + key, user, false);
                securePrefs.put(PreferenceConstants.PREFS_MDW_PASSWORD + "_" + key, password, true);
                securePrefs.flush();
            } catch (Exception ex) {
                // don't prevent user from being authenticated because of
                // this
                PluginMessages.log(ex);
            }
        }
        if (authenticator instanceof MdwAuthenticator)
            authenticatedUsers.put(key, new User(user, password, ((MdwAuthenticator) authenticator).getJwtToken()));
        else
            authenticatedUsers.put(key, new User(user, password, null));
    } catch (MdwSecurityException ex) {
        PluginMessages.log(ex);
        throw ex;
    } catch (Exception ex) {
        PluginMessages.log(ex);
        throw new MdwSecurityException(ex.getMessage(), ex);
    }
}
Also used : User(com.centurylink.mdw.plugin.User) MdwAuthenticator(com.centurylink.mdw.designer.auth.MdwAuthenticator) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) ResourceException(org.eclipse.core.internal.resources.ResourceException) CoreException(org.eclipse.core.runtime.CoreException) JavaModelException(org.eclipse.jdt.core.JavaModelException) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException) DiscoveryException(com.centurylink.mdw.plugin.designer.DiscoveryException) XmlException(org.apache.xmlbeans.XmlException) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException)

Example 3 with MdwSecurityException

use of com.centurylink.mdw.auth.MdwSecurityException in project mdw-designer by CenturyLinkCloud.

the class WorkflowProjectManager method getAuthenticatedUser.

/**
 * Triggers automatic authentication if credentials are in Eclipse secure
 * store.
 */
public User getAuthenticatedUser(Authenticator authenticator) {
    String key = authenticator.getClass().getName() + "_" + authenticator.getKey();
    User authUser = authenticatedUsers.get(key);
    if (authUser == null) {
        try {
            ISecurePreferences securePrefs = SecurePreferencesFactory.getDefault();
            String user = securePrefs.get(PreferenceConstants.PREFS_MDW_USER + "_" + key, "");
            if (user.length() > 0) {
                String password = securePrefs.get(PreferenceConstants.PREFS_MDW_PASSWORD + "_" + key, "");
                if (password.length() > 0) {
                    try {
                        authenticate(authenticator, user, password, false);
                        if (authenticator instanceof MdwAuthenticator)
                            authUser = new User(user, password, ((MdwAuthenticator) authenticator).getJwtToken());
                        else
                            authUser = new User(user, password, null);
                    } catch (MdwSecurityException ex) {
                        // prevent repeated attempts to auto-authenticate
                        securePrefs.put(PreferenceConstants.PREFS_MDW_USER + "_" + key, "", false);
                        securePrefs.flush();
                    }
                }
            }
        } catch (Exception ex) {
            // just log exception and force user to log in -- if pw expired
            // they'll enter the new one
            PluginMessages.log(ex);
        }
    }
    return authUser;
}
Also used : User(com.centurylink.mdw.plugin.User) MdwAuthenticator(com.centurylink.mdw.designer.auth.MdwAuthenticator) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException) ResourceException(org.eclipse.core.internal.resources.ResourceException) CoreException(org.eclipse.core.runtime.CoreException) JavaModelException(org.eclipse.jdt.core.JavaModelException) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException) DiscoveryException(com.centurylink.mdw.plugin.designer.DiscoveryException) XmlException(org.apache.xmlbeans.XmlException)

Example 4 with MdwSecurityException

use of com.centurylink.mdw.auth.MdwSecurityException in project mdw-designer by CenturyLinkCloud.

the class MdwAuthenticator method doAuthentication.

/**
 * <p>
 * Takes a cuid and pass combination and authenticates against JWT.
 * </p>
 *
 * @param cuid
 * @param pass
 * @return the JWT access token
 */
public String doAuthentication(String cuid, String pass) throws MdwSecurityException {
    String accessToken = null;
    try {
        if (StringHelper.isEmpty(tokenLocation)) {
            throw new MdwSecurityException("Token location is empty, should point to an JWT token location endpoint." + " Unable to authenticate user " + cuid + " with JWT");
        }
        JSONObject json = new JSONObject();
        json.put("user", cuid);
        json.put("password", pass);
        json.put("appId", appId);
        try {
            HttpHelper helper = new HttpHelper(new URL(tokenLocation));
            Map<String, String> hdrs = new HashMap<>();
            hdrs.put("Content-Type", "application/json; charset=utf-8");
            helper.setHeaders(hdrs);
            String response = helper.post(json.toString());
            JSONObject responseJson = new JSONObject(response);
            accessToken = responseJson.getString("mdwauth");
            if (accessToken == null || accessToken.isEmpty())
                throw new IOException("User authentication failed with response:" + responseJson);
        } catch (IOException ex) {
            throw new ServiceException(ex.getMessage(), ex);
        }
    } catch (Exception ex) {
        String msg = "Unable to authenticate user " + cuid + " with JWT";
        throw new AuthenticationException(msg, ex);
    }
    return accessToken;
}
Also used : JSONObject(org.json.JSONObject) ServiceException(com.centurylink.mdw.common.service.ServiceException) HashMap(java.util.HashMap) AuthenticationException(com.centurylink.mdw.auth.AuthenticationException) IOException(java.io.IOException) HttpHelper(com.centurylink.mdw.common.utilities.HttpHelper) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException) URL(java.net.URL) ServiceException(com.centurylink.mdw.common.service.ServiceException) AuthenticationException(com.centurylink.mdw.auth.AuthenticationException) IOException(java.io.IOException) MdwSecurityException(com.centurylink.mdw.auth.MdwSecurityException)

Aggregations

MdwSecurityException (com.centurylink.mdw.auth.MdwSecurityException)4 MdwAuthenticator (com.centurylink.mdw.designer.auth.MdwAuthenticator)2 User (com.centurylink.mdw.plugin.User)2 DiscoveryException (com.centurylink.mdw.plugin.designer.DiscoveryException)2 XmlException (org.apache.xmlbeans.XmlException)2 ResourceException (org.eclipse.core.internal.resources.ResourceException)2 CoreException (org.eclipse.core.runtime.CoreException)2 ISecurePreferences (org.eclipse.equinox.security.storage.ISecurePreferences)2 JavaModelException (org.eclipse.jdt.core.JavaModelException)2 AuthenticationException (com.centurylink.mdw.auth.AuthenticationException)1 ServiceException (com.centurylink.mdw.common.service.ServiceException)1 HttpHelper (com.centurylink.mdw.common.utilities.HttpHelper)1 IOException (java.io.IOException)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)1 SelectionEvent (org.eclipse.swt.events.SelectionEvent)1 GridData (org.eclipse.swt.layout.GridData)1 Button (org.eclipse.swt.widgets.Button)1 Label (org.eclipse.swt.widgets.Label)1