binwiederhier / ntfy

Send push notifications to your phone or desktop using PUT/POST
https://ntfy.sh
Apache License 2.0
18.64k stars 732 forks source link

Documentation for Using Tokens via PowerShell Appears to be Incorrect #1060

Closed theparadox1083 closed 8 months ago

theparadox1083 commented 8 months ago

:lady_beetle: Describe the bug In the documentation here: https://docs.ntfy.sh/publish/#access-tokens The PowerShell 7+ example is

# With PowerShell 7 or greater, we can use the Authentication and Token parameters
$Request = @{
  Method = "POST"
  URI = "https://ntfy.example.com/mysecrets"
  Authorization = "Bearer"
  Token = "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2"
  Body = "Look ma, with auth"
}
Invoke-RestMethod @Request

I tried this out and encountered two problems using Invoke-RestMethod as used in the example in the ntfy documentation.

  1. The parameter Authorization should be Authentication - your comment mentions the correct parameter but the example uses the a parameter that doesn't exist.
  2. PowerShell requires the token be a SecureString

Understandably, the second issue might require some explanation for anyone not familiar with PowerShell but the code below technically works. Ideally you'd want to import a SecureString from a file or vault or something but I don't see any other token examples make an effort to keep the token secret so I didn't bother with that in my recommended correction - I just convert on the fly which is absolutely not recommended.

# With PowerShell 7 or greater, we can use the Authentication and Token parameters
# The Token parameter must be in the form of a System.Security.SecureString

$Request = @{
  Method = "POST"
  URI = "https://ntfy.example.com/mysecrets"
  Authentication = "Bearer"
  Token = ConvertTo-SecureString "tk_AgQdq7mVBoFD37zQVN29RhuMzNIz2" -AsPlainText
  Body = "Look ma, with auth"
}
Invoke-RestMethod @Request

:computer: Components impacted Documentation regarding Access Tokens and PowerShell 7+

:bulb: Screenshots and/or logs

Re 1:

|
|  Invoke-RestMethod @Request
|                    ~~~~~~~~
| A parameter cannot be found that matches parameter name 'Authorization'.

Re 2:

|
|  Invoke-RestMethod @Request
|                    ~~~~~~~~
| Cannot bind parameter 'Token'. Cannot convert the value of type "System.String" to type "System.Security.SecureString".

:crystal_ball: Additional context

binwiederhier commented 8 months ago

All the PowerShell stuff is contributed by other people, so I do not know what is best practice or works. WOuld you be able to create a pull request with your suggestions?

theparadox1083 commented 8 months ago

Done - I just created my first PR ever! Hopefully I did it correctly?

binwiederhier commented 8 months ago

Congrats on your first-ever PR. You did everything right. Thank you for your contribution!