sumoheavy / jira-ruby

A Ruby gem for the JIRA REST API
MIT License
655 stars 412 forks source link

OAuth 2.0 #423

Open marlinpierce opened 9 months ago

marlinpierce commented 9 months ago

I am creating this issue for a request client which supports OAuth 2.0.

marlinpierce commented 9 months ago

I have work in progress for writing such a Request Client. I have it to a POC level. When I have a viable prototype I'll create a draft PR.

I created this issue to track discussion about such an implementation.

marlinpierce commented 9 months ago

We use OAuth 2.0 for our Jira server. We have yet been passing the Access Token in a bearer authentication header as an additional default header. My RequestClient code uses the oauth2 gem to make calls to Jira.

marlinpierce commented 6 months ago

I became busy so this will not be ready for the next release, 0.2.4. I will work on having it ready, with enough time for review and discussion, by the end of 2024, to be ready for the 0.2.5 release.

InbaKrish commented 2 weeks ago

For the workaround I have followed the below for OAuth2 token based API authentication,

class JiraClient
  def initialize(token)
    headers = { 'Authorization' => 'Bearer ' + token }
    options = {
      site: 'https://api.atlassian.com',
      auth_type: :basic,
      default_headers: headers,
      context_path: '',
      rest_base_path: '/ex/jira/' + ENV['ATLASSIAN_CLOUD_ID'] + '/rest/api/2',
    }
    @client = JIRA::Client.new(options)
  end
end

The token required above is fetched from the Atlassian OAuth2 client using atlassian OAuth2 provider,

SCOPES = %w[
read:me
read:account
read:jira-work
read:jira-user
write:jira-work
]

OmniAuth.config.allowed_request_methods = [:get, :post]

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :atlassian_oauth2, ENV['ATLASSIAN_CLIENT_ID'], ENV['ATLASSIAN_CLIENT_SECRET'],
    scope: SCOPES.join(' '),
    prompt: "consent"
end

with 'omniauth-atlassian-oauth2' gem.

@marlinpierce you approve of this approach, I'm prepared and interested in starting to work on the contribution.