コード例 #1
0
  @Override
  public boolean get(final URIish uri, final CredentialItem... items)
      throws UnsupportedCredentialItem {

    if (items.length == 0) {
      return true;
    }

    CredentialItem.Username userItem = null;
    CredentialItem.Password passwordItem = null;
    boolean isSpecial = false;

    for (CredentialItem item : items) {
      if (item instanceof CredentialItem.Username) userItem = (CredentialItem.Username) item;
      else if (item instanceof CredentialItem.Password)
        passwordItem = (CredentialItem.Password) item;
      else isSpecial = true;
    }

    if (!isSpecial && (userItem != null || passwordItem != null)) {
      UserPasswordCredentials credentials = null;
      if ((user != null) && (password != null))
        credentials = new UserPasswordCredentials(user, password);
      else credentials = SecureStoreUtils.getCredentialsQuietly(uri);

      if (credentials == null) {
        credentials = getCredentialsFromUser(uri);
        if (credentials == null) return false;
      }
      if (userItem != null) userItem.setValue(credentials.getUser());
      if (passwordItem != null) passwordItem.setValue(credentials.getPassword().toCharArray());
      return true;
    }

    // special handling for non-user,non-password type items
    final boolean[] result = new boolean[1];

    PlatformUI.getWorkbench()
        .getDisplay()
        .syncExec(
            new Runnable() {
              @Override
              public void run() {
                Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

                if (items.length == 1) {
                  CredentialItem item = items[0];
                  result[0] = getSingleSpecial(shell, uri, item);
                } else {
                  result[0] = getMultiSpecial(shell, uri, items);
                }
              }
            });

    return result[0];
  }
コード例 #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
 }