forked from discourse/discourse-apple-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.rb
More file actions
50 lines (40 loc) · 1.53 KB
/
Copy pathplugin.rb
File metadata and controls
50 lines (40 loc) · 1.53 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
# frozen_string_literal: true
# name: discourse-apple-auth
# about: Enable login via "Sign-in with Apple"
# version: 1.0
# authors: Robert Barrow, David Taylor
# url: https://github.com/discourse/discourse-apple-auth
require_relative "lib/omniauth_apple"
register_svg_icon "fab-apple"
enabled_site_setting :sign_in_with_apple_enabled
register_asset "stylesheets/apple-auth.scss"
class AppleAuthenticator < ::Auth::ManagedAuthenticator
def name
'apple'
end
def enabled?
SiteSetting.sign_in_with_apple_enabled?
end
def fetch_jwks(options)
Discourse.cache.fetch("sign-in-with-apple-jwks", expires_in: 1.day) do
connection = Faraday.new { |c| c.use Faraday::Response::RaiseError }
JSON.parse(connection.get("https://appleid.apple.com/auth/keys").body, symbolize_names: true)
end
rescue Faraday::Error, JSON::ParserError => e
Rails.logger.error("Unable to fetch sign-in-with-apple-jwks #{e.class} #{e.message}")
nil
end
def register_middleware(omniauth)
omniauth.provider :apple,
setup: lambda { |env|
strategy = env["omniauth.strategy"]
strategy.options[:client_id] = SiteSetting.apple_client_id
strategy.options[:team_id] = SiteSetting.apple_team_id
strategy.options[:key_id] = SiteSetting.apple_key_id
strategy.options[:pem] = SiteSetting.apple_pem
strategy.options[:jwk_fetcher] = ->(options) { fetch_jwks(options) }
}
end
end
auth_provider icon: 'fab-apple',
authenticator: AppleAuthenticator.new