コード例 #1
0
  /**
   * Opens a dialog for a single non-user, non-password type item.
   *
   * @param shell the shell to use
   * @param uri the uri of the get request
   * @param item the item to handle
   * @return <code>true</code> if the request was successful and values were supplied; <code>false
   *     </code> if the user canceled the request and did not supply all requested values.
   */
  private boolean getSingleSpecial(Shell shell, URIish uri, CredentialItem item) {
    if (item instanceof CredentialItem.InformationalMessage) {
      MessageDialog.openInformation(
          shell, UIText.EGitCredentialsProvider_information, item.getPromptText());
      return true;
    } else if (item instanceof CredentialItem.YesNoType) {
      CredentialItem.YesNoType v = (CredentialItem.YesNoType) item;
      String[] labels =
          new String[] {
            IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL
          };
      int[] resultIDs =
          new int[] {IDialogConstants.YES_ID, IDialogConstants.NO_ID, IDialogConstants.CANCEL_ID};

      MessageDialog dialog =
          new MessageDialog(
              shell,
              UIText.EGitCredentialsProvider_question,
              null,
              item.getPromptText(),
              MessageDialog.QUESTION_WITH_CANCEL,
              labels,
              0);
      dialog.setBlockOnOpen(true);
      int r = dialog.open();
      if (r < 0) {
        return false;
      }

      switch (resultIDs[r]) {
        case IDialogConstants.YES_ID:
          {
            v.setValue(true);
            return true;
          }
        case IDialogConstants.NO_ID:
          {
            v.setValue(false);
            return true;
          }
        default:
          // abort
          return false;
      }
    } else {
      // generically handles all other types of items
      return getMultiSpecial(shell, uri, item);
    }
  }
コード例 #2
0
 @Override
 public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
   for (CredentialItem item : items) {
     if (item instanceof CredentialItem.Username) {
       if ((this.privateKey == null || this.privateKey.length == 0)
           && (this.publicKey == null || this.publicKey.length == 0)
           && (this.passphrase == null || this.passphrase.length == 0)) {
         CredentialItem.Username u = new CredentialItem.Username();
         CredentialItem.Password p = new CredentialItem.Password();
         super.get(uri, u, p);
         if ((u.getValue() == null || u.getValue().length() == 0)
             && (p.getValue() == null || p.getValue().length == 0)) {
           if (uri != null) {
             if (this.remoteUser != null) {
               /* see if a GitHub token is available (obviously only applicable for repos hosted at a GitHub) */
               String uriString = uri.toString();
               String token = null;
               for (int i = 0; token == null && i < GithubTokenProviders.size(); i++) {
                 token = GithubTokenProviders.get(i).getToken(uriString, remoteUser);
               }
               if (token != null) {
                 ((CredentialItem.Username) item).setValue(token);
                 continue;
               }
             }
           }
         }
       }
     }
     if (super.supports(item)) {
       super.get(uri, item);
     } else if (item instanceof CredentialItem.StringType) {
       if (item.getPromptText().toLowerCase(Locale.ENGLISH).contains("passphrase")
           && passphrase != null
           && passphrase.length > 0) {
         ((CredentialItem.StringType) item).setValue(new String(passphrase));
       } else {
         ((CredentialItem.StringType) item).setValue("");
       }
     } else if (item instanceof CredentialItem.CharArrayType) {
       ((CredentialItem.CharArrayType) item).setValue(new char[0]);
     } else {
       throw new UnsupportedCredentialItem(uri, item.getPromptText());
     }
   }
   return true; // we assume that user provided all credentials that are needed
 }