-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.rb
More file actions
79 lines (61 loc) · 1.96 KB
/
plugin.rb
File metadata and controls
79 lines (61 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# frozen_string_literal: true
# name: discourse-codeberg-auth
# about: Allows users to sign in with their Codeberg account
# version: 1.0.0
# authors: Regalijan
# url: https://github.com/Regalijan/discourse-codeberg-auth
enabled_site_setting :enable_codeberg_login
require 'base64'
require_relative 'lib/validators/CodebergLoginToggle'
register_svg_icon 'codeberg-auth-codeberg-logo' if respond_to?(:register_svg_icon)
class CodebergAuthenticator < Auth::ManagedAuthenticator
class CodebergStrategy < OmniAuth::Strategies::OAuth2
option :name, 'codeberg'
option :scope, 'openid profile email'
option :client_options,
site: 'https://codeberg.org/login/oauth/',
authorize_url: 'authorize',
token_url: 'access_token'
option :authorize_options, %i[scope]
uid { raw_info['sub'] }
info do
{
name: raw_info['name'],
email: raw_info['email_verified'] ? raw_info['email'] : nil,
image: raw_info['picture']
}
end
extra { { 'raw_info' => raw_info } }
def callback_url
full_host + script_name + callback_path
end
def name
'codeberg'
end
def raw_info
@raw_info ||= JSON.parse(
Base64.urlsafe_decode64(
access_token['id_token'].split('.')[1]
)
)
end
end
def enabled?
SiteSetting.enable_codeberg_login?
end
def name
'codeberg'
end
def primary_email_verified?(auth_token)
auth_token['extra']['raw_info']['email_verified']
end
def register_middleware(omniauth)
omniauth.provider CodebergStrategy,
setup: lambda { |env|
strategy = env['omniauth.strategy']
strategy.options[:client_id] = SiteSetting.codeberg_client_id
strategy.options[:client_secret] = SiteSetting.codeberg_secret
}
end
end
auth_provider authenticator: CodebergAuthenticator.new, icon: 'codeberg-auth-codeberg-logo'