Skip to content

Invoke-WebRequest should directly support Basic authentication #4274

Closed

Description

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.

Activity

  1. dragonwolf83 commented on Jul 17, 2017

    @dragonwolf83

    We are having the same situation with Device42, an inventory management software. The REST API is requiring the same methods as above to login.

    In my case, I didn't use the PSCX extensions though and just did the Base64 conversions in .NET manually. Then, I wrapped it in it's own function, so that all my Device42 cmdlets can reuse the logic.

    The -UseBasicAuth parameter is needed for both Invoke-WebRequest and Invoke-RestMethod.

    Keith Hill (@rkeithhill) based on the doc you linked, I think -UseBasicAuth should be a string with a [ValidateSet] attribute that can allow user to set the different methods of Basic Authentication that exist.

  2. SteveL-MSFT commented on Jul 18, 2017

    @SteveL-MSFT
    Member

    In the future, we may want to support other forms of Auth, so perhaps have a -Authentication parameter. Note that Basic won't work with -UseDefaultCredentials as I believe there's no way to retrieve the password to send to the remote target which is required for Basic.

  3. rkeithhill commented on Jul 18, 2017

    @rkeithhill
    CollaboratorAuthor

    Steve Lee (@SteveL-MSFT) That makes sense.

  4. RandomNoun7 commented on Jul 21, 2017

    @RandomNoun7
    Contributor

    Add Jenkins and even Github to the list of popular tools that would be easier to work with if iwr and irm supported something like -Authentication Basic

    Also, since neither of these tools are really Linux based (Jenkins is Java which I run on my Windows servers), it points out that this is becoming a widespread issue with a lot of the OSS and otherwise community based tools that Windows folks are increasingly trying to work with using PowerShell.

  5. DexterPOSH commented on Jul 21, 2017

    @DexterPOSH

    +1
    Having the -Authentication parameter would make sense.

  6. treestryder commented on Jul 21, 2017

    @treestryder

    Basic Auth would be nice, but my current struggle is with "OASIS Web Services Security" authentication for Workday's API.

    https://www.oasis-open.org/standards#wssv1.1.1

  7. APraestegaard commented on Jul 21, 2017

    @APraestegaard

    +1

  8. vMarkusK commented on Jul 21, 2017

    @vMarkusK

    Maybe the Syntax "-BasicAuth (get-credential)" is more intuitive. But a great enhancement!

  9. mkellerman commented on Jul 22, 2017

    @mkellerman

    +1 for -Authentication

  10. added this to the milestone on Jul 22, 2017
  11. bitzbyte commented on Jul 23, 2017

    @bitzbyte

    +1 for -Authentication. I have 3 different services that I have to manually Base64 encode credentials for routinely (multiple times per week for each of them) and more that come up on a less frequent basis.

  12. removed
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Jul 23, 2017
  13. 40 remaining items

  14. removed
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Oct 6, 2017
  15. SteveL-MSFT commented on Oct 6, 2017

    @SteveL-MSFT
    Member

    Mark Kraus (@markekraus) I'm assigning this to myself since GitHub won't let me put your name there so that no one duplicates your work

  16. markekraus commented on Oct 7, 2017

    @markekraus
    Contributor
  17. added a commit that references this issue on Oct 18, 2017
    7c9bddf
  18. added a commit that references this issue on Aug 6, 2026
    f0e36aa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

HacktoberfestPotential candidate to participate in HacktoberfestIssue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions