{"id":2327,"date":"2018-07-05T22:49:54","date_gmt":"2018-07-06T03:49:54","guid":{"rendered":"http:\/\/osric.com\/chris\/accidental-developer\/?p=2327"},"modified":"2018-07-05T22:49:54","modified_gmt":"2018-07-06T03:49:54","slug":"curl-basic-auth-base64-encoded-credentials","status":"publish","type":"post","link":"https:\/\/osric.com\/chris\/accidental-developer\/2018\/07\/curl-basic-auth-base64-encoded-credentials\/","title":{"rendered":"curl basic auth using base64 encoded credentials"},"content":{"rendered":"<p>I was trying to access password-protected files via HTTPS using curl. The site required basic auth. For a demo, I created this example:<\/p>\n<blockquote><p>https:\/\/osric.com\/chris\/demo\/admin\/<br \/>\nUsername: admin<br \/>\nPassword: 123456<\/p><\/blockquote>\n<p>It&#8217;s trivial to access this interactively via <code>curl<\/code>:<\/p>\n<pre><code>$ curl -u admin https:\/\/osric.com\/chris\/demo\/admin\/\r\nEnter host password for user 'admin':<\/code><\/pre>\n<p>Or programmatically by providing the credentials in the URL:<\/p>\n<pre><code>$ curl https:\/\/admin:123456@osric.com\/chris\/demo\/admin\/<\/code><\/pre>\n<p>Or by providing a base64-encoded <code>username:password<\/code> pair in an Authorization header:<\/p>\n<pre><code>$ curl -H \"Authorization: Basic $(echo -n admin:123456 | base64)\" https:\/\/osric.com\/chris\/demo\/admin\/<\/code><\/pre>\n<p>(Note that echo includes a trailing newline character by default, which we do not want to include in the base64-encoded value. Specify the <code>-n<\/code> flag to <code>echo<\/code> to eliminate the trailing newline.)<\/p>\n<p>But I was manipulating files with a Bash script that was being stored in a Git repository, and I didn&#8217;t want to store the credentials in the repository. So I stored the credentials in a separate file:<\/p>\n<pre><code>$ echo -n 'admin:123456' &gt; ~\/admin-credentials\r\n$ chmod 0600 ~\/admin-credentials<\/code><\/pre>\n<p>Now I can read the credentials from the file:<\/p>\n<pre><code>$ curl -H \"Authorization: Basic $(cat admin-credentials | base64)\" https:\/\/osric.com\/chris\/demo\/admin\/<\/code><\/pre>\n<p>I ran into a problem when I tried to update the credentials file with <code>vi<\/code> (or <code>vim<\/code>). Vi automatically inserts an end-of-line (EOL) character, which is not apparent to the user. The base64-encoded value includes the EOL character, and therefore the above command would supply invalid credentials.<\/p>\n<p>To eliminate this in <code>vi<\/code>, use the following <code>vi<\/code> commands:<\/p>\n<pre><code>:set binary\r\n:set noeol<\/code><\/pre>\n<p>Alternately, just overwrite the file with the updated credentials:<\/p>\n<pre><code>$ echo -n 'admin:123456' &gt; ~\/admin-credentials<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Vim includes an invisible EOL character in text files, which can cause problems if, like me, you are piping the contents of the file to base64 to supply basic auth credentials. This post describes 2 ways to create such a text file so that it does not include the extraneous EOL character.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[232],"tags":[476,215,478,477],"class_list":["post-2327","post","type-post","status-publish","format-standard","hentry","category-tips-tricks","tag-base64","tag-curl","tag-echo","tag-vim"],"_links":{"self":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/comments?post=2327"}],"version-history":[{"count":6,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2327\/revisions"}],"predecessor-version":[{"id":2690,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/posts\/2327\/revisions\/2690"}],"wp:attachment":[{"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/media?parent=2327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/categories?post=2327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/osric.com\/chris\/accidental-developer\/wp-json\/wp\/v2\/tags?post=2327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}