Skip to content

Commit

Permalink
Merge pull request #439 from pwshmatt/bearer-token-fix
Browse files Browse the repository at this point in the history
Made Credential param optional to allow for bearer token authentication
  • Loading branch information
lipkau authored Jun 21, 2023
2 parents 00286c1 + 12073ef commit a7f26c4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
4 changes: 2 additions & 2 deletions JiraPS/Public/New-JiraSession.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ function New-JiraSession {
[CmdletBinding()]
[System.Diagnostics.CodeAnalysis.SuppressMessage('PSUseShouldProcessForStateChangingFunctions', '')]
param(
[Parameter( Mandatory )]
[Parameter( )]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
Expand All @@ -29,8 +29,8 @@ function New-JiraSession {
Method = "GET"
Headers = $Headers
StoreSession = $true
Credential = $Credential
}
if ($Credential) { $parameter.Add('Credential',$Credential) }
Write-Debug "[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"
$result = Invoke-JiraMethod @parameter

Expand Down
15 changes: 15 additions & 0 deletions docs/en-US/about_JiraPS_Authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ the email address and the API token must be used.
> with **Cloud Servers** to **always** use API Tokens.
> More information in the [Deprecation notice](https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/).
Some implementations of Jira Server (on-premise) might not be able to use HTTP Basic authentication method noted above when using API tokens. For this, you may need to create a new Jira session using `New-JiraSession` and pass a custom Authorization header.

_More information on the API tokens and how to create one can be found at:_
_<https://confluence.atlassian.com/cloud/api-tokens-938839638.html>_

Expand Down Expand Up @@ -85,6 +87,19 @@ The session is stored in the module's runtime.
This means that it will not be available in a new Powershell session
or if the module is reloaded.

### Creating a Session Using Custom Authorization Headers

Some implementations of Jira Server (on-premise) might not be able to use the methods listed above of using HTTP Basic authentication by passing an email address and token to authenticate. In this case, you will need to create a session by passing the API Token as a bearer token in a custom Authorization header.

To create a session using the API Token as the bearer token, you can use the New-JiraSession function:

```powershell
$personalAccessToken = "<your_token_here>"
$headers = @{ Authorization = "Bearer $($personalAccessToken)" }
New-JiraSession -Headers $headers
```

## What About OAuth

Jira does support use of OAuth, but JiraPS does not - yet.
Expand Down
15 changes: 14 additions & 1 deletion docs/en-US/commands/New-JiraSession.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ Get-JiraIssue TEST-01
Creates a Jira session for jiraUsername.
The following `Get-JiraIssue` is run using the saved session for jiraUsername.

### EXAMPLE 2

```powershell
$personalAccessToken = "<your_token_here>"
$headers = @{ Authorization = "Bearer $($personalAccessToken)" }
New-JiraSession -Headers $headers
Get-JiraIssue TEST-01
```

Creates a Jira session using a Personal Access Token (PAT) as a bearer token in a custom Authorization header.
The following `Get-JiraIssue` is run using the saved session created using the PAT.

## PARAMETERS

### -Credential
Expand All @@ -52,7 +65,7 @@ Type: PSCredential
Parameter Sets: (All)
Aliases:

Required: True
Required: False
Position: 1
Default value: None
Accept pipeline input: False
Expand Down

0 comments on commit a7f26c4

Please sign in to comment.