com.microsoft.alm.auth.basic.DefaultCredentialPrompt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of auth-core Show documentation
Show all versions of auth-core Show documentation
A library for authenticating against VSTS and TFS.
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root.
package com.microsoft.alm.auth.basic;
import com.microsoft.alm.secret.Credential;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URI;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* A basic credential prompt implementation in Java Swing.
*
* This class provides a very basic CredentialPrompt implementation in Swing.
*
* For any IDE plugin, please implement you own CredentialPrompt
that conforms to IDE standard.
*/
public class DefaultCredentialPrompt implements CredentialPrompt {
private final Lock lock = new ReentrantLock();
private final Condition userResponseReceived = lock.newCondition();
private Credential userEnteredCredential;
/**
* Creates new form DefaultCredentialPrompt
*/
public DefaultCredentialPrompt() {
JPanel panel = createJPanel();
frame = new JFrame();
frame.setSize(380, 180);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosed(WindowEvent e) {
// closing the windows is the same as cancelling the operation
jButtonCancelActionPerformed(null);
}
});
frame.add(panel);
}
/**
* This method is called from within the constructor to create the panel.
* WARNING: this code was generated by netbeans gui editor. Modify at your own risk.
*/
@SuppressWarnings("unchecked")
private JPanel createJPanel() {
final JPanel jPanel = new JPanel();
final JLabel jLabel1 = new javax.swing.JLabel();
final JLabel jLabel2 = new javax.swing.JLabel();
jTextFieldUsername = new javax.swing.JTextField();
jPasswordField = new javax.swing.JPasswordField();
jButtonOK = new javax.swing.JButton();
jButtonCancel = new javax.swing.JButton();
jLabel1.setText("Username:");
jLabel2.setText("Password:");
jButtonOK.setText("OK");
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOKActionPerformed(evt);
}
});
jButtonCancel.setText("Cancel");
jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonCancelActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel);
jPanel.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(23, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jTextFieldUsername)
.addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 5, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButtonOK)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButtonCancel)))
.addGap(18, 18, 18))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextFieldUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jPasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButtonOK)
.addComponent(jButtonCancel))
.addContainerGap(14, Short.MAX_VALUE))
);
return jPanel;
}
private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {
lock.lock();
try {
userEnteredCredential = null;
userResponseReceived.signal();
} finally {
lock.unlock();
}
}
private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {
lock.lock();
try {
final String username = jTextFieldUsername.getText();
final String password = String.valueOf(jPasswordField.getPassword());
userEnteredCredential = new Credential(username, password);
userResponseReceived.signal();
} finally {
lock.unlock();
}
}
private void showPrompt() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
frame.setVisible(true);
}
});
}
public Credential prompt(URI target) {
/* Create and display the form */
showPrompt();
lock.lock();
try {
userResponseReceived.awaitUninterruptibly();
return userEnteredCredential;
} finally {
frame.dispose();
lock.unlock();
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButtonCancel;
private javax.swing.JButton jButtonOK;
private javax.swing.JPasswordField jPasswordField;
private javax.swing.JTextField jTextFieldUsername;
private JFrame frame;
// End of variables declaration
}