omniauth-feishu-jh 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 +7 -0
- data/CHANGELOG.md +10 -0
- data/LICENSE +21 -0
- data/README.md +77 -0
- data/lib/omniauth/feishu_jh/version.rb +7 -0
- data/lib/omniauth/strategies/feishu.rb +100 -0
- data/lib/omniauth-feishu-jh.rb +4 -0
- metadata +78 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 84aa16a984a8f3c30b03739f118ce06699e956e37dedf18c43ab16af922af686
|
|
4
|
+
data.tar.gz: e6f19b86a79387f19616d68982a4e665deaf82c62e2b2d3084ca31ceb45b6933
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f03123e678d8ba78ed1fc45bb9e87e60c0eb7c326391a6acfc1a3a489753be39373648187780f2ad3e4f2247d374594a386f976e811719ca81260758df70583a
|
|
7
|
+
data.tar.gz: a697cf874071fe42fe29459eb1dfc88e41e1b29e3b5d72a0d2a7f8592f8e67d55f1e6a7bbfd3f68edb705a15e27606c6a471ca1c1b8fb7d083da485e55863741
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## 0.1.0 (2026-09-14)
|
|
6
|
+
|
|
7
|
+
- Extract the Feishu OmniAuth strategy from the JiHu GitLab monolith into a standalone gem named `omniauth-feishu-jh`.
|
|
8
|
+
- Add standalone tests, linting, gem packaging, and contributor documentation.
|
|
9
|
+
|
|
10
|
+
The strategy was introduced in [MR 3841](https://jihulab.com/gitlab-cn/gitlab/-/merge_requests/3841) for [work item 5317](https://jihulab.com/gitlab-cn/gitlab/-/work_items/5317).
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 JiHu GitLab contributors
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# omniauth-feishu-jh
|
|
2
|
+
|
|
3
|
+
`omniauth-feishu-jh` is an [OmniAuth](https://github.com/omniauth/omniauth) OAuth 2.0 strategy for signing in with a Feishu enterprise (self-built) application. JiHu GitLab contributors maintain it for the JiHu GitLab distribution.
|
|
4
|
+
|
|
5
|
+
The gem is named `omniauth-feishu-jh` because `omniauth-feishu` on RubyGems is an unrelated project. Both gems define `OmniAuth::Strategies::Feishu`, so do not install them in the same bundle.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add the gem to your `Gemfile`:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'omniauth-feishu-jh', '~> 0.1.0'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Then run `bundle install`.
|
|
16
|
+
|
|
17
|
+
The gem supports Ruby 3.2 or later and OmniAuth 2.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
use OmniAuth::Builder do
|
|
23
|
+
provider :feishu,
|
|
24
|
+
ENV.fetch('FEISHU_APP_ID'),
|
|
25
|
+
ENV.fetch('FEISHU_APP_SECRET'),
|
|
26
|
+
tenant_key: ENV.fetch('FEISHU_TENANT_KEY')
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The `tenant_key` option is required. The strategy rejects a callback when Feishu returns a different `tenant_key`, or when the configured tenant, application, or user identifiers contain characters other than letters, digits, `_`, and `-`.
|
|
31
|
+
|
|
32
|
+
The default endpoints are:
|
|
33
|
+
|
|
34
|
+
| Option | Default |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `client_options.site` | `https://open.feishu.cn` |
|
|
37
|
+
| `client_options.authorize_url` | `https://accounts.feishu.cn/open-apis/authen/v1/authorize` |
|
|
38
|
+
| `client_options.token_url` | `https://accounts.feishu.cn/oauth/v3/token` |
|
|
39
|
+
|
|
40
|
+
Override all three `client_options` values when a deployment uses another Feishu-compatible region.
|
|
41
|
+
|
|
42
|
+
The host application must follow OmniAuth 2's integration requirements: start authentication with `POST`, preserve the session from the request phase through the callback, and keep request validation and CSRF protection enabled.
|
|
43
|
+
|
|
44
|
+
PKCE is disabled because the Feishu token endpoint currently rejects valid S256 code verifiers with error `20049` ([larksuite/oapi-sdk-go#230](https://github.com/larksuite/oapi-sdk-go/issues/230)). The OAuth `state` check still protects the callback against CSRF.
|
|
45
|
+
|
|
46
|
+
## Authentication data
|
|
47
|
+
|
|
48
|
+
The OmniAuth UID has this format:
|
|
49
|
+
|
|
50
|
+
```plaintext
|
|
51
|
+
<tenant_key>:<app_id>:<open_id>
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
The `info` hash contains only `name` and `nickname`. The nickname is the Feishu `open_id`. The strategy does not return an email address because Feishu documents email and mobile values as administrator-imported contact data that has not been verified with the user in real time.
|
|
55
|
+
|
|
56
|
+
The `credentials` hash is empty. The strategy uses the user access token only to request the current user profile during the callback. It does not expose the token through the OmniAuth authentication hash.
|
|
57
|
+
|
|
58
|
+
Failures are reported as `csrf_detected`, `access_denied`, or `invalid_credentials`, with a generic message so that provider responses do not leak into logs.
|
|
59
|
+
|
|
60
|
+
## Host integration hooks
|
|
61
|
+
|
|
62
|
+
The strategy provides protected hooks so a host application can use its own HTTP client and JSON parser:
|
|
63
|
+
|
|
64
|
+
- `request_token(params)` returns `[status, response_body]` for the OAuth token request.
|
|
65
|
+
- `request_user_info` returns `[status, response_body]` for the current user profile request.
|
|
66
|
+
- `parse_json(raw)` parses a response body and returns the decoded object.
|
|
67
|
+
- `parse_response(status, raw)` checks the HTTP status and Feishu response code, then uses `parse_json`.
|
|
68
|
+
|
|
69
|
+
A host can override these methods in a subclass or prepend a module to the strategy. Overrides must keep the method signatures and return values described above. The default implementation uses the HTTP connection from `omniauth-oauth2` and Ruby `JSON`.
|
|
70
|
+
|
|
71
|
+
The GitLab-specific overrides (`Gitlab::HTTP`, `Gitlab::Json::SafeParser`, and disabling sign-in on GitLab.com) remain in the JiHu GitLab monolith and are not part of this gem.
|
|
72
|
+
|
|
73
|
+
## Development
|
|
74
|
+
|
|
75
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md). This project is licensed under the MIT License; see [LICENSE](LICENSE).
|
|
76
|
+
|
|
77
|
+
The strategy was introduced in [MR 3841](https://jihulab.com/gitlab-cn/gitlab/-/merge_requests/3841) for [work item 5317](https://jihulab.com/gitlab-cn/gitlab/-/work_items/5317).
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'omniauth-oauth2'
|
|
4
|
+
require 'json'
|
|
5
|
+
|
|
6
|
+
module OmniAuth
|
|
7
|
+
module Strategies
|
|
8
|
+
class Feishu < OAuth2
|
|
9
|
+
API_SITE = 'https://open.feishu.cn'
|
|
10
|
+
AUTHORIZE_URL = 'https://accounts.feishu.cn/open-apis/authen/v1/authorize'
|
|
11
|
+
TOKEN_URL = 'https://accounts.feishu.cn/oauth/v3/token'
|
|
12
|
+
USER_INFO_PATH = '/open-apis/authen/v1/user_info'
|
|
13
|
+
|
|
14
|
+
option :name, 'feishu'
|
|
15
|
+
option :tenant_key, nil
|
|
16
|
+
# Feishu accepts code_challenge but rejects every S256 token exchange with 20049, so PKCE stays off
|
|
17
|
+
# until upstream fixes it. This drops code-injection protection only; the state check still stops CSRF.
|
|
18
|
+
# https://github.com/larksuite/oapi-sdk-go/issues/230
|
|
19
|
+
option :pkce, false
|
|
20
|
+
option :client_options, site: API_SITE, authorize_url: AUTHORIZE_URL, token_url: TOKEN_URL
|
|
21
|
+
|
|
22
|
+
uid { [raw_info.fetch('tenant_key'), options.client_id, raw_info.fetch('open_id')].join(':') }
|
|
23
|
+
info { { name: raw_info['name'], nickname: raw_info['open_id'] } }
|
|
24
|
+
|
|
25
|
+
def credentials
|
|
26
|
+
{}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def raw_info
|
|
30
|
+
@raw_info ||= begin
|
|
31
|
+
body = parse_response(*request_user_info)
|
|
32
|
+
data = body['data']
|
|
33
|
+
unless data.is_a?(Hash) && identifier?(data['open_id']) && identifier?(options.client_id) &&
|
|
34
|
+
identifier?(options.tenant_key) && data['tenant_key'] == options.tenant_key
|
|
35
|
+
raise CallbackError.new(:invalid_credentials, 'Feishu identity does not match the configured tenant')
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
data.slice('name', 'open_id', 'tenant_key')
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def callback_url
|
|
43
|
+
full_host + script_name + callback_path
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def fail!(message_key, _exception = nil)
|
|
47
|
+
key = %i[csrf_detected access_denied].include?(message_key.to_sym) ? message_key.to_sym : :invalid_credentials
|
|
48
|
+
super(key, CallbackError.new(key, 'Feishu authentication failed'))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
protected
|
|
52
|
+
|
|
53
|
+
def build_access_token
|
|
54
|
+
params = token_params.to_hash.merge(
|
|
55
|
+
'grant_type' => 'authorization_code', 'client_id' => options.client_id,
|
|
56
|
+
'client_secret' => options.client_secret, 'code' => request.params['code'], 'redirect_uri' => callback_url
|
|
57
|
+
)
|
|
58
|
+
body = parse_response(*request_token(params))
|
|
59
|
+
unless body['access_token'].is_a?(String) && !body['access_token'].empty? &&
|
|
60
|
+
body['expires_in'].is_a?(Integer) && body['expires_in'] > 0
|
|
61
|
+
raise CallbackError.new(:invalid_credentials, 'Feishu returned an invalid token')
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
::OAuth2::AccessToken.new(client, body['access_token'], expires_in: body['expires_in'])
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def request_token(params)
|
|
68
|
+
response = client.connection.post(client.token_url, URI.encode_www_form(params),
|
|
69
|
+
'Content-Type' => 'application/x-www-form-urlencoded')
|
|
70
|
+
[response.status, response.body]
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def request_user_info
|
|
74
|
+
response = client.connection.get(USER_INFO_PATH, nil, 'Authorization' => "Bearer #{access_token.token}")
|
|
75
|
+
[response.status, response.body]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def parse_json(raw)
|
|
79
|
+
JSON.parse(raw)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def parse_response(status, raw)
|
|
83
|
+
raise CallbackError.new(:invalid_credentials, 'Feishu request failed') unless status == 200
|
|
84
|
+
|
|
85
|
+
body = parse_json(raw)
|
|
86
|
+
unless body.is_a?(Hash) && body['code'].is_a?(Integer) && body['code'] == 0
|
|
87
|
+
raise CallbackError.new(:invalid_credentials, 'Feishu rejected the request')
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
body
|
|
91
|
+
rescue JSON::ParserError
|
|
92
|
+
raise CallbackError.new(:invalid_credentials, 'Feishu returned an invalid response')
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def identifier?(value)
|
|
96
|
+
value.is_a?(String) && value.match?(/\A[A-Za-z0-9_-]+\z/)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: omniauth-feishu-jh
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- JiHu GitLab contributors
|
|
8
|
+
bindir: bin
|
|
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: OmniAuth OAuth 2.0 strategy for signing in with a Feishu enterprise (self-built)
|
|
41
|
+
application.
|
|
42
|
+
executables: []
|
|
43
|
+
extensions: []
|
|
44
|
+
extra_rdoc_files: []
|
|
45
|
+
files:
|
|
46
|
+
- CHANGELOG.md
|
|
47
|
+
- LICENSE
|
|
48
|
+
- README.md
|
|
49
|
+
- lib/omniauth-feishu-jh.rb
|
|
50
|
+
- lib/omniauth/feishu_jh/version.rb
|
|
51
|
+
- lib/omniauth/strategies/feishu.rb
|
|
52
|
+
homepage: https://gitlab.com/gitlab-jh/jh-team/omniauth-feishu-jh
|
|
53
|
+
licenses:
|
|
54
|
+
- MIT
|
|
55
|
+
metadata:
|
|
56
|
+
source_code_uri: https://gitlab.com/gitlab-jh/jh-team/omniauth-feishu-jh
|
|
57
|
+
bug_tracker_uri: https://gitlab.com/gitlab-jh/jh-team/omniauth-feishu-jh/-/issues
|
|
58
|
+
changelog_uri: https://gitlab.com/gitlab-jh/jh-team/omniauth-feishu-jh/-/blob/main/CHANGELOG.md
|
|
59
|
+
allowed_push_host: https://rubygems.org
|
|
60
|
+
rubygems_mfa_required: 'true'
|
|
61
|
+
rdoc_options: []
|
|
62
|
+
require_paths:
|
|
63
|
+
- lib
|
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.2'
|
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '0'
|
|
74
|
+
requirements: []
|
|
75
|
+
rubygems_version: 4.0.13
|
|
76
|
+
specification_version: 4
|
|
77
|
+
summary: OmniAuth strategy for Feishu enterprise applications
|
|
78
|
+
test_files: []
|