gnustavo / JIRA-REST

Thin wrapper around Jira's REST API
https://metacpan.org/release/JIRA-REST/
17 stars 17 forks source link

Username/Password or PAT: JIRA::REST Error[401 - Unauthorized] #27

Open rwp0 opened 1 year ago

rwp0 commented 1 year ago

Using username/password I get:

JIRA::REST Error[401 - Unauthorized]:
Basic authentication with passwords is deprecated.  For more information, see: https://developer.atlassian.com/cloud/confluence/deprecation-notice-basic-auth/

Using pat (assuming it's the Jira API Token) I get:

JIRA::REST Error[400 - Bad Request]:
- The value '<EXISTING_PROJECT>' does not exist for the field 'project'.

https://id.atlassian.com/manage-profile/security

API Token

I didn't find a separate Personal Authentication Token (PAT) option looking through Jira settings

trentfisher commented 1 year ago

It does not appear this module supports the API tokens directly, but I made it work like so:

    $jira = JIRA::REST->new({
        url => 'https://URLHERE',
        anonymous => 1});
    $jira->{rest}->addHeader(Authorization => 'Bearer TOKENHERE');
gnustavo commented 1 year ago

Hi! The Personal Access Tokens JIRA::REST supports are the ones available for Jira DataCenter, which are described here.

I don't use Jira Cloud and I can see that it deprecated Basic Authentication. I'll study the documentation and try to come up with a specific form of authentication for it.

Thanks for the tip!

gnustavo commented 1 year ago

I just created an API Token following these instructions and used it as the password in a call to a Jira Cloud instance I have. It worked!

This is the script I used:

use JIRA::REST;
# use LWP::ConsoleLogger::Everywhere ();
use Data::Dump;

my $jira = JIRA::REST->new({
    url => 'https://MYJIRA.atlassian.net/',
    username => 'gustavo@MYDOMAIN',

    # https://id.atlassian.com/manage-profile/security/api-tokens
    password => '************************',
});

my $user = $jira->GET('/myself');

ddx $user;

Does it work for you?

trentfisher commented 1 year ago

No, that doesn't work for me. But we have an on-prem Jira (v8.16) so either it works differently or there is some issue with the username.

FYI, the solution I came up with is based on the curl example at https://community.atlassian.com/t5/Jira-questions/How-to-authenticate-to-Jira-REST-API-with-curl/qaq-p/1312165 Based on that I concluded that the auth header is handled differently than the basic auth, i.e. the token is used verbatim with no username, whereas basic auth combines the username and password and encrypts them together.

Matze848 commented 7 months ago

Hi Gustavo! First of all thanks for this very helpful module which has done a perfect job for years for me. Recently we migrated our Jira Server to version 9.4.12 (from v7.x), with the impact that I've to deal with the PAT topic now. So I updated you module to the version 0.023, created a PAT and could directly use my scripts to modify issues. Unfortunately I have some long running tasks, where after several minutes after the successful login the issue processing is aborted with a 401 error:

JIRA::REST Error[401 - Unauthorized]:
Unauthorized (401)Unauthorized (401)Encountered a "401 - Unauthorized" error while loading this page.

After diggin' through the official documentation from Atlassian regarding the usage of the PAT, I realized that the client should send the PAT with every request to the JIRA server via additional header, but it seems this is not done by the JIRA:REST module. PUT and POST requests support additional headers, but GET and DELETE don't have this option, and that's the task where I get the 401 error. Could you think about another modification of the module to support the sending of the PAT with every request by default? Thanks ;-)