omniauth-oauth2-oneid 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2b61ad0817150bd052d2e76ebd163ebf48f7e18fa6f90283604a432c1fcf75ef
4
+ data.tar.gz: 53132f583c695c88a87ba02d774262bfe5a07bb205b9b33e3a5959b3d9537d3e
5
+ SHA512:
6
+ metadata.gz: ff7e75bced530609cd8e78a082645c27a9447674e5c12925fe000d2788edaef7f1cc10318507d7be6cebf04666432784a534565df3326d38076d7d0db8163cce
7
+ data.tar.gz: 62d1293cbad8e21dbeda14b1b4e5b1ce76c103d8b5d22d42d9156cf3d314d2ee9b564fd72d60db346199456dc67e00e387f7d0b6da042a8ae4c265dcb7b8b4c6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "omniauth-oauth2", "~> 1.8"
8
+ gem "rake", "~> 13.0"
9
+ gem 'faraday', '~> 2.12', '>= 2.12.2'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Patcharapong
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 @@
1
+ # Omniauth::Strategies::OAuth2OneID
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "omniauth/sso/oneid"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "irb"
11
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OmniAuth
4
+ module OAuth2OneID
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,165 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'omniauth-oauth2'
4
+ require 'faraday'
5
+ require 'json'
6
+
7
+ module OmniAuth
8
+ module Strategies
9
+ ConnectionError = Class.new(Faraday::ConnectionFailed)
10
+ TimeoutError = Class.new(Faraday::TimeoutError)
11
+
12
+ class OAuth2OneID < OmniAuth::Strategies::OAuth2
13
+ option :name, "oauth2_oneid"
14
+ option :provider_ignores_state, true
15
+ option :client_options,
16
+ {
17
+ site: "https://one.th",
18
+ user_info_url: "/go-api/v1/citizen/service/account?q=all",
19
+ auth_scheme: :request_body,
20
+ authorize_url: "/api/oauth/getcode",
21
+ token_url: "/oauth/token",
22
+ business_url: "/api/v3/business/service/list-business",
23
+ }
24
+ option :businesses, []
25
+ option :auto_block,
26
+ {
27
+ enabled: false,
28
+ gitlab_api: "http://localhost",
29
+ gitlab_token: ""
30
+ }
31
+
32
+ # Get uid
33
+ uid do
34
+ user_path['id'].to_s
35
+ end
36
+
37
+ # Get account info
38
+ info do
39
+ hash = {
40
+ name: "#{user_path['account_title_th']}#{user_path['first_name_th']} #{user_path['last_name_th']}",
41
+ email: user_path['thai_email'],
42
+ email_verified: user_path['thai_email'],
43
+ urls: {
44
+ oneid: "https://one.th/portal_index"
45
+ }
46
+ }
47
+ if user_path['account_category '] != "Residential"
48
+ hash['name'] = "#{user_path['account_title_eng']} #{user_path['first_name_eng']} #{user_path['last_name_eng']}"
49
+ end
50
+
51
+ if !check_business
52
+ gitlab_auto_block
53
+ raise "Forbidden. Unauthorized access restricted."
54
+ end
55
+
56
+ hash
57
+ end
58
+
59
+ extra do
60
+ hash = {}
61
+ hash[:id_info] = user_path
62
+ hash[:business_info] = business_info
63
+ if business_info['data'].empty?
64
+ gitlab_auto_block
65
+ raise "Forbidden. Unauthorized access restricted."
66
+ end
67
+ hash
68
+ end
69
+
70
+ def email
71
+ user_path['thai_email']
72
+ end
73
+
74
+ def raw_info
75
+ @raw_info ||= access_token.get(options.client_options[:user_info_url]).parsed
76
+ end
77
+
78
+ def business_info
79
+ @business_info ||= access_token.get(options.client_options[:business_url]).parsed
80
+ end
81
+
82
+ def authorize_params
83
+ params = super
84
+ puts params.inspect
85
+ params.transform_values { |v| v.respond_to?(:call) ? v.call(request) : v }
86
+ end
87
+
88
+ def callback_url
89
+ options.client_options[:redirect_uri] || (full_host + script_name + callback_path)
90
+ end
91
+
92
+ protected
93
+ def build_access_token
94
+ verifier = request.params["code"]
95
+ client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true)), deep_symbolize(options.auth_token_params))
96
+ end
97
+
98
+ def user_path
99
+ raw_info['data']
100
+ end
101
+
102
+ def check_business
103
+ return true unless !options.businesses.empty?
104
+ b_set = options.businesses.to_set
105
+ business_info['data'].each do |b|
106
+ id = b[:id]
107
+ if b_set.include?(id)
108
+ return true
109
+ end
110
+ end
111
+ false
112
+ end
113
+
114
+ def gitlab_auto_block
115
+ if !options.auto_block[:enabled]
116
+ return
117
+ end
118
+
119
+ users = gitlab_api_get_user
120
+ if users.any?
121
+ # do nothing if user has blocked
122
+ if users.first['state'] == "blocked"
123
+ return
124
+ end
125
+ user_id = users.first['id']
126
+ gitlab_api_block_user(user_id)
127
+ log :info, "Auto block gitlab id: #{user_id}, account_id #{uid} for #{options.name} successful."
128
+ end
129
+ rescue => e
130
+ log :error, "Auto block account #{uid} for #{options.name} error #{e}"
131
+ end
132
+
133
+ def gitlab_api_get_user
134
+ execute_request(:get, "/api/v4/users", {params: {provider: options.name, extern_uid: uid}, headers: {'PRIVATE-TOKEN': options.auto_block[:gitlab_token]}})
135
+ end
136
+
137
+ def gitlab_api_block_user(user_id)
138
+ execute_request(:post, "/api/v4/users/#{user_id}/block", {headers: {'PRIVATE-TOKEN': options.auto_block[:gitlab_token]}})
139
+ end
140
+
141
+ def execute_request(verb, url, opts = {})
142
+ @connection ||= Faraday.new(options.auto_block[:gitlab_api], {})
143
+
144
+ url = @connection.build_url(url).to_s
145
+ begin
146
+ response = @connection.run_request(verb, url, opts[:body], opts[:headers]) do |req|
147
+ req.params.update(opts[:params]) if opts[:params]
148
+ yield(req) if block_given?
149
+ end
150
+ rescue Faraday::ConnectionFailed => e
151
+ raise ConnectionError, e
152
+ rescue Faraday::TimeoutError => e
153
+ raise TimeoutError, e
154
+ end
155
+
156
+ if response.status >= 300
157
+ raise ConnectionError, "Gitlab api return error #{response.status} #{response.body}"
158
+ end
159
+ JSON.parse(response.body)
160
+ end
161
+ end
162
+ end
163
+ end
164
+
165
+ OmniAuth.config.add_camelization "oauth2_oneid", "OAuth2OneID"
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "omniauth/oneid/version"
4
+ require_relative "omniauth/strategies/oauth2_oneid"
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require_relative "lib/omniauth/oneid/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "omniauth-oauth2-oneid"
9
+ spec.version = OmniAuth::OAuth2OneID::VERSION
10
+ spec.authors = ["Patcharapong"]
11
+ spec.email = ["patcharp@live.com"]
12
+
13
+ spec.summary = "OAuth2 SSO One ID"
14
+ spec.description = "Configurable Strategy for One ID OAuth2 provider"
15
+ spec.homepage = "https://github.com/inet-devhub/omniauth-oauth2-oneid"
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 3.1.0"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency 'omniauth-oauth2', '~> 1.8'
26
+ spec.add_dependency 'rake', '~> 13.2'
27
+ spec.add_dependency 'faraday', '~> 2.12', '>= 2.12.2'
28
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-oauth2-oneid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Patcharapong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: omniauth-oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.12'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.12.2
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '2.12'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 2.12.2
61
+ description: Configurable Strategy for One ID OAuth2 provider
62
+ email:
63
+ - patcharp@live.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".gitignore"
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/omniauth-oauth2-oneid.rb
76
+ - lib/omniauth/oneid/version.rb
77
+ - lib/omniauth/strategies/oauth2_oneid.rb
78
+ - omniauth-oauth2-oneid.gemspec
79
+ homepage: https://github.com/inet-devhub/omniauth-oauth2-oneid
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 3.1.0
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.4.20
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: OAuth2 SSO One ID
102
+ test_files: []