omniauth-atlassian-oauth2-0.2.0/0000755000175000017500000000000013751213754015546 5ustar pravipraviomniauth-atlassian-oauth2-0.2.0/lib/0000755000175000017500000000000013751213754016314 5ustar pravipraviomniauth-atlassian-oauth2-0.2.0/lib/omniauth/0000755000175000017500000000000013751213754020140 5ustar pravipraviomniauth-atlassian-oauth2-0.2.0/lib/omniauth/strategies/0000755000175000017500000000000013751213754022312 5ustar pravipraviomniauth-atlassian-oauth2-0.2.0/lib/omniauth/strategies/atlassian_oauth2.rb0000644000175000017500000000503013751213754026076 0ustar pravipravi# frozen_string_literal: true require 'omniauth/strategies/oauth2' require 'uri' # Potential scopes: https://developer.atlassian.com/cloud/jira/platform/scopes/ # offline_access read:jira-user read:jira-work write:jira-work manage:jira-project manage:jira-configuration # # Separate scopes with a space (%20) # https://developer.atlassian.com/cloud/jira/platform/oauth-2-authorization-code-grants-3lo-for-apps/ module OmniAuth module Strategies # Omniauth strategy for Atlassian class AtlassianOauth2 < OmniAuth::Strategies::OAuth2 option :name, 'atlassian_oauth2' option :client_options, site: 'https://auth.atlassian.com', authorize_url: 'https://auth.atlassian.com/authorize', token_url: 'https://auth.atlassian.com/oauth/token', audience: 'api.atlassian.com' option :authorize_params, prompt: 'consent', audience: 'api.atlassian.com' uid do raw_info['myself']['accountId'] end info do { name: raw_info['myself']['displayName'], email: raw_info['myself']['emailAddress'], nickname: raw_info['myself']['name'], location: raw_info['myself']['timeZone'], image: raw_info['myself']['avatarUrls']['48x48'] } end extra do { 'raw_info' => raw_info } end def raw_info return @raw_info if @raw_info # NOTE: api.atlassian.com, not auth.atlassian.com! accessible_resources_url = 'https://api.atlassian.com/oauth/token/accessible-resources' sites = JSON.parse(access_token.get(accessible_resources_url).body) # Jira's OAuth gives us many potential sites. To request information # about the user for the OmniAuth hash, pick the first one that has the # necessary 'read:jira-user' scope. jira_user_scope = 'read:jira-user' site = sites.find do |candidate_site| candidate_site['scopes'].include?(jira_user_scope) end unless site raise "No site found with scope #{jira_user_scope}, please ensure the scope ${jira_user_scope} is added to your OmniAuth config" end cloud_id = site['id'] base_url = "https://api.atlassian.com/ex/jira/#{cloud_id}" myself_url = "#{base_url}/rest/api/3/myself" myself = JSON.parse(access_token.get(myself_url).body) @raw_info ||= { 'site' => site, 'sites' => sites, 'myself' => myself } end end end end omniauth-atlassian-oauth2-0.2.0/lib/omniauth/atlassian_oauth2/0000755000175000017500000000000013751213754023401 5ustar pravipraviomniauth-atlassian-oauth2-0.2.0/lib/omniauth/atlassian_oauth2/version.rb0000644000175000017500000000015013751213754025407 0ustar pravipravi# frozen_string_literal: true module OmniAuth module AtlassianOauth2 VERSION = '0.2.0' end end omniauth-atlassian-oauth2-0.2.0/lib/omniauth/atlassian_oauth2.rb0000644000175000017500000000011613751213754023724 0ustar pravipravi# frozen_string_literal: true require 'omniauth/strategies/atlassian_oauth2' omniauth-atlassian-oauth2-0.2.0/lib/omniauth-atlassian-oauth2.rb0000644000175000017500000000010313751213754023634 0ustar pravipravi# frozen_string_literal: true require 'omniauth/atlassian_oauth2' omniauth-atlassian-oauth2-0.2.0/omniauth-atlassian-oauth2.gemspec0000644000175000017500000000205613751213754024117 0ustar pravipravi# frozen_string_literal: true require File.expand_path( File.join('..', 'lib', 'omniauth', 'atlassian_oauth2', 'version'), __FILE__ ) Gem::Specification.new do |gem| gem.name = 'omniauth-atlassian-oauth2' gem.version = OmniAuth::AtlassianOauth2::VERSION gem.license = 'MIT' gem.summary = %(An Atlassian OAuth2 strategy for OmniAuth 1.x) gem.description = %(An Atlassian OAuth2 strategy for OmniAuth 1.x. This allows you to login to Atlassian with your ruby app.) gem.authors = ['Ben Standefer'] gem.email = ['benstandefer@gmail.com'] gem.homepage = 'https://github.com/aguynamedben/omniauth-atlassian-oauth2' gem.files = `git ls-files`.split("\n") gem.require_paths = ['lib'] gem.required_ruby_version = '>= 2.1' gem.add_runtime_dependency 'omniauth', '>= 1.1.1' gem.add_runtime_dependency 'omniauth-oauth2', '>= 1.5' gem.add_development_dependency 'rake', '~> 12.0' gem.add_development_dependency 'rspec', '~> 3.6' gem.add_development_dependency 'rubocop', '~> 0.49' end omniauth-atlassian-oauth2-0.2.0/.rubocop.yml0000644000175000017500000000067713751213754020032 0ustar pravipraviClassLength: Enabled: false Layout/IndentHeredoc: Enabled: false Metrics/AbcSize: Enabled: false Metrics/BlockLength: ExcludedMethods: ['describe', 'context'] Metrics/CyclomaticComplexity: Enabled: false Metrics/LineLength: Enabled: false Metrics/MethodLength: Enabled: false Metrics/PerceivedComplexity: Enabled: false Naming: Enabled: false Style/MutableConstant: Enabled: false Gemspec/RequiredRubyVersion: Enabled: false omniauth-atlassian-oauth2-0.2.0/Gemfile0000644000175000017500000000010613751213754017036 0ustar pravipravi# frozen_string_literal: true source 'https://rubygems.org' gemspec omniauth-atlassian-oauth2-0.2.0/README.md0000644000175000017500000001176713751213754017041 0ustar pravipravi[![Gem Version](https://badge.fury.io/rb/omniauth-atlassian-oauth2.svg)](https://badge.fury.io/rb/omniauth-atlassian-oauth2) # OmniAuth Atlassian OAuth2 Strategy Strategy to authenticate with Atlassian via OAuth2 in OmniAuth. Get your API key at: https://developer.atlassian.com/apps/ Note the Client ID and the Client Secret. For more details, read the Atlassian docs about OAuth 2.0 (3LO): https://developer.atlassian.com/cloud/jira/platform/oauth-2-authorization-code-grants-3lo-for-apps/ Note that as of March 2019, the OAuth 2.0 APIs at in "developer preview" mode with Atlassian. See [ACJIRA-1588](https://ecosystem.atlassian.net/browse/ACJIRA-1588) for updates. ## Installation Add to your `Gemfile`: ```ruby gem 'omniauth-atlassian-oauth2' ``` Then `bundle install`. ## Atlassian API Setup * Go to 'https://developer.atlassian.com/apps/' * Create a new app. * Note the Client ID and Secret values in the App Details section. * Under APIs and Features, add the "Authorization code grants" feature. Configure the feature with your callback URL (something like http://localhost:3000/auth/atlassian_oauth2/callback). * Under APIs and Features, add the "Jira platform REST API" API. Configure the API by adding the "View Jira issue data" and "View user profiles" scopes. These coincide with the scopes listed in the [Jira scopes](https://developer.atlassian.com/cloud/jira/platform/oauth-2-authorization-code-grants-3lo-for-apps/) section of the Atlassian Docs. Add additional scopes in this UI as needed. ## Usage Here's an example for adding the middleware to a Rails app in `config/initializers/omniauth.rb`: ```ruby Rails.application.config.middleware.use OmniAuth::Builder do provider :atlassian_oauth2, ENV['ATLASSIAN_CLIENT_ID'], ENV['ATLASSIAN_CLIENT_SECRET'] scope: "offline_access read:jira-user read:jira-work", prompt: "consent", end ``` This OmniAuth strategy makes API calls that require the `read:jira-user` scope, so that scope must be included by you in your OmniAuth configuration. The `offline_access` scope must be included if you wish to attain a refresh token. You may wish to include additional scopes depending on how you've configured your app in the Atlassian UI. You can now access the OmniAuth Atlassian OAuth2 URL: `/auth/atlassian_oauth2` NOTE: While developing your application, if you change the scope in the initializer you will need to restart your app server. Remember that either the 'email' or 'profile' scope is required! ## Auth Hash Here's an example of an authentication hash available in the callback by accessing `request.env['omniauth.auth']`: Note: Atlassian's OAuth2 flow grants access to potentially many Atlassian "sites", and each site defines a user on its own. There's no single definitive profile of the user. We must loop through the sites and call the `/rest/api/3/myself` endpoint for each. `auth_hash['extra']['raw_info']['site']` is the site that was used to populate `auth_hash['info']` and `auth_hash['uid']`. `auth_hash['extra']['raw_info']['sites']` is the data for all the sites available for the user. After authing a user, when you make API calls over time, you should follow the [Atlassian OAuth 2.0 docs](https://developer.atlassian.com/cloud/jira/platform/oauth-2-authorization-code-grants-3lo-for-apps/) and continue to check the `accessible-resources` endpoint to ensure your app continues to have access to the sites you expect. ```ruby { "provider" => "atlassian_oauth2", "uid" => "100000000000000000000", "info" => { "name" => "John Smith", "email" => "john@example.com", "nickname" => "john_smith", # username "location" => "Australia/Sydney", # time zone "image" => "https://whatever.atlassiancdn.com/photo_48x48.jpg", # 48x48 pixels }, "credentials" => { "token" => "TOKEN", "refresh_token" => "REFRESH_TOKEN", "expires_at" => 1496120719, "expires" => true }, "extra" => { "raw_info" => { "site" => {}, "sites" => {}, "myself" => {}, } } } ``` ## License Copyright (c) 2019 by Ben Standefer Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. omniauth-atlassian-oauth2-0.2.0/CHANGELOG.md0000644000175000017500000000062313751213754017360 0ustar pravipravi# Changelog All notable changes to this project will be documented in this file. Follows the religion set forth in https://keepachangelog.com/en/1.0.0/ ## 0.2.0 - 2018-03-02 ### Fixed - Fixed issue where callback phase was unnecessarily slow. ## 0.1.0 - 2018-03-01 ### Added - First version released. - Docs in README. - Supports Jira API, refresh tokens, populating auth hash profile values, etc. omniauth-atlassian-oauth2-0.2.0/.gitignore0000644000175000017500000000000513751213754017531 0ustar pravipravi*.gemomniauth-atlassian-oauth2-0.2.0/Gemfile.lock0000644000175000017500000000324113751213754017770 0ustar pravipraviPATH remote: . specs: omniauth-atlassian-oauth2 (0.1.0) omniauth (>= 1.1.1) omniauth-oauth2 (>= 1.5) GEM remote: https://rubygems.org/ specs: ast (2.4.0) diff-lcs (1.3) faraday (0.15.4) multipart-post (>= 1.2, < 3) hashie (3.6.0) jaro_winkler (1.5.2) jwt (2.1.0) multi_json (1.13.1) multi_xml (0.6.0) multipart-post (2.0.0) oauth2 (1.4.1) faraday (>= 0.8, < 0.16.0) jwt (>= 1.0, < 3.0) multi_json (~> 1.3) multi_xml (~> 0.5) rack (>= 1.2, < 3) omniauth (1.9.0) hashie (>= 3.4.6, < 3.7.0) rack (>= 1.6.2, < 3) omniauth-oauth2 (1.6.0) oauth2 (~> 1.1) omniauth (~> 1.9) parallel (1.14.0) parser (2.6.0.0) ast (~> 2.4.0) powerpack (0.1.2) psych (3.1.0) rack (2.0.6) rainbow (3.0.0) rake (12.3.2) rspec (3.8.0) rspec-core (~> 3.8.0) rspec-expectations (~> 3.8.0) rspec-mocks (~> 3.8.0) rspec-core (3.8.0) rspec-support (~> 3.8.0) rspec-expectations (3.8.2) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-mocks (3.8.0) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-support (3.8.0) rubocop (0.65.0) jaro_winkler (~> 1.5.1) parallel (~> 1.10) parser (>= 2.5, != 2.5.1.1) powerpack (~> 0.1) psych (>= 3.1.0) rainbow (>= 2.2.2, < 4.0) ruby-progressbar (~> 1.7) unicode-display_width (~> 1.4.0) ruby-progressbar (1.10.0) unicode-display_width (1.4.1) PLATFORMS ruby DEPENDENCIES omniauth-atlassian-oauth2! rake (~> 12.0) rspec (~> 3.6) rubocop (~> 0.49) BUNDLED WITH 1.17.1