-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for entering user credentials to access remote private repos #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ianthomas23
wants to merge
1
commit into
QuantStack:main
Choose a base branch
from
ianthomas23:askpass
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+395
−71
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #include <git2/credential.h> | ||
| #include <iostream> | ||
|
|
||
| #include "credentials.hpp" | ||
| #include "input_output.hpp" | ||
|
|
||
| // git_credential_acquire_cb | ||
| int user_credentials( | ||
| git_credential** out, | ||
| const char* url, | ||
| const char* username_from_url, | ||
| unsigned int allowed_types, | ||
| void* payload) | ||
| { | ||
| // Check for cached credentials here, if desired. | ||
| // It might be necessary to make this function stateful to avoid repeating unnecessary checks. | ||
|
|
||
| *out = nullptr; | ||
|
|
||
| if (allowed_types & GIT_CREDENTIAL_USERPASS_PLAINTEXT) { | ||
| std::string username = username_from_url ? username_from_url : prompt_input("Username: "); | ||
| if (username.empty()) { | ||
| giterr_set_str(GIT_ERROR_HTTP, "No username specified"); | ||
| return GIT_EAUTH; | ||
| } | ||
|
|
||
| std::string password = prompt_input("Password: ", false); | ||
| if (password.empty()) { | ||
| giterr_set_str(GIT_ERROR_HTTP, "No password specified"); | ||
| return GIT_EAUTH; | ||
| } | ||
|
|
||
| // If successful, this will create and return a git_credential* in the out argument. | ||
| return git_credential_userpass_plaintext_new(out, username.c_str(), password.c_str()); | ||
| } | ||
|
|
||
| giterr_set_str(GIT_ERROR_HTTP, "Unexpected credentials request"); | ||
| return GIT_ERROR; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| #pragma once | ||
|
|
||
| #include <git2/credential.h> | ||
|
|
||
| // Libgit2 callback of type git_credential_acquire_cb to obtain user credentials | ||
| // (username and password) to authenticate remote https access. | ||
| int user_credentials( | ||
| git_credential** out, | ||
| const char* url, | ||
| const char* username_from_url, | ||
| unsigned int allowed_types, | ||
| void* payload | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #include "ansi_code.hpp" | ||
| #include "input_output.hpp" | ||
|
|
||
| // OS-specific libraries. | ||
| #include <sys/ioctl.h> | ||
|
|
||
| cursor_hider::cursor_hider(bool hide /* = true */) | ||
| : m_hide(hide) | ||
| { | ||
| std::cout << (m_hide ? ansi_code::hide_cursor : ansi_code::show_cursor); | ||
| } | ||
|
|
||
| cursor_hider::~cursor_hider() | ||
| { | ||
| std::cout << (m_hide ? ansi_code::show_cursor : ansi_code::hide_cursor); | ||
| } | ||
|
|
||
|
|
||
| alternative_buffer::alternative_buffer() | ||
| { | ||
| tcgetattr(fileno(stdin), &m_previous_termios); | ||
| auto new_termios = m_previous_termios; | ||
| // Disable canonical mode (buffered I/O) and echo from stdin to stdout. | ||
| new_termios.c_lflag &= (~ICANON & ~ECHO); | ||
| tcsetattr(fileno(stdin), TCSANOW, &new_termios); | ||
|
|
||
| std::cout << ansi_code::enable_alternative_buffer; | ||
| } | ||
|
|
||
| alternative_buffer::~alternative_buffer() | ||
| { | ||
| std::cout << ansi_code::disable_alternative_buffer; | ||
|
|
||
| // Restore previous termios settings. | ||
| tcsetattr(fileno(stdin), TCSANOW, &m_previous_termios); | ||
| } | ||
|
|
||
| echo_control::echo_control(bool echo) | ||
| : m_echo(echo) | ||
| { | ||
| if (!m_echo) { | ||
| tcgetattr(fileno(stdin), &m_previous_termios); | ||
| auto new_termios = m_previous_termios; | ||
| new_termios.c_lflag &= ~ECHO; | ||
| tcsetattr(fileno(stdin), TCSANOW, &new_termios); | ||
| } | ||
| } | ||
|
|
||
| echo_control::~echo_control() | ||
| { | ||
| if (!m_echo) { | ||
| // Restore previous termios settings. | ||
| tcsetattr(fileno(stdin), TCSANOW, &m_previous_termios); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| std::string prompt_input(const std::string_view prompt, bool echo /* = true */) | ||
| { | ||
| std::cout << prompt; | ||
|
|
||
| echo_control ec(echo); | ||
| std::string input; | ||
|
|
||
| cursor_hider ch(false); // Re-enable cursor if currently hidden. | ||
| std::getline(std::cin, input); | ||
|
|
||
| if (!echo) { | ||
| std::cout << std::endl; | ||
| } | ||
|
|
||
| // Maybe sanitise input, removing escape codes? | ||
| return input; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #pragma once | ||
|
|
||
| #include <iostream> | ||
| #include "common.hpp" | ||
|
|
||
| // OS-specific libraries. | ||
| #include <termios.h> | ||
|
|
||
| // Scope object to hide the cursor. This avoids | ||
| // cursor twinkling when rewritting the same line | ||
| // too frequently. | ||
| // If you are within a cursor_hider context you can | ||
| // reenable the cursor using cursor_hider(false). | ||
| class cursor_hider : noncopyable_nonmovable | ||
| { | ||
| public: | ||
| cursor_hider(bool hide = true); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This allows nested scope objects to disable the cursor when doing a long |
||
|
|
||
| ~cursor_hider(); | ||
|
|
||
| private: | ||
| bool m_hide; | ||
| }; | ||
|
|
||
| // Scope object to use alternative output buffer for | ||
| // fullscreen interactive terminal input/output. | ||
| class alternative_buffer : noncopyable_nonmovable | ||
| { | ||
| public: | ||
| alternative_buffer(); | ||
|
|
||
| ~alternative_buffer(); | ||
|
|
||
| private: | ||
| struct termios m_previous_termios; | ||
| }; | ||
|
|
||
| // Scope object to control echo of stdin to stdout. | ||
| // This should be disabled when entering passwords for example. | ||
| class echo_control : noncopyable_nonmovable | ||
| { | ||
| public: | ||
| echo_control(bool echo); | ||
|
|
||
| ~echo_control(); | ||
|
|
||
| private: | ||
| bool m_echo; | ||
| struct termios m_previous_termios; | ||
| }; | ||
|
|
||
| // Display a prompt on stdout and return newline-terminated input received on | ||
| // stdin from the user. The `echo` argument controls whether stdin is echoed | ||
| // to stdout, use `false` for passwords. | ||
| std::string prompt_input(const std::string_view prompt, bool echo = true); | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have renamed this from
output.cpptoinput_output.cppas it is now a mixture of input and output functions and scope classes (and the header file too).