Skip to content

Commit

Permalink
feat(oauth2): add Gitee Oauth2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
mashirozx committed Jul 7, 2022
1 parent bcfc33f commit 7bdc4fa
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .env.production.sample
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,9 @@ GITHUB_OAUTH2_SECRET=your_secret
GITLAB_OAUTH2_ENABLED=true
GITLAB_OAUTH2_KEY=your_key
GITLAB_OAUTH2_SECRET=your_secret

# Callback url: https://littlefo.rest/auth/auth/gitee/callback
# Scope: user_info emails
GITEE_OAUTH2_ENABLED=true
GITEE_OAUTH2_KEY=2d14987df6f93bc11053ffba34a66ad0b4e6bcfb91125ccc1cc8594c442b611e
GITEE_OAUTH2_SECRET=e81da4c92d419aca9186185dc88674b6f6a696ac85ab54cc87444d7dbd393842
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ gem 'net-ldap', '~> 0.17'
gem 'omniauth-cas', '~> 2.0'
gem 'omniauth-github', '~> 1.4'
gem 'omniauth-gitlab', '~> 1.0.2'
gem 'omniauth-gitee', '~> 1.0.0'
# gem 'omniauth-gitee', github: 'mashirozx/omniauth-gitee', branch: 'master'
gem 'omniauth-saml', '~> 1.10'
gem 'gitlab-omniauth-openid-connect', '~>0.9.1', require: 'omniauth_openid_connect'
gem 'omniauth', '~> 1.9'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@ GEM
addressable (~> 2.3)
nokogiri (~> 1.5)
omniauth (~> 1.2)
omniauth-gitee (1.0.0)
omniauth (>= 1.5, < 3.0)
omniauth-oauth2 (>= 1.4.0, < 2.0)
omniauth-github (1.4.0)
omniauth (~> 1.5)
omniauth-oauth2 (>= 1.4.0, < 2.0)
Expand Down Expand Up @@ -830,6 +833,7 @@ DEPENDENCIES
oj (~> 3.13)
omniauth (~> 1.9)
omniauth-cas (~> 2.0)
omniauth-gitee (~> 1.0.0)
omniauth-github (~> 1.4)
omniauth-gitlab (~> 1.0.2)
omniauth-rails_csrf_protection (~> 0.1)
Expand Down
15 changes: 8 additions & 7 deletions app/javascript/styles/customize/fontawesome-fix.scss
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// .fa- {
// &globe {
// &:before {
// content: "\f7a3";
// }
// }
// }
.fa- {
&gitee {
&:before {
font-family: mastodon-custom;
content: "\e60c";
}
}
}
12 changes: 12 additions & 0 deletions app/javascript/styles/mastodon/components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
&.button-secondary,
&.button-alternative-2,
&.button-github,
&.button-gitee,
&.button-gitlab {
font-size: 16px;
line-height: 36px;
Expand Down Expand Up @@ -157,6 +158,17 @@
}
}

