omniauth-qiita-v2 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +139 -0
- data/Rakefile +11 -0
- data/lib/omniauth/qiita_v2/version.rb +7 -0
- data/lib/omniauth/strategies/qiita_v2.rb +140 -0
- data/lib/omniauth-qiita-v2.rb +4 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0a90625468134bcc53ef80ece9eb86fb4af2ead2ee299c6984f574a2f9d5ed13
|
4
|
+
data.tar.gz: 20e949b5096f0ed4ea1a6c53aa79a2452539c9914fd4a71c380a0011edb122f3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2f1b320bc2142f79b5b6351ca54d74538ab2826af33ddc7f7789108d532a5d1b543fa90db7f6d4f6b183dc8bab3ddde94120273fc254674bfe18442558d87847
|
7
|
+
data.tar.gz: c175f42b745e6108ad6e552b5045e1d8fc3557d6d1ab401b2ded3e24c5f00e52b0e55f981dd986573d6d5426b622cd0c5522807459513430278ea8799a387c24
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 Masahiro
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# OmniauthQiitaV2
|
2
|
+
|
3
|
+
[](https://github.com/cadenza-tech/omniauth-qiita-v2/blob/main/LICENSE.txt) [](https://github.com/cadenza-tech/omniauth-qiita-v2/blob/main/CHANGELOG.md) [](https://github.com/cadenza-tech/omniauth-qiita-v2/actions?query=workflow%3Arelease) [](https://github.com/cadenza-tech/omniauth-qiita-v2/actions?query=workflow%3Atest) [](https://github.com/cadenza-tech/omniauth-qiita-v2/actions?query=workflow%3Alint)
|
4
|
+
|
5
|
+
Qiita strategy for OmniAuth.
|
6
|
+
|
7
|
+
- [Installation](#installation)
|
8
|
+
- [Usage](#usage)
|
9
|
+
- [Rails Configuration with Devise](#rails-configuration-with-devise)
|
10
|
+
- [Configuration Options](#configuration-options)
|
11
|
+
- [Auth Hash](#auth-hash)
|
12
|
+
- [Changelog](#changelog)
|
13
|
+
- [Contributing](#contributing)
|
14
|
+
- [License](#license)
|
15
|
+
- [Code of Conduct](#code-of-conduct)
|
16
|
+
- [Sponsor](#sponsor)
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Install the gem and add to the application's Gemfile by executing:
|
21
|
+
|
22
|
+
```bash
|
23
|
+
bundle add omniauth-qiita-v2
|
24
|
+
```
|
25
|
+
|
26
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
27
|
+
|
28
|
+
```bash
|
29
|
+
gem install omniauth-qiita-v2
|
30
|
+
```
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
### Rails Configuration with Devise
|
35
|
+
|
36
|
+
Add the following to `config/initializers/devise.rb`:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
# config/initializers/devise.rb
|
40
|
+
Devise.setup do |config|
|
41
|
+
config.omniauth :qiita_v2, ENV['QIITA_CLIENT_ID'], ENV['QIITA_CLIENT_SECRET']
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Add the OmniAuth callbacks routes to `config/routes.rb`:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
# config/routes.rb
|
49
|
+
Rails.application.routes.draw do
|
50
|
+
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
Add the OmniAuth configuration to your Devise model:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class User < ApplicationRecord
|
58
|
+
devise :database_authenticatable, :registerable,
|
59
|
+
:recoverable, :rememberable, :validatable,
|
60
|
+
:omniauthable, omniauth_providers: %i[qiita_v2]
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
### Configuration Options
|
65
|
+
|
66
|
+
You can configure several options:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# config/initializers/devise.rb
|
70
|
+
Devise.setup do |config|
|
71
|
+
config.omniauth :qiita_v2, ENV['QIITA_CLIENT_ID'], ENV['QIITA_CLIENT_SECRET'],
|
72
|
+
{
|
73
|
+
scope: 'read_qiita write_qiita', # Specify OAuth scopes
|
74
|
+
callback_path: '/custom/qiita_v2/callback' # Custom callback path
|
75
|
+
}
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
Available scopes:
|
80
|
+
|
81
|
+
- `read_qiita` - Access to read data from Qiita
|
82
|
+
- `write_qiita` - Access to write data to Qiita
|
83
|
+
|
84
|
+
### Auth Hash
|
85
|
+
|
86
|
+
After successful authentication, the auth hash will be available in `request.env['omniauth.auth']`:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
{
|
90
|
+
provider: 'qiita_v2',
|
91
|
+
uid: 'example_qiita_user_john_doe',
|
92
|
+
info: {
|
93
|
+
name: 'John Doe',
|
94
|
+
nickname: 'example_qiita_user_john_doe',
|
95
|
+
image: 'https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/...',
|
96
|
+
description: 'Software Developer',
|
97
|
+
location: 'Tokyo, Japan',
|
98
|
+
followees_count: 50,
|
99
|
+
followers_count: 100,
|
100
|
+
items_count: 30,
|
101
|
+
urls: {
|
102
|
+
website: 'https://example.com',
|
103
|
+
twitter: 'https://x.com/...',
|
104
|
+
facebook: 'https://facebook.com/...',
|
105
|
+
linkedin: 'https://www.linkedin.com/in/...',
|
106
|
+
github: 'https://github.com/...'
|
107
|
+
}
|
108
|
+
},
|
109
|
+
credentials: {
|
110
|
+
token: 'access_token_here',
|
111
|
+
expires: false
|
112
|
+
},
|
113
|
+
extra: {
|
114
|
+
raw_info: {
|
115
|
+
# Complete user information from Qiita API
|
116
|
+
}
|
117
|
+
}
|
118
|
+
}
|
119
|
+
```
|
120
|
+
|
121
|
+
## Changelog
|
122
|
+
|
123
|
+
See [CHANGELOG.md](https://github.com/cadenza-tech/omniauth-qiita-v2/blob/main/CHANGELOG.md).
|
124
|
+
|
125
|
+
## Contributing
|
126
|
+
|
127
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cadenza-tech/omniauth-qiita-v2. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/cadenza-tech/omniauth-qiita-v2/blob/main/CODE_OF_CONDUCT.md).
|
128
|
+
|
129
|
+
## License
|
130
|
+
|
131
|
+
The gem is available as open source under the terms of the [MIT License](https://github.com/cadenza-tech/omniauth-qiita-v2/blob/main/LICENSE.txt).
|
132
|
+
|
133
|
+
## Code of Conduct
|
134
|
+
|
135
|
+
Everyone interacting in the OmniauthQiitaV2 project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cadenza-tech/omniauth-qiita-v2/blob/main/CODE_OF_CONDUCT.md).
|
136
|
+
|
137
|
+
## Sponsor
|
138
|
+
|
139
|
+
You can sponsor this project on [Patreon](https://patreon.com/CadenzaTech).
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'rubocop/rake_task'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
RuboCop::RakeTask.new(:rubocop) do |task|
|
10
|
+
task.options = ['--format', ENV['RUBOCOP_FORMAT']] if ENV['RUBOCOP_FORMAT']
|
11
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'omniauth-oauth2'
|
4
|
+
|
5
|
+
module OmniAuth
|
6
|
+
module Strategies
|
7
|
+
class QiitaV2 < OmniAuth::Strategies::OAuth2
|
8
|
+
DEFAULT_SCOPE = 'read_qiita'
|
9
|
+
USER_INFO_URL = '/api/v2/authenticated_user'
|
10
|
+
|
11
|
+
option :name, 'qiita_v2'
|
12
|
+
|
13
|
+
option :client_options, {
|
14
|
+
site: 'https://qiita.com',
|
15
|
+
authorize_url: '/api/v2/oauth/authorize',
|
16
|
+
token_url: '/api/v2/access_tokens'
|
17
|
+
}
|
18
|
+
|
19
|
+
option :token_params, { parse: :json }
|
20
|
+
|
21
|
+
option :authorize_options, [:scope, :state]
|
22
|
+
|
23
|
+
option :scope, DEFAULT_SCOPE
|
24
|
+
|
25
|
+
uid { raw_info['id'] }
|
26
|
+
|
27
|
+
info do
|
28
|
+
prune!({
|
29
|
+
name: raw_info['name'],
|
30
|
+
nickname: raw_info['id'],
|
31
|
+
email: nil, # Qiita API v2 does not provide email
|
32
|
+
description: raw_info['description'],
|
33
|
+
image: raw_info['profile_image_url'],
|
34
|
+
location: raw_info['location'],
|
35
|
+
followees_count: raw_info['followees_count'],
|
36
|
+
followers_count: raw_info['followers_count'],
|
37
|
+
items_count: raw_info['items_count'],
|
38
|
+
urls: {
|
39
|
+
website: raw_info['website_url'],
|
40
|
+
twitter: raw_info['twitter_screen_name'] ? "https://x.com/#{raw_info['twitter_screen_name']}" : nil,
|
41
|
+
facebook: raw_info['facebook_id'] ? "https://facebook.com/#{raw_info['facebook_id']}" : nil,
|
42
|
+
linkedin: raw_info['linkedin_id'] ? "https://www.linkedin.com/in/#{raw_info['linkedin_id']}" : nil,
|
43
|
+
github: raw_info['github_login_name'] ? "https://github.com/#{raw_info['github_login_name']}" : nil
|
44
|
+
}.compact
|
45
|
+
})
|
46
|
+
end
|
47
|
+
|
48
|
+
extra do
|
49
|
+
hash = {}
|
50
|
+
hash[:raw_info] = raw_info unless skip_info?
|
51
|
+
prune!(hash)
|
52
|
+
end
|
53
|
+
|
54
|
+
credentials do
|
55
|
+
hash = { token: access_token.token }
|
56
|
+
hash[:expires] = access_token.expires?
|
57
|
+
hash[:expires_at] = access_token.expires_at if access_token.expires?
|
58
|
+
hash[:refresh_token] = access_token.refresh_token if access_token.refresh_token
|
59
|
+
hash
|
60
|
+
end
|
61
|
+
|
62
|
+
def raw_info # rubocop:disable Metrics/AbcSize
|
63
|
+
@raw_info ||= begin
|
64
|
+
access_token.get(USER_INFO_URL).parsed || {}
|
65
|
+
rescue ::OAuth2::Error => e
|
66
|
+
case e.response.status
|
67
|
+
when 401
|
68
|
+
log :error, '401 Unauthorized - Invalid access token'
|
69
|
+
raise ::OmniAuth::NoSessionError.new('Invalid access token')
|
70
|
+
when 403
|
71
|
+
log :error, '403 Forbidden - Insufficient permissions'
|
72
|
+
raise ::OmniAuth::NoSessionError.new('Insufficient permissions')
|
73
|
+
when 404
|
74
|
+
log :error, '404 Not Found - User not found'
|
75
|
+
raise ::OmniAuth::NoSessionError.new('User not found')
|
76
|
+
else
|
77
|
+
log :error, "API Error: #{e.response.status} - #{e.message}"
|
78
|
+
raise e
|
79
|
+
end
|
80
|
+
rescue ::Errno::ETIMEDOUT
|
81
|
+
log :error, 'Connection timed out'
|
82
|
+
raise ::OmniAuth::NoSessionError.new('Connection timed out')
|
83
|
+
rescue ::SocketError => e
|
84
|
+
log :error, "Network error: #{e.message}"
|
85
|
+
raise ::OmniAuth::NoSessionError.new('Network error')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def callback_url
|
90
|
+
options[:redirect_uri] || (full_host + script_name + callback_path)
|
91
|
+
end
|
92
|
+
|
93
|
+
def authorize_params
|
94
|
+
super.tap do |params|
|
95
|
+
['scope', 'state'].each do |v|
|
96
|
+
params[v.to_sym] = request.params[v] if request.params[v]
|
97
|
+
end
|
98
|
+
params[:scope] ||= DEFAULT_SCOPE
|
99
|
+
session['omniauth.state'] = params[:state] if params[:state]
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def prune!(hash)
|
106
|
+
hash.delete_if do |_, value|
|
107
|
+
prune!(value) if value.is_a?(Hash)
|
108
|
+
value.nil? || (value.respond_to?(:empty?) && value.empty?)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def build_access_token
|
113
|
+
verifier = request.params['code']
|
114
|
+
token_params = {
|
115
|
+
redirect_uri: callback_url,
|
116
|
+
client_id: options.client_id,
|
117
|
+
client_secret: options.client_secret
|
118
|
+
}.merge(token_params_from_options)
|
119
|
+
client.auth_code.get_token(verifier, token_params, deep_symbolize(options.auth_token_params))
|
120
|
+
rescue ::OAuth2::Error => e
|
121
|
+
log :error, "Failed to build access token: #{e.message}"
|
122
|
+
fail!(:invalid_credentials, e)
|
123
|
+
rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
|
124
|
+
log :error, "Timeout during token exchange: #{e.message}"
|
125
|
+
fail!(:timeout, e)
|
126
|
+
end
|
127
|
+
|
128
|
+
def token_params_from_options
|
129
|
+
return {} unless options.token_params
|
130
|
+
|
131
|
+
if options.token_params.is_a?(Hash)
|
132
|
+
params = options.token_params
|
133
|
+
else
|
134
|
+
params = options.token_params.to_hash
|
135
|
+
end
|
136
|
+
params.transform_keys(&:to_sym)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-qiita-v2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Masahiro
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: omniauth
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '2.0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '2.0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: omniauth-oauth2
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '1.8'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.8'
|
40
|
+
description: Qiita strategy for OmniAuth
|
41
|
+
email:
|
42
|
+
- watanabe@cadenza-tech.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- ".rspec"
|
48
|
+
- CHANGELOG.md
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- lib/omniauth-qiita-v2.rb
|
53
|
+
- lib/omniauth/qiita_v2/version.rb
|
54
|
+
- lib/omniauth/strategies/qiita_v2.rb
|
55
|
+
homepage: https://github.com/cadenza-tech/omniauth-qiita-v2/tree/v0.0.0
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata:
|
59
|
+
homepage_uri: https://github.com/cadenza-tech/omniauth-qiita-v2/tree/v0.0.0
|
60
|
+
source_code_uri: https://github.com/cadenza-tech/omniauth-qiita-v2/tree/v0.0.0
|
61
|
+
changelog_uri: https://github.com/cadenza-tech/omniauth-qiita-v2/blob/v0.0.0/CHANGELOG.md
|
62
|
+
bug_tracker_uri: https://github.com/cadenza-tech/omniauth-qiita-v2/issues
|
63
|
+
documentation_uri: https://rubydoc.info/gems/omniauth-qiita-v2/0.0.0
|
64
|
+
funding_uri: https://patreon.com/CadenzaTech
|
65
|
+
rubygems_mfa_required: 'true'
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 2.5.0
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.6.7
|
81
|
+
specification_version: 4
|
82
|
+
summary: Qiita strategy for OmniAuth
|
83
|
+
test_files: []
|