question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoke-WebRequest should directly support Basic authentication

See original GitHub issue

We are working with more and more “Linux” based servers where we get instructions to run curl to say get back content for a .npmrc file from a product called Artifactory. This product uses Basic auth to accept credentials.

The instructions they give are:

curl -u<USERNAME>:<PASSWORD> http://artifactory.it.keysight.com:8081/artifactory/api/npm/auth

To get this to work in PowerShell using Invoke-WebRequest, this is the best I could come up with:

$userpassB64 = [byte[]][char[]]"hillr:OpenSesame" | ConvertTo-Base64 -NoLineBreak
$res = Invoke-WebRequest http://artifactory.my.company.com:8081/artifactory/api/npm/auth `
       -Headers @{Authorization = "Basic $userpassB64"}

First, ConvertTo-Base64 is a PSCX command (we really need B64 encode/decode in PS Core). Second, Basic auth should be built-in like it is in CURL.

This what I’d like to see from Invoke-WebRequest:

$res = Invoke-WebRequest http://artifactory.my.company.com:8081/artifactory/api/npm/auth `
       -UseBasicAuth -Credential (Get-Credential)

This -UseBasicAuth switch should also support the -UseDefaultCredentials parameter. However the trick is whether or not to include the Domain\ in the username field. That option, when coupled with -UseBasicAuth “probably” should not include the domain. In our scenario, Artifactory will fail to authenticate if the domain is specified. Either way, the user can always fall back to explicitly providing the username with -Credential (Get-Credential).

FYI, the curl docs on -u:

-u, --user user:password Specify the user name and password to use for server authentication. Overrides -n, --netrc and --netrc-optional. If you simply specify the user name, curl will prompt for a password. The user name and passwords are split up on the first colon, which makes it impossible to use a colon in the user name with this option. The password can, still. When using Kerberos V5 with a Windows based server you should include the Windows domain name in the user name, in order for the server to successfully obtain a Kerberos Ticket. If you don’t then the initial authentication handshake may fail. When using NTLM, the user name can be specified simply as the user name, without the domain, if there is a single domain and forest in your setup for example. To specify the domain name use either Down-Level Logon Name or UPN (User Principal Name) formats. For example, EXAMPLE\user and user@example.com respectively. If you use a Windows SSPI-enabled curl binary and perform Kerberos V5, Negotiate, NTLM or Digest authentication then you can tell curl to select the user name and password from your environment by specifying a single colon with this option: “-u :”. If this option is used several times, the last one will be used.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:24
  • Comments:45 (30 by maintainers)

github_iconTop GitHub Comments

4reactions
devblackopscommented, Oct 5, 2017

My two cents:

I don’t think accepting a PSCredential -Token parameter where we only really need the password property makes sense. [SecureString] makes more sense to me as that is the only part we care about. If the user already has the token in a credential object, they can just pass in the securestring password with:

Invoke-RestMethod -Authentication OAuth -Token $cred.Password

Here are a couple of scenarios that I think Invoke-RestMethod and Invoke-WebRequest should support:

# Credential is a [PSCredential]
Invoke-RestMethod -Authorization Basic -Credential $cred
# Behind the scenes do the base64 encoding stuff
# Token is a [SecureString]
Invoke-RestMethod -Authorization OAuth -Token $token
# Under the covers set header Authorization = "Bearer $token"
3reactions
SteveL-MSFTcommented, Oct 6, 2017

I’m ok without the prompting. Thanks for working on this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Invoke-WebRequest with a username and password ...
The -u flag accepts a username for authentication, and then cURL will request the password. The cURL example is for Basic authentication with ......
Read more >
Web requests with basic authentication in Powershell
The basic authentication scheme is very simple and consists of generating a base64 token from your username and password seperated by a colon...
Read more >
Invoke-WebRequest - PowerShell
It parses the response and returns collections of links, images, and other significant HTML elements. This cmdlet was introduced in PowerShell 3.0.
Read more >
Solved: credentials with powershell
Hello all,. I want to script REST API using powershell. How do I get the credentials (token) and pass it to the commands?...
Read more >
Working with REST APIs and PowerShell's Invoke- ...
The Invoke-RestMethod cmdlet supports all HTTP methods, including authentication, sending different HTTP headers, HTTP bodies, and also ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found