Skip to content

Commit

Permalink
Merge pull request #404 from codeforjapan/line_login-2
Browse files Browse the repository at this point in the history
Retry LINE login
  • Loading branch information
ayuki-joto authored Aug 2, 2022
2 parents 1494f15 + b81a597 commit 08909bc
Show file tree
Hide file tree
Showing 22 changed files with 341 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ gem "wicked_pdf", "~> 1.4"
gem "deface"
gem "newrelic_rpm"

gem "omniauth-line_login", path: "omniauth-line_login"

gem "decidim-user_extension", path: "decidim-user_extension"

# When rails >= 5.2.5 or 6.0.3.6, you can remove this gem.
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ PATH
decidim-core
rails

PATH
remote: omniauth-line_login
specs:
omniauth-line_login (0.1.0)
omniauth-oauth2

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -913,6 +919,7 @@ DEPENDENCIES
listen (~> 3.1)
mimemagic (~> 0.3.10)
newrelic_rpm
omniauth-line_login!
puma (>= 5.0.0)
rspec-rails
rubocop-faker
Expand Down
25 changes: 25 additions & 0 deletions config/initializers/omniauth_line.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

def setup_provider_proc(provider, config_mapping = {})
lambda do |env|
request = Rack::Request.new(env)
organization = Decidim::Organization.find_by(host: request.host)
provider_config = organization.enabled_omniauth_providers[provider]

config_mapping.each do |option_key, config_key|
env["omniauth.strategy"].options[option_key] = provider_config[config_key]
end
end
end

Rails.application.config.middleware.use OmniAuth::Builder do
omniauth_config = Rails.application.secrets[:omniauth]

if omniauth_config && omniauth_config[:line_login].present?
require "omniauth-line_login"
provider(
:line_login,
setup: setup_provider_proc(:line_login, client_id: :client_id, client_secret: :client_secret)
)
end
end
8 changes: 8 additions & 0 deletions config/locales/line_login.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
en:
decidim:
system:
organizations:
omniauth_settings:
line_login:
client_id: Channel ID
client_secret: Channel secret
8 changes: 8 additions & 0 deletions config/locales/line_login.ja.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ja:
decidim:
system:
organizations:
omniauth_settings:
line_login:
client_id: チャンネルID
client_secret: チャンネルシークレット
8 changes: 8 additions & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ default: &default
icon_path: decidim/brands/google.svg
client_id: <%= ENV["OMNIAUTH_GOOGLE_CLIENT_ID"] %>
client_secret: <%= ENV["OMNIAUTH_GOOGLE_CLIENT_SECRET"] %>
line_login:
enabled: true
client_id: <%= ENV["OMNIAUTH_LINE_LOGIN_CHANNEL_ID"] %>
client_secret: <%= ENV["OMNIAUTH_LINE_LOGIN_CHANNEL_SECRET"] %>
geocoder:
here_api_key: <%= ENV["GEOCODER_API_KEY"] %>
etherpad:
Expand All @@ -40,6 +44,10 @@ development:
developer:
enabled: true
icon: phone
line_login:
enabled: true
client_id:
client_secret:

test:
<<: *default
Expand Down
11 changes: 11 additions & 0 deletions omniauth-line_login/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

# rspec failure tracking
.rspec_status
3 changes: 3 additions & 0 deletions omniauth-line_login/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper
6 changes: 6 additions & 0 deletions omniauth-line_login/.rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AllCops:
NewCops: enable

Naming/FileName:
Exclude:
- 'lib/omniauth-line_login.rb'
10 changes: 10 additions & 0 deletions omniauth-line_login/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

source 'https://rubygems.org'

# Specify your gem's dependencies in omniauth-line_login.gemspec
gemspec

gem 'rake', '~> 13.0'

gem 'rspec', '~> 3.0'
60 changes: 60 additions & 0 deletions omniauth-line_login/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
PATH
remote: .
specs:
omniauth-line_login (0.1.0)
omniauth-oauth2

GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.5.0)
faraday (2.2.0)
faraday-net_http (~> 2.0)
ruby2_keywords (>= 0.0.4)
faraday-net_http (2.0.2)
hashie (5.0.0)
jwt (2.3.0)
multi_json (1.15.0)
multi_xml (0.6.0)
oauth2 (1.4.9)
faraday (>= 0.17.3, < 3.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
omniauth (2.1.0)
hashie (>= 3.4.6)
rack (>= 2.2.3)
rack-protection
omniauth-oauth2 (1.7.2)
oauth2 (~> 1.4)
omniauth (>= 1.9, < 3)
rack (2.2.3)
rack-protection (2.2.0)
rack
rake (13.0.6)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
ruby2_keywords (0.0.5)

PLATFORMS
arm64-darwin-21

DEPENDENCIES
omniauth-line_login!
rake (~> 13.0)
rspec (~> 3.0)

BUNDLED WITH
2.3.11
29 changes: 29 additions & 0 deletions omniauth-line_login/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# OmniAuth::LineLogin

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/omniauth/line_login`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem

## Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add omniauth-line_login

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install omniauth-line_login

## Usage

TODO: Write usage instructions here

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/takahashim/omniauth-line_login.
8 changes: 8 additions & 0 deletions omniauth-line_login/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task default: :spec
15 changes: 15 additions & 0 deletions omniauth-line_login/bin/console
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'bundler/setup'
require 'omniauth/line_login'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require 'irb'
IRB.start(__FILE__)
8 changes: 8 additions & 0 deletions omniauth-line_login/bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
5 changes: 5 additions & 0 deletions omniauth-line_login/lib/omniauth-line_login.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require_relative 'omniauth/line_login/version'
require_relative 'omniauth/line_login/error'
require_relative 'omniauth/strategies/line_login'
7 changes: 7 additions & 0 deletions omniauth-line_login/lib/omniauth/line_login/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module OmniAuth
module LineLogin
class Error < StandardError; end
end
end
7 changes: 7 additions & 0 deletions omniauth-line_login/lib/omniauth/line_login/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module OmniAuth
module LineLogin
VERSION = '0.1.0'
end
end
58 changes: 58 additions & 0 deletions omniauth-line_login/lib/omniauth/strategies/line_login.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

module OmniAuth
module Strategies
# OmniAuth Strategy for LINE Login
class LineLogin < OmniAuth::Strategies::OAuth2
option :name, 'line_login'
option :scope, 'profile openid'

option :client_options, {
site: 'https://api.line.me',
authorize_url: 'https://access.line.me/oauth2/v2.1/authorize',
token_url: '/oauth2/v2.1/token'
}

uid { raw_info['sub'] }

info do
{
user_id: raw_info['sub'],
name: raw_info['name'],
email: raw_info['email'],
image: raw_info['picture']
}
end

def raw_info
@raw_info ||= verify_id_token
end

private

def authorize_params
super.tap do |params|
params[:nonce] = SecureRandom.uuid
session['omniauth.nonce'] = params[:nonce]
end
end

def callback_url
full_host + script_name + callback_path
end

def verify_id_token
@id_token_payload ||= client.request(:post, 'https://api.line.me/oauth2/v2.1/verify',
{
body: {
id_token: access_token['id_token'],
client_id: options.client_id,
nonce: session.delete('omniauth.nonce')
}
}).parsed
Rails.logger.info("token:#{@id_token_payload.inspect}")
@id_token_payload
end
end
end
end
35 changes: 35 additions & 0 deletions omniauth-line_login/omniauth-line_login.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require_relative 'lib/omniauth/line_login/version'

Gem::Specification.new do |spec|
spec.name = 'omniauth-line_login'
spec.version = OmniAuth::LineLogin::VERSION
spec.authors = ['takahashim']
spec.email = ['takahashimm@gmail.com']

spec.summary = 'Unofficial OmniAuth Strategy for LINE Login'
spec.description = 'Unofficial OmniAuth Strategy for LINE Login'
spec.homepage = 'https://github.com/codeforjapan/decidim-cfj'
spec.required_ruby_version = '>= 2.6.0' # rubocop:disable Gemspec/RequiredRubyVersion

spec.metadata['homepage_uri'] = spec.homepage
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."

# Specify which files should be added to the gem when it is released.
spec.files = Dir[
'lib/**/*',
'Rakefile',
'README.md'
]
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']

spec.add_dependency 'omniauth-oauth2'

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
spec.metadata['rubygems_mfa_required'] = 'true'
end
6 changes: 6 additions & 0 deletions omniauth-line_login/sig/omniauth/line_login.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module OmniAuth
module LineLogin
VERSION: String
# See the writing guide of rbs: https://github.com/ruby/rbs#guides
end
end
15 changes: 15 additions & 0 deletions omniauth-line_login/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'omniauth/line_login'

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'

# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!

config.expect_with :rspec do |c|
c.syntax = :expect
end
end

0 comments on commit 08909bc

Please sign in to comment.