1

I am developing a custom git credential helper using java and trying to implement azure device code flow in the background. As part of implementation i have to print URL and device code on console which user will use for authentication then press ENTER on the console , but git terminal hangs after printing the URL and device code. Below is my code.

public class GitHelper {

    public static void main(String[] args) {

        String op = args[0];
        switch (op) {
        case "get":
            System.out.printf("username=%s\n", "oauth2");
            System.out.printf("password=%s\n", getToken());
            break;
        default:
            System.out.println(args[0]);
            break;
        }

    }
}
public void waitForUser() {

        System.out.println("To sign in, use a web browser to open the page " + URL
                + "\n and enter the code " + code
                + " to authenticate then return to this window and press ENTER.");
        Callable<String> userInput = () -> new Scanner(System.in).nextLine();
        String res = getUserInputWithTimeout(TIMEOUT, userInput); // 30s until timeout

        if (res != null) {
            System.out.println("Validating Authentication ...");

        }

    }

I have my code bundled as a jar and I have below in my gitconfig.

[credential]
    helper = "!java -jar D:/config/git-credential-helper.jar"

When I do git operation like git clone I am getting below warning and it just hangs there

warning: invalid credential line: To sign in, use a web browser to open the page www.login.com and enter the code 324523453425 to authenticate then return to this window and press ENTER.
Umesh Kumar
  • 1,387
  • 2
  • 16
  • 34

1 Answers1

0

There are a couple problems here. First, the reason you're seeing this message is because in a credential helper, standard input and output are wired up to Git, not the user's TTY. As a result, you can only print properly formatted lines to standard output, and not messages to the user. If you want to interact with the user from the console, you'll need to use /dev/tty or a similar equivalent on Windows.

Second, generally people expect credential helpers to be mostly noninteractive. Your program is not noninteractive and prompts the user, and Git has its own credential prompt, so you're uselessly duplicating the behavior that Git has built-in. Moreover, Git also has proper behavior when asked not to prompt the user or when the TTY doesn't exist, which your program does not. For example, it doesn't honor GIT_TERMINAL_PROMPT, and therefore will break when used in a script or by certain programs, such as the Go toolchain.

Third, credential helpers may be invoked frequently by Git and Git LFS. Therefore, using a language like Java, which is slow to start, would be a bad idea, since it can make the operation take substantially longer than a program written in a different language. If you need a cross-platform language that is fast to start, I'd recommend Rust, or maybe Go, for this purpose.

bk2204
  • 64,793
  • 6
  • 84
  • 100