&.button-gitee {
color: $white;
background: #c71d24;

&:active,
&:focus,
&:hover {
background-color: lighten(#c71d24, 4%);
}
}

&.button-gitlab {
color: $white;
background: #fc6d27;
Expand Down
18 changes: 10 additions & 8 deletions app/models/concerns/omniauthable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ def ensure_unique_username(auth)
auth_provided_username = nil
i = 0

if auth.provider == 'github'
if %w[github gitee].include?(auth.provider)
auth_provided_username = auth.info.nickname
elsif auth.provider == 'gitlab'
elsif %w[gitlab].include?(auth.provider)
auth_provided_username = auth.info.username
end

Expand All @@ -125,24 +125,26 @@ def ensure_valid_username(starting_username)
end

def trusted_auth_provider(auth)
auth.provider == 'github' || auth.provider == 'gitlab'
%w[github gitlab gitee].include?(auth.provider)
end

def trusted_auth_provider_display_name(auth)
if auth.provider == 'github'
if %w[github gitee].include?(auth.provider)
return auth.info.name
elsif auth.provider == 'gitlab'
elsif %w[gitlab].include?(auth.provider)
return auth.info.name
end

return nil
end

def trusted_auth_provider_email(auth)
if auth.provider == 'github'
return auth.info.email || auth.extra['all_emails'].select{ |email| email.primary? }.email
elsif auth.provider == 'gitlab'
if %w[github].include?(auth.provider)
return auth.info.email || auth.extra['all_emails'].select{ |email| email.primary? }[0]&.email
elsif %w[gitlab].include?(auth.provider)
return auth.info.email
elsif %w[gitee].include?(auth.provider)
return auth.info.email || auth.extra['all_emails'].select{ |email| email['scope'].include? 'primary' }[0]&.email
end

return auth.info.verified_email || auth.info.email
Expand Down
2 changes: 2 additions & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
- if ENV['MATOMO_ANALYTICS_HOST']
= javascript_pack_tag 'matomo', crossorigin: 'anonymous', 'data-matomo-host' => ENV['MATOMO_ANALYTICS_HOST'], 'data-matomo-site-id' => ENV['MATOMO_ANALYTICS_SITE_ID']

= stylesheet_link_tag '/plugins/icon-mastodon-custom/iconfont.css?t=1656835877240', skip_pipeline: true, media: 'all', id: 'icon-mastodon-custom-style'

- if user_signed_in?
- unless (not current_user.setting_enable_noto_serif) or current_user.setting_system_font_ui
- if ['zh-HK', 'zh-TW'].include? I18n.locale.to_s
Expand Down
5 changes: 5 additions & 0 deletions config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,9 @@
if ENV['GITLAB_OAUTH2_ENABLED'] == 'true'
config.omniauth :gitlab, ENV['GITLAB_OAUTH2_KEY'], ENV['GITLAB_OAUTH2_SECRET'], scope: 'read_user'
end

# Gitee strategy
if ENV['GITEE_OAUTH2_ENABLED'] == 'true'
config.omniauth :gitee, ENV['GITEE_OAUTH2_KEY'], ENV['GITEE_OAUTH2_SECRET'], scope: 'user_info emails'
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ en:
saml: SAML
github: GitHub
gitlab: GitLab
gitee: Gitee
register: Sign up
registration_closed: "%{instance} is not accepting new members"
resend_confirmation: Resend confirmation instructions
Expand Down
3 changes: 3 additions & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ zh-CN:
providers:
cas: CAS
saml: SAML
github: GitHub
gitlab: GitLab
gitee: 码云
register: 注册
registration_closed: "%{instance} 目前不接收新成员"
resend_confirmation: 重新发送确认邮件
Expand Down
38 changes: 38 additions & 0 deletions public/plugins/icon-mastodon-custom/iconfont.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@font-face {
font-family: "mastodon-custom"; /* Project id 1809229 */
src: url('iconfont.eot?t=1657188224111'); /* IE9 */
src: url('iconfont.eot?t=1657188224111#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAXcAAsAAAAADEAAAAWQAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHFQGYACDOAqKPIhhATYCJAMYCw4ABCAFhTsHehuTCsguIScbXkRDRUQCuxsAtDck44qX0U8Q9WOve4/CCJbYZmIRdYRpK6tQ1QNKRuH+ENuqUS9lO7itwI1OvCjAQbFQHL3Ij7qOJD66RZjcWwVicUKBUIkwIgoTOWgeGGZ8IHj/EwD+bUD4+HwXl6lfq934r5096OMj1NbSOsYyAQ52K7XiiyYewGBQAhRBiALugrWky09BAF+KqULd3tQxAPzWKWQHUQzBmCsQN8o6Z6TACN6KqeEK3gaHHmmQK9CgjtWPAODS/3z5BfJkBCisxjjP8DYawBXyv+7VbHmPfg1c6XBDqK0VYAEN5C/MLuv13kWaUJ3va33LyodQC8DbdCsYNO/zda9XiN+tZ+kS+ksHhWAxaBCH8WtQEFp/itift0CZzVPw1R+FwNcbKCx8vYnCwNdbVjRpdAP29wV5AKhm0ph/dPtoBIWKyQ8dWV5VZKSoeaQsW8gLekVB5g9KEjJ3QBTvNcln3gpgayXmd7oS2Lqg9T1ZpZLty1jA6Wq/fZXW/0R88gp1bSTgVFGykmGkjJSRMiKzB2PnX5x7Yd6hFw7r54kic3OPSthpsWrFDe2JTBxSbSvKqSRtHdGerFOJL77X57QngJhLnY/G1p71yhX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o6aNAkSSC2nLA5OLgQ5OLZZa+8y4gbKXTyM8tQ0wsJtrQD7cZq25IasppqntI7ytBnEKF4CU4e0Qxk6cyaW0hJHY7T9YQTx7ZETJVqMWHHihV3iEIR+FJrEcAJjhZNYzmrFXG4HFLIR7+CYcKebRxQ65XWybZRgcWUL0HPqs0H/MgsA') format('woff2'),
url('iconfont.woff?t=1657188224111') format('woff'),
url('iconfont.ttf?t=1657188224111') format('truetype'),
url('iconfont.svg?t=1657188224111#mastodon-custom') format('svg');
}

.mastodon-custom {
font-family: "mastodon-custom" !important;
font-size: 16px;
font-style: normal;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

.icon-gitee:before {
content: "\e60c";
}

.icon-at_circle_fill:before {
content: "\33";
}

.icon-super-topic:before {
content: "\e6a9";
}

.icon-quotation-marks-reverse:before {
content: "\e6a8";
}

.icon-quotation-marks:before {
content: "\e6a7";
}

Binary file added public/plugins/icon-mastodon-custom/iconfont.eot
Binary file not shown.
1 change: 1 addition & 0 deletions public/plugins/icon-mastodon-custom/iconfont.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions public/plugins/icon-mastodon-custom/iconfont.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"id": "1809229",
"name": "mastodon全局",
"font_family": "mastodon-custom",
"css_prefix_text": "icon-",
"description": "",
"glyphs": [
{
"icon_id": "26267572",
"name": "gitee",
"font_class": "gitee",
"unicode": "e60c",
"unicode_decimal": 58892
},
{
"icon_id": "12309605",
"name": "at_circle_fill",
"font_class": "at_circle_fill",
"unicode": "33",
"unicode_decimal": 51
},
{
"icon_id": "16355368",
"name": "微博超话",
"font_class": "super-topic",
"unicode": "e6a9",
"unicode_decimal": 59049
},
{
"icon_id": "14606901",
"name": "quotation-marks",
"font_class": "quotation-marks-reverse",
"unicode": "e6a8",
"unicode_decimal": 59048
},
{
"icon_id": "14606896",
"name": "quotation-marks",
"font_class": "quotation-marks",
"unicode": "e6a7",
"unicode_decimal": 59047
}
]
}
Loading

0 comments on commit 7bdc4fa

Please sign in to comment.