better_auth 0.10.0 → 0.11.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 +4 -4
- data/CHANGELOG.md +83 -0
- data/LICENSE.md +1 -1
- data/README.md +138 -91
- data/lib/better_auth/adapters/base.rb +64 -1
- data/lib/better_auth/adapters/internal_adapter.rb +197 -11
- data/lib/better_auth/adapters/join_support.rb +10 -0
- data/lib/better_auth/adapters/memory.rb +191 -56
- data/lib/better_auth/adapters/mssql.rb +7 -3
- data/lib/better_auth/adapters/sql.rb +294 -49
- data/lib/better_auth/api.rb +3 -24
- data/lib/better_auth/configuration.rb +41 -46
- data/lib/better_auth/context.rb +43 -67
- data/lib/better_auth/cookies.rb +110 -14
- data/lib/better_auth/core.rb +0 -2
- data/lib/better_auth/crypto.rb +4 -3
- data/lib/better_auth/doctor.rb +14 -3
- data/lib/better_auth/error.rb +3 -1
- data/lib/better_auth/logger.rb +1 -1
- data/lib/better_auth/middleware/origin_check.rb +9 -1
- data/lib/better_auth/migration_plan.rb +4 -4
- data/lib/better_auth/oauth_state.rb +147 -0
- data/lib/better_auth/plugin_loader.rb +220 -0
- data/lib/better_auth/plugins/access.rb +10 -13
- data/lib/better_auth/plugins/admin.rb +79 -28
- data/lib/better_auth/plugins/api_key.rb +4 -7
- data/lib/better_auth/plugins/bearer.rb +8 -22
- data/lib/better_auth/plugins/captcha.rb +24 -6
- data/lib/better_auth/plugins/device_authorization.rb +47 -9
- data/lib/better_auth/plugins/dub.rb +2 -2
- data/lib/better_auth/plugins/email_otp.rb +30 -47
- data/lib/better_auth/plugins/generic_oauth.rb +141 -146
- data/lib/better_auth/plugins/i18n.rb +215 -0
- data/lib/better_auth/plugins/jwt.rb +37 -19
- data/lib/better_auth/plugins/magic_link.rb +18 -21
- data/lib/better_auth/plugins/oauth_popup.rb +330 -0
- data/lib/better_auth/plugins/oauth_protocol.rb +142 -32
- data/lib/better_auth/plugins/oauth_provider.rb +4 -7
- data/lib/better_auth/plugins/oauth_proxy.rb +1 -1
- data/lib/better_auth/plugins/one_tap.rb +61 -60
- data/lib/better_auth/plugins/one_time_token.rb +1 -4
- data/lib/better_auth/plugins/open_api.rb +35 -7
- data/lib/better_auth/plugins/organization.rb +455 -122
- data/lib/better_auth/plugins/passkey.rb +4 -7
- data/lib/better_auth/plugins/phone_number.rb +24 -26
- data/lib/better_auth/plugins/scim.rb +4 -7
- data/lib/better_auth/plugins/siwe.rb +126 -16
- data/lib/better_auth/plugins/sso.rb +4 -7
- data/lib/better_auth/plugins/stripe.rb +4 -7
- data/lib/better_auth/plugins/two_factor.rb +186 -21
- data/lib/better_auth/plugins/username.rb +56 -13
- data/lib/better_auth/plugins.rb +57 -0
- data/lib/better_auth/rate_limiter.rb +297 -135
- data/lib/better_auth/request_ip.rb +123 -13
- data/lib/better_auth/router.rb +3 -3
- data/lib/better_auth/routes/account.rb +40 -8
- data/lib/better_auth/routes/email_verification.rb +88 -5
- data/lib/better_auth/routes/password.rb +5 -6
- data/lib/better_auth/routes/session.rb +7 -2
- data/lib/better_auth/routes/sign_in.rb +10 -1
- data/lib/better_auth/routes/sign_up.rb +45 -22
- data/lib/better_auth/routes/social.rb +169 -54
- data/lib/better_auth/routes/user.rb +11 -8
- data/lib/better_auth/schema/sql.rb +90 -24
- data/lib/better_auth/schema.rb +124 -3
- data/lib/better_auth/session.rb +27 -9
- data/lib/better_auth/session_store.rb +1 -1
- data/lib/better_auth/social_providers/apple.rb +6 -1
- data/lib/better_auth/social_providers/atlassian.rb +1 -0
- data/lib/better_auth/social_providers/base.rb +41 -14
- data/lib/better_auth/social_providers/cognito.rb +75 -5
- data/lib/better_auth/social_providers/facebook.rb +95 -1
- data/lib/better_auth/social_providers/figma.rb +2 -0
- data/lib/better_auth/social_providers/google.rb +12 -1
- data/lib/better_auth/social_providers/line.rb +35 -1
- data/lib/better_auth/social_providers/linear.rb +1 -0
- data/lib/better_auth/social_providers/microsoft_entra_id.rb +9 -0
- data/lib/better_auth/social_providers/naver.rb +24 -1
- data/lib/better_auth/social_providers/notion.rb +1 -0
- data/lib/better_auth/social_providers/paybin.rb +25 -1
- data/lib/better_auth/social_providers/paypal.rb +118 -2
- data/lib/better_auth/social_providers/reddit.rb +25 -4
- data/lib/better_auth/social_providers/salesforce.rb +1 -0
- data/lib/better_auth/social_providers/twitch.rb +24 -1
- data/lib/better_auth/social_providers/twitter.rb +36 -1
- data/lib/better_auth/social_providers/vk.rb +34 -1
- data/lib/better_auth/social_providers/wechat.rb +6 -4
- data/lib/better_auth/sql_migration.rb +5 -5
- data/lib/better_auth/version.rb +1 -1
- data/lib/better_auth.rb +11 -41
- metadata +5 -13
- data/lib/better_auth/plugins/mcp/authorization.rb +0 -111
- data/lib/better_auth/plugins/mcp/config.rb +0 -51
- data/lib/better_auth/plugins/mcp/consent.rb +0 -31
- data/lib/better_auth/plugins/mcp/legacy_aliases.rb +0 -43
- data/lib/better_auth/plugins/mcp/metadata.rb +0 -81
- data/lib/better_auth/plugins/mcp/registration.rb +0 -31
- data/lib/better_auth/plugins/mcp/resource_handler.rb +0 -37
- data/lib/better_auth/plugins/mcp/schema.rb +0 -91
- data/lib/better_auth/plugins/mcp/token.rb +0 -108
- data/lib/better_auth/plugins/mcp/userinfo.rb +0 -37
- data/lib/better_auth/plugins/mcp.rb +0 -256
- data/lib/better_auth/plugins/oidc_provider.rb +0 -769
|
@@ -5,6 +5,7 @@ module BetterAuth
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
7
|
def line(client_id:, client_secret:, scopes: ["openid", "profile", "email"], **options)
|
|
8
|
+
normalized = Base.normalize_options(options)
|
|
8
9
|
provider = Base.oauth_provider(
|
|
9
10
|
id: "line",
|
|
10
11
|
name: "LINE",
|
|
@@ -26,7 +27,40 @@ module BetterAuth
|
|
|
26
27
|
},
|
|
27
28
|
**options
|
|
28
29
|
)
|
|
29
|
-
provider
|
|
30
|
+
provider[:verify_id_token] ||= lambda do |token, nonce = nil|
|
|
31
|
+
return false if normalized[:disable_id_token_sign_in]
|
|
32
|
+
|
|
33
|
+
profile = Base.post_form_json(
|
|
34
|
+
normalized[:verify_id_token_endpoint] || "https://api.line.me/oauth2/v2.1/verify",
|
|
35
|
+
{id_token: token, client_id: client_id, nonce: nonce}
|
|
36
|
+
)
|
|
37
|
+
next false unless profile
|
|
38
|
+
next false unless profile["aud"] == client_id
|
|
39
|
+
next false if profile["nonce"] && profile["nonce"] != nonce
|
|
40
|
+
|
|
41
|
+
true
|
|
42
|
+
end
|
|
43
|
+
provider[:get_user_info] = lambda do |tokens|
|
|
44
|
+
custom = normalized[:get_user_info]
|
|
45
|
+
next custom.call(tokens) if custom
|
|
46
|
+
|
|
47
|
+
profile = Base.id_token(tokens) ? Base.decode_jwt_payload(Base.id_token(tokens)) : {}
|
|
48
|
+
profile = Base.fetch_user_info("https://api.line.me/oauth2/v2.1/userinfo", tokens) if profile.empty?
|
|
49
|
+
next nil unless profile && !profile.empty?
|
|
50
|
+
|
|
51
|
+
user = Base.apply_profile_mapping(
|
|
52
|
+
{
|
|
53
|
+
id: profile["sub"] || profile["userId"],
|
|
54
|
+
name: profile["name"] || profile["displayName"] || "",
|
|
55
|
+
email: profile["email"],
|
|
56
|
+
image: profile["picture"] || profile["pictureUrl"],
|
|
57
|
+
emailVerified: false
|
|
58
|
+
},
|
|
59
|
+
profile,
|
|
60
|
+
normalized
|
|
61
|
+
)
|
|
62
|
+
{user: user, data: profile}
|
|
63
|
+
end
|
|
30
64
|
provider
|
|
31
65
|
end
|
|
32
66
|
end
|
|
@@ -33,6 +33,7 @@ module BetterAuth
|
|
|
33
33
|
"Authorization" => "Bearer #{Base.access_token(tokens)}"
|
|
34
34
|
)
|
|
35
35
|
return profile if Base.provider_user_info?(profile)
|
|
36
|
+
return nil unless profile
|
|
36
37
|
|
|
37
38
|
mapped = provider[:options][:map_profile_to_user]&.call(profile) || {}
|
|
38
39
|
viewer = profile.dig("data", "viewer") || {}
|
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
module BetterAuth
|
|
4
4
|
module SocialProviders
|
|
5
|
+
MICROSOFT_CONSUMER_TENANT_ID = "9188040d-6c67-4c5b-b112-36a304b66dad"
|
|
6
|
+
private_constant :MICROSOFT_CONSUMER_TENANT_ID
|
|
7
|
+
|
|
5
8
|
module_function
|
|
6
9
|
|
|
7
10
|
def microsoft_entra_id(client_id:, client_secret:, tenant_id: "common", scopes: ["openid", "profile", "email", "User.Read", "offline_access"], **options)
|
|
@@ -81,6 +84,12 @@ module BetterAuth
|
|
|
81
84
|
nonce: nonce
|
|
82
85
|
)
|
|
83
86
|
|
|
87
|
+
tid = profile&.fetch("tid", nil)
|
|
88
|
+
next false unless tid.is_a?(String)
|
|
89
|
+
next false unless profile["iss"] == "#{authority.to_s.sub(%r{/+\z}, "")}/#{tid}/v2.0"
|
|
90
|
+
next false if tenant_id.to_s == "organizations" && tid == MICROSOFT_CONSUMER_TENANT_ID
|
|
91
|
+
next false if tenant_id.to_s == "consumers" && tid != MICROSOFT_CONSUMER_TENANT_ID
|
|
92
|
+
|
|
84
93
|
!!(profile&.fetch("sub", nil) || profile&.fetch("oid", nil))
|
|
85
94
|
end,
|
|
86
95
|
get_user_info: lambda do |tokens|
|
|
@@ -5,7 +5,8 @@ module BetterAuth
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
7
|
def naver(client_id:, client_secret:, scopes: ["profile", "email"], **options)
|
|
8
|
-
Base.
|
|
8
|
+
normalized = Base.normalize_options(options)
|
|
9
|
+
provider = Base.oauth_provider(
|
|
9
10
|
id: "naver",
|
|
10
11
|
name: "Naver",
|
|
11
12
|
client_id: client_id,
|
|
@@ -26,6 +27,28 @@ module BetterAuth
|
|
|
26
27
|
},
|
|
27
28
|
**options
|
|
28
29
|
)
|
|
30
|
+
provider[:get_user_info] = lambda do |tokens|
|
|
31
|
+
custom = normalized[:get_user_info]
|
|
32
|
+
next custom.call(tokens) if custom
|
|
33
|
+
|
|
34
|
+
profile = Base.fetch_user_info("https://openapi.naver.com/v1/nid/me", tokens)
|
|
35
|
+
next nil unless profile && profile["resultcode"] == "00"
|
|
36
|
+
|
|
37
|
+
data = profile["response"] || {}
|
|
38
|
+
user = Base.apply_profile_mapping(
|
|
39
|
+
{
|
|
40
|
+
id: data["id"],
|
|
41
|
+
name: data["name"] || data["nickname"] || "",
|
|
42
|
+
email: data["email"],
|
|
43
|
+
image: data["profile_image"],
|
|
44
|
+
emailVerified: false
|
|
45
|
+
},
|
|
46
|
+
profile,
|
|
47
|
+
normalized
|
|
48
|
+
)
|
|
49
|
+
{user: user, data: profile}
|
|
50
|
+
end
|
|
51
|
+
provider
|
|
29
52
|
end
|
|
30
53
|
end
|
|
31
54
|
end
|
|
@@ -15,6 +15,7 @@ module BetterAuth
|
|
|
15
15
|
user_info_endpoint: "https://api.notion.com/v1/users/me",
|
|
16
16
|
scopes: scopes,
|
|
17
17
|
auth_params: {owner: "user"},
|
|
18
|
+
token_authentication: :basic,
|
|
18
19
|
user_info_headers: {"Notion-Version" => "2022-06-28"},
|
|
19
20
|
profile_map: ->(profile) {
|
|
20
21
|
user = profile.dig("bot", "owner", "user") || profile
|
|
@@ -5,8 +5,9 @@ module BetterAuth
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
7
|
def paybin(client_id:, client_secret:, scopes: ["openid", "email", "profile"], **options)
|
|
8
|
+
normalized = Base.normalize_options(options)
|
|
8
9
|
issuer = (options[:issuer] || "https://idp.paybin.io").to_s.sub(%r{/+\z}, "")
|
|
9
|
-
Base.oauth_provider(
|
|
10
|
+
provider = Base.oauth_provider(
|
|
10
11
|
id: "paybin",
|
|
11
12
|
name: "Paybin",
|
|
12
13
|
client_id: client_id,
|
|
@@ -15,6 +16,7 @@ module BetterAuth
|
|
|
15
16
|
token_endpoint: "#{issuer}/oauth2/token",
|
|
16
17
|
scopes: scopes,
|
|
17
18
|
pkce: true,
|
|
19
|
+
require_code_verifier: true,
|
|
18
20
|
profile_map: ->(profile) {
|
|
19
21
|
{
|
|
20
22
|
id: profile["sub"],
|
|
@@ -26,6 +28,28 @@ module BetterAuth
|
|
|
26
28
|
},
|
|
27
29
|
**options
|
|
28
30
|
)
|
|
31
|
+
provider[:get_user_info] = lambda do |tokens|
|
|
32
|
+
custom = normalized[:get_user_info]
|
|
33
|
+
next custom.call(tokens) if custom
|
|
34
|
+
next nil unless Base.id_token(tokens)
|
|
35
|
+
|
|
36
|
+
profile = Base.decode_jwt_payload(Base.id_token(tokens))
|
|
37
|
+
next nil if profile.empty?
|
|
38
|
+
|
|
39
|
+
user = Base.apply_profile_mapping(
|
|
40
|
+
{
|
|
41
|
+
id: profile["sub"],
|
|
42
|
+
name: profile["name"] || profile["preferred_username"] || "",
|
|
43
|
+
email: profile["email"],
|
|
44
|
+
image: profile["picture"],
|
|
45
|
+
emailVerified: !!profile["email_verified"]
|
|
46
|
+
},
|
|
47
|
+
profile,
|
|
48
|
+
normalized
|
|
49
|
+
)
|
|
50
|
+
{user: user, data: profile}
|
|
51
|
+
end
|
|
52
|
+
provider
|
|
29
53
|
end
|
|
30
54
|
end
|
|
31
55
|
end
|
|
@@ -5,19 +5,24 @@ module BetterAuth
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
7
|
def paypal(client_id:, client_secret:, scopes: [], **options)
|
|
8
|
+
normalized = Base.normalize_options(options)
|
|
8
9
|
sandbox = (options[:environment] || "sandbox").to_s == "sandbox"
|
|
9
10
|
auth_host = sandbox ? "https://www.sandbox.paypal.com" : "https://www.paypal.com"
|
|
10
11
|
api_host = sandbox ? "https://api-m.sandbox.paypal.com" : "https://api-m.paypal.com"
|
|
12
|
+
token_endpoint = normalized[:token_endpoint] || "#{api_host}/v1/oauth2/token"
|
|
13
|
+
issuer = sandbox ? "https://www.sandbox.paypal.com" : "https://www.paypal.com"
|
|
14
|
+
jwks_endpoint = normalized[:jwks_endpoint] || (sandbox ? "https://api.sandbox.paypal.com/v1/oauth2/certs" : "https://api.paypal.com/v1/oauth2/certs")
|
|
11
15
|
provider = Base.oauth_provider(
|
|
12
16
|
id: "paypal",
|
|
13
17
|
name: "PayPal",
|
|
14
18
|
client_id: client_id,
|
|
15
19
|
client_secret: client_secret,
|
|
16
20
|
authorization_endpoint: "#{auth_host}/signin/authorize",
|
|
17
|
-
token_endpoint:
|
|
21
|
+
token_endpoint: token_endpoint,
|
|
18
22
|
user_info_endpoint: "#{api_host}/v1/identity/oauth2/userinfo?schema=paypalv1.1",
|
|
19
23
|
scopes: scopes,
|
|
20
24
|
pkce: true,
|
|
25
|
+
token_authentication: :basic,
|
|
21
26
|
profile_map: ->(profile) {
|
|
22
27
|
{
|
|
23
28
|
id: profile["user_id"],
|
|
@@ -29,7 +34,118 @@ module BetterAuth
|
|
|
29
34
|
},
|
|
30
35
|
**options
|
|
31
36
|
)
|
|
32
|
-
provider
|
|
37
|
+
provider[:validate_authorization_code] = lambda do |data|
|
|
38
|
+
Base.normalize_tokens(Base.post_token_form(
|
|
39
|
+
token_endpoint,
|
|
40
|
+
{
|
|
41
|
+
code: data[:code] || data["code"],
|
|
42
|
+
grant_type: "authorization_code",
|
|
43
|
+
redirect_uri: data[:redirect_uri] || data[:redirectURI] || data["redirect_uri"] || data["redirectURI"]
|
|
44
|
+
},
|
|
45
|
+
client_id: Base.primary_client_id(client_id),
|
|
46
|
+
client_secret: client_secret,
|
|
47
|
+
authentication: :basic,
|
|
48
|
+
headers: {"Accept-Language" => "en_US"}
|
|
49
|
+
))
|
|
50
|
+
end
|
|
51
|
+
provider[:refresh_access_token] = lambda do |refresh_token|
|
|
52
|
+
Base.normalize_tokens(Base.post_token_form(
|
|
53
|
+
token_endpoint,
|
|
54
|
+
{
|
|
55
|
+
grant_type: "refresh_token",
|
|
56
|
+
refresh_token: refresh_token
|
|
57
|
+
},
|
|
58
|
+
client_id: Base.primary_client_id(client_id),
|
|
59
|
+
client_secret: client_secret,
|
|
60
|
+
authentication: :basic,
|
|
61
|
+
headers: {"Accept-Language" => "en_US"}
|
|
62
|
+
))
|
|
63
|
+
end
|
|
64
|
+
provider[:verify_id_token] = lambda do |token, nonce = nil|
|
|
65
|
+
return false if normalized[:disable_id_token_sign_in]
|
|
66
|
+
|
|
67
|
+
custom = normalized[:verify_id_token]
|
|
68
|
+
next custom.call(token, nonce) if custom
|
|
69
|
+
|
|
70
|
+
begin
|
|
71
|
+
encoded_header, _encoded_payload, signature = token.to_s.split(".", 3)
|
|
72
|
+
next false if encoded_header.to_s.empty? || signature.to_s.empty?
|
|
73
|
+
|
|
74
|
+
header = JSON.parse(Base64.urlsafe_decode64(Base.padded_base64(encoded_header)))
|
|
75
|
+
algorithm = header["alg"]
|
|
76
|
+
profile = case algorithm
|
|
77
|
+
when "RS256"
|
|
78
|
+
Base.verify_jwt_with_jwks(
|
|
79
|
+
token,
|
|
80
|
+
jwks: normalized[:jwks],
|
|
81
|
+
jwks_endpoint: jwks_endpoint,
|
|
82
|
+
algorithms: ["RS256"],
|
|
83
|
+
issuers: issuer,
|
|
84
|
+
audience: Array(client_id),
|
|
85
|
+
nonce: nonce,
|
|
86
|
+
max_age: 3600
|
|
87
|
+
)
|
|
88
|
+
when "HS256"
|
|
89
|
+
next false if client_secret.to_s.empty?
|
|
90
|
+
|
|
91
|
+
claims, = JWT.decode(token.to_s, client_secret, true, {
|
|
92
|
+
algorithm: "HS256",
|
|
93
|
+
aud: Array(client_id),
|
|
94
|
+
verify_aud: true,
|
|
95
|
+
iss: issuer,
|
|
96
|
+
verify_iss: true
|
|
97
|
+
})
|
|
98
|
+
next false if nonce && claims["nonce"] != nonce
|
|
99
|
+
|
|
100
|
+
claims
|
|
101
|
+
else
|
|
102
|
+
next false
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
issued_at = Integer(profile.fetch("iat"))
|
|
106
|
+
token_age = Time.now.to_i - issued_at
|
|
107
|
+
next false if token_age.negative? || token_age > 3600
|
|
108
|
+
|
|
109
|
+
!!profile.fetch("sub", nil)
|
|
110
|
+
rescue
|
|
111
|
+
false
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
provider[:get_user_info] = lambda do |tokens|
|
|
115
|
+
custom = normalized[:get_user_info]
|
|
116
|
+
next custom.call(tokens) if custom
|
|
117
|
+
|
|
118
|
+
begin
|
|
119
|
+
access_token = Base.access_token(tokens)
|
|
120
|
+
next nil if access_token.to_s.empty?
|
|
121
|
+
|
|
122
|
+
profile = Base.fetch_user_info("#{api_host}/v1/identity/oauth2/userinfo?schema=paypalv1.1", tokens)
|
|
123
|
+
next nil unless profile.is_a?(Hash)
|
|
124
|
+
next nil if profile["user_id"].to_s.empty?
|
|
125
|
+
|
|
126
|
+
id_token = Base.id_token(tokens)
|
|
127
|
+
if id_token
|
|
128
|
+
id_token_subject = Base.decode_jwt_payload(id_token)["sub"]
|
|
129
|
+
profile_subject = profile["sub"] || profile["user_id"]
|
|
130
|
+
next nil if id_token_subject.to_s.empty? || profile_subject.to_s.empty? || id_token_subject != profile_subject
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
user = Base.apply_profile_mapping(
|
|
134
|
+
{
|
|
135
|
+
id: profile["user_id"],
|
|
136
|
+
name: profile["name"],
|
|
137
|
+
email: profile["email"],
|
|
138
|
+
image: profile["picture"],
|
|
139
|
+
emailVerified: !!profile["email_verified"]
|
|
140
|
+
},
|
|
141
|
+
profile,
|
|
142
|
+
normalized
|
|
143
|
+
)
|
|
144
|
+
{user: user, data: profile}
|
|
145
|
+
rescue
|
|
146
|
+
nil
|
|
147
|
+
end
|
|
148
|
+
end
|
|
33
149
|
provider
|
|
34
150
|
end
|
|
35
151
|
end
|
|
@@ -5,28 +5,49 @@ module BetterAuth
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
7
|
def reddit(client_id:, client_secret:, scopes: ["identity"], **options)
|
|
8
|
-
Base.
|
|
8
|
+
normalized = Base.normalize_options(options)
|
|
9
|
+
token_endpoint = normalized[:token_endpoint] || "https://www.reddit.com/api/v1/access_token"
|
|
10
|
+
provider = Base.oauth_provider(
|
|
9
11
|
id: "reddit",
|
|
10
12
|
name: "Reddit",
|
|
11
13
|
client_id: client_id,
|
|
12
14
|
client_secret: client_secret,
|
|
13
15
|
authorization_endpoint: "https://www.reddit.com/api/v1/authorize",
|
|
14
|
-
token_endpoint:
|
|
16
|
+
token_endpoint: token_endpoint,
|
|
15
17
|
user_info_endpoint: "https://oauth.reddit.com/api/v1/me",
|
|
16
18
|
user_info_headers: {"User-Agent" => "better-auth"},
|
|
17
19
|
scopes: scopes,
|
|
18
20
|
auth_params: ->(_data, opts) { {duration: opts[:duration]} },
|
|
21
|
+
token_authentication: :basic,
|
|
19
22
|
profile_map: ->(profile) {
|
|
20
23
|
{
|
|
21
24
|
id: profile["id"],
|
|
22
25
|
name: profile["name"],
|
|
23
|
-
email: profile["
|
|
26
|
+
email: "#{profile["id"]}@reddit.invalid",
|
|
24
27
|
image: profile["icon_img"].to_s.split("?").first,
|
|
25
|
-
emailVerified:
|
|
28
|
+
emailVerified: false
|
|
26
29
|
}
|
|
27
30
|
},
|
|
28
31
|
**options
|
|
29
32
|
)
|
|
33
|
+
provider[:validate_authorization_code] = lambda do |data|
|
|
34
|
+
Base.post_token_form(
|
|
35
|
+
token_endpoint,
|
|
36
|
+
{
|
|
37
|
+
code: data[:code] || data["code"],
|
|
38
|
+
grant_type: "authorization_code",
|
|
39
|
+
redirect_uri: data[:redirect_uri] || data[:redirectURI] || data["redirect_uri"] || data["redirectURI"]
|
|
40
|
+
},
|
|
41
|
+
client_id: Base.primary_client_id(client_id),
|
|
42
|
+
client_secret: client_secret,
|
|
43
|
+
authentication: :basic,
|
|
44
|
+
headers: {
|
|
45
|
+
"accept" => "text/plain",
|
|
46
|
+
"user-agent" => "better-auth"
|
|
47
|
+
}
|
|
48
|
+
)
|
|
49
|
+
end
|
|
50
|
+
provider
|
|
30
51
|
end
|
|
31
52
|
end
|
|
32
53
|
end
|
|
@@ -5,7 +5,8 @@ module BetterAuth
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
7
|
def twitch(client_id:, client_secret:, scopes: ["user:read:email", "openid"], **options)
|
|
8
|
-
Base.
|
|
8
|
+
normalized = Base.normalize_options(options)
|
|
9
|
+
provider = Base.oauth_provider(
|
|
9
10
|
id: "twitch",
|
|
10
11
|
name: "Twitch",
|
|
11
12
|
client_id: client_id,
|
|
@@ -34,6 +35,28 @@ module BetterAuth
|
|
|
34
35
|
},
|
|
35
36
|
**options
|
|
36
37
|
)
|
|
38
|
+
provider[:get_user_info] = lambda do |tokens|
|
|
39
|
+
custom = normalized[:get_user_info]
|
|
40
|
+
next custom.call(tokens) if custom
|
|
41
|
+
next nil unless Base.id_token(tokens)
|
|
42
|
+
|
|
43
|
+
profile = Base.decode_jwt_payload(Base.id_token(tokens))
|
|
44
|
+
next nil if profile.empty?
|
|
45
|
+
|
|
46
|
+
user = Base.apply_profile_mapping(
|
|
47
|
+
{
|
|
48
|
+
id: profile["sub"],
|
|
49
|
+
name: profile["preferred_username"],
|
|
50
|
+
email: profile["email"],
|
|
51
|
+
image: profile["picture"],
|
|
52
|
+
emailVerified: !!profile["email_verified"]
|
|
53
|
+
},
|
|
54
|
+
profile,
|
|
55
|
+
normalized
|
|
56
|
+
)
|
|
57
|
+
{user: user, data: profile}
|
|
58
|
+
end
|
|
59
|
+
provider
|
|
37
60
|
end
|
|
38
61
|
end
|
|
39
62
|
end
|
|
@@ -5,7 +5,8 @@ module BetterAuth
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
7
|
def twitter(client_id:, client_secret:, scopes: ["users.read", "tweet.read", "offline.access", "users.email"], **options)
|
|
8
|
-
Base.
|
|
8
|
+
normalized = Base.normalize_options(options)
|
|
9
|
+
provider = Base.oauth_provider(
|
|
9
10
|
id: "twitter",
|
|
10
11
|
name: "Twitter",
|
|
11
12
|
client_id: client_id,
|
|
@@ -15,6 +16,7 @@ module BetterAuth
|
|
|
15
16
|
user_info_endpoint: "https://api.x.com/2/users/me?user.fields=profile_image_url,verified",
|
|
16
17
|
scopes: scopes,
|
|
17
18
|
pkce: true,
|
|
19
|
+
token_authentication: :basic,
|
|
18
20
|
profile_map: ->(profile) {
|
|
19
21
|
data = profile["data"] || profile
|
|
20
22
|
{
|
|
@@ -27,6 +29,39 @@ module BetterAuth
|
|
|
27
29
|
},
|
|
28
30
|
**options
|
|
29
31
|
)
|
|
32
|
+
provider[:get_user_info] = lambda do |tokens|
|
|
33
|
+
custom = normalized[:get_user_info]
|
|
34
|
+
next custom.call(tokens) if custom
|
|
35
|
+
|
|
36
|
+
profile = Base.get_json(
|
|
37
|
+
"https://api.x.com/2/users/me?user.fields=profile_image_url",
|
|
38
|
+
"Authorization" => "Bearer #{Base.access_token(tokens)}"
|
|
39
|
+
)
|
|
40
|
+
next nil unless profile
|
|
41
|
+
|
|
42
|
+
email_data = Base.get_json(
|
|
43
|
+
"https://api.x.com/2/users/me?user.fields=confirmed_email",
|
|
44
|
+
"Authorization" => "Bearer #{Base.access_token(tokens)}"
|
|
45
|
+
)
|
|
46
|
+
data = profile["data"] || profile
|
|
47
|
+
confirmed_email = email_data&.dig("data", "confirmed_email")
|
|
48
|
+
data["email"] = confirmed_email if confirmed_email
|
|
49
|
+
data["confirmed_email"] = true if confirmed_email
|
|
50
|
+
|
|
51
|
+
user = Base.apply_profile_mapping(
|
|
52
|
+
{
|
|
53
|
+
id: data["id"],
|
|
54
|
+
name: data["name"],
|
|
55
|
+
email: data["email"] || data["username"],
|
|
56
|
+
image: data["profile_image_url"],
|
|
57
|
+
emailVerified: !!data["confirmed_email"]
|
|
58
|
+
},
|
|
59
|
+
profile,
|
|
60
|
+
normalized
|
|
61
|
+
)
|
|
62
|
+
{user: user, data: profile}
|
|
63
|
+
end
|
|
64
|
+
provider
|
|
30
65
|
end
|
|
31
66
|
end
|
|
32
67
|
end
|
|
@@ -5,7 +5,8 @@ module BetterAuth
|
|
|
5
5
|
module_function
|
|
6
6
|
|
|
7
7
|
def vk(client_id:, client_secret:, scopes: ["email", "phone"], **options)
|
|
8
|
-
Base.
|
|
8
|
+
normalized = Base.normalize_options(options)
|
|
9
|
+
provider = Base.oauth_provider(
|
|
9
10
|
id: "vk",
|
|
10
11
|
name: "VK",
|
|
11
12
|
client_id: client_id,
|
|
@@ -29,6 +30,38 @@ module BetterAuth
|
|
|
29
30
|
},
|
|
30
31
|
**options
|
|
31
32
|
)
|
|
33
|
+
provider[:get_user_info] = lambda do |tokens|
|
|
34
|
+
custom = normalized[:get_user_info]
|
|
35
|
+
next custom.call(tokens) if custom
|
|
36
|
+
|
|
37
|
+
access_token = Base.access_token(tokens)
|
|
38
|
+
next nil unless access_token
|
|
39
|
+
|
|
40
|
+
profile = Base.post_json(
|
|
41
|
+
"https://id.vk.com/oauth2/user_info",
|
|
42
|
+
{access_token: access_token, client_id: client_id},
|
|
43
|
+
"Content-Type" => "application/x-www-form-urlencoded"
|
|
44
|
+
)
|
|
45
|
+
next nil unless profile
|
|
46
|
+
|
|
47
|
+
user_profile = profile["user"] || profile
|
|
48
|
+
user_map = normalized[:map_profile_to_user]&.call(profile) || {}
|
|
49
|
+
next nil if user_profile["email"].to_s.empty? && user_map[:email].to_s.empty? && user_map["email"].to_s.empty?
|
|
50
|
+
|
|
51
|
+
user = {
|
|
52
|
+
id: user_profile["user_id"],
|
|
53
|
+
first_name: user_profile["first_name"],
|
|
54
|
+
last_name: user_profile["last_name"],
|
|
55
|
+
name: [user_profile["first_name"], user_profile["last_name"]].compact.join(" "),
|
|
56
|
+
email: user_profile["email"],
|
|
57
|
+
image: user_profile["avatar"],
|
|
58
|
+
emailVerified: false,
|
|
59
|
+
birthday: user_profile["birthday"],
|
|
60
|
+
sex: user_profile["sex"]
|
|
61
|
+
}.merge(user_map)
|
|
62
|
+
{user: user, data: profile}
|
|
63
|
+
end
|
|
64
|
+
provider
|
|
32
65
|
end
|
|
33
66
|
end
|
|
34
67
|
end
|
|
@@ -17,10 +17,11 @@ module BetterAuth
|
|
|
17
17
|
scopes: scopes,
|
|
18
18
|
scope_separator: ",",
|
|
19
19
|
profile_map: ->(profile) {
|
|
20
|
+
id = profile["unionid"] || profile["openid"]
|
|
20
21
|
{
|
|
21
|
-
id:
|
|
22
|
+
id: id,
|
|
22
23
|
name: profile["nickname"],
|
|
23
|
-
email: profile["email"],
|
|
24
|
+
email: profile["email"] || "#{id}@wechat.invalid",
|
|
24
25
|
image: profile["headimgurl"],
|
|
25
26
|
emailVerified: false
|
|
26
27
|
}
|
|
@@ -85,11 +86,12 @@ module BetterAuth
|
|
|
85
86
|
profile = Base.get_json(url)
|
|
86
87
|
next nil if !profile || profile["errcode"]
|
|
87
88
|
|
|
89
|
+
id = profile["unionid"] || profile["openid"] || openid
|
|
88
90
|
user = Base.apply_profile_mapping(
|
|
89
91
|
{
|
|
90
|
-
id:
|
|
92
|
+
id: id,
|
|
91
93
|
name: profile["nickname"],
|
|
92
|
-
email: profile["email"],
|
|
94
|
+
email: profile["email"] || "#{id}@wechat.invalid",
|
|
93
95
|
image: profile["headimgurl"],
|
|
94
96
|
emailVerified: false
|
|
95
97
|
},
|
|
@@ -39,7 +39,7 @@ module BetterAuth
|
|
|
39
39
|
def plan_from_existing(options, existing:, dialect:)
|
|
40
40
|
dialect = normalize_dialect(dialect)
|
|
41
41
|
config = configuration_for(options)
|
|
42
|
-
desired = BetterAuth::Schema.
|
|
42
|
+
desired = BetterAuth::Schema.migration_tables(config)
|
|
43
43
|
to_create = []
|
|
44
44
|
to_add = []
|
|
45
45
|
to_index = []
|
|
@@ -219,14 +219,14 @@ module BetterAuth
|
|
|
219
219
|
def execute_sql(connection, sql)
|
|
220
220
|
statements(sql).each_with_object([]) do |statement, results|
|
|
221
221
|
result =
|
|
222
|
-
if connection.respond_to?(:
|
|
223
|
-
connection.exec(statement)
|
|
224
|
-
elsif connection.respond_to?(:execute)
|
|
222
|
+
if connection.respond_to?(:execute)
|
|
225
223
|
connection.execute(statement)
|
|
224
|
+
elsif connection.respond_to?(:exec)
|
|
225
|
+
connection.exec(statement)
|
|
226
226
|
elsif connection.respond_to?(:query)
|
|
227
227
|
connection.query(statement)
|
|
228
228
|
else
|
|
229
|
-
raise UnsupportedAdapterError, "SQL connection does not support
|
|
229
|
+
raise UnsupportedAdapterError, "SQL connection does not support execute, exec, or query"
|
|
230
230
|
end
|
|
231
231
|
results.concat(result.to_a) if result.respond_to?(:to_a)
|
|
232
232
|
end
|
data/lib/better_auth/version.rb
CHANGED
data/lib/better_auth.rb
CHANGED
|
@@ -38,50 +38,11 @@ require_relative "better_auth/context"
|
|
|
38
38
|
require_relative "better_auth/plugin_context"
|
|
39
39
|
require_relative "better_auth/plugin_registry"
|
|
40
40
|
require_relative "better_auth/plugins"
|
|
41
|
-
|
|
42
|
-
require_relative "better_auth/plugins/custom_session"
|
|
43
|
-
require_relative "better_auth/plugins/multi_session"
|
|
44
|
-
require_relative "better_auth/plugins/last_login_method"
|
|
45
|
-
require_relative "better_auth/plugins/bearer"
|
|
46
|
-
require_relative "better_auth/plugins/jwt"
|
|
47
|
-
require_relative "better_auth/plugins/open_api"
|
|
48
|
-
require_relative "better_auth/plugins/access"
|
|
49
|
-
require_relative "better_auth/plugins/username"
|
|
50
|
-
require_relative "better_auth/plugins/anonymous"
|
|
51
|
-
require_relative "better_auth/plugins/magic_link"
|
|
52
|
-
require_relative "better_auth/plugins/email_otp"
|
|
53
|
-
require_relative "better_auth/plugins/phone_number"
|
|
54
|
-
require_relative "better_auth/plugins/one_time_token"
|
|
55
|
-
require_relative "better_auth/plugins/one_tap"
|
|
56
|
-
require_relative "better_auth/plugins/siwe"
|
|
57
|
-
require_relative "better_auth/plugins/generic_oauth"
|
|
58
|
-
require_relative "better_auth/plugins/dub"
|
|
59
|
-
require_relative "better_auth/plugins/oauth_proxy"
|
|
60
|
-
require_relative "better_auth/plugins/passkey"
|
|
61
|
-
require_relative "better_auth/plugins/organization/schema"
|
|
62
|
-
require_relative "better_auth/plugins/organization"
|
|
63
|
-
require_relative "better_auth/plugins/admin/schema"
|
|
64
|
-
require_relative "better_auth/plugins/admin"
|
|
65
|
-
require_relative "better_auth/plugins/oauth_protocol"
|
|
66
|
-
require_relative "better_auth/plugins/oidc_provider"
|
|
67
|
-
require_relative "better_auth/plugins/oauth_provider"
|
|
68
|
-
require_relative "better_auth/plugins/device_authorization"
|
|
69
|
-
require_relative "better_auth/plugins/mcp"
|
|
70
|
-
require_relative "better_auth/plugins/two_factor"
|
|
71
|
-
require_relative "better_auth/plugins/captcha"
|
|
72
|
-
require_relative "better_auth/plugins/have_i_been_pwned"
|
|
73
|
-
require_relative "better_auth/plugins/api_key"
|
|
74
|
-
require_relative "better_auth/plugins/sso"
|
|
75
|
-
require_relative "better_auth/plugins/scim"
|
|
41
|
+
BetterAuth::Plugins.load_boot_plugins!
|
|
76
42
|
require_relative "better_auth/social_providers"
|
|
77
|
-
%w[
|
|
78
|
-
better_auth/plugins/stripe
|
|
79
|
-
better_auth/plugins/expo
|
|
80
|
-
].each do |optional_plugin|
|
|
81
|
-
require_relative optional_plugin if File.file?(File.expand_path("#{optional_plugin}.rb", __dir__))
|
|
82
|
-
end
|
|
83
43
|
require_relative "better_auth/session_store"
|
|
84
44
|
require_relative "better_auth/cookies"
|
|
45
|
+
require_relative "better_auth/oauth_state"
|
|
85
46
|
require_relative "better_auth/session"
|
|
86
47
|
require_relative "better_auth/endpoint"
|
|
87
48
|
require_relative "better_auth/routes/validation"
|
|
@@ -107,4 +68,13 @@ module BetterAuth
|
|
|
107
68
|
def self.auth(options = {})
|
|
108
69
|
Auth.new(options)
|
|
109
70
|
end
|
|
71
|
+
|
|
72
|
+
def self.const_missing(name)
|
|
73
|
+
if name == :OpenAPI
|
|
74
|
+
Plugins.load_plugin!(:open_api)
|
|
75
|
+
return const_get(:OpenAPI) if const_defined?(:OpenAPI, false)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
super
|
|
79
|
+
end
|
|
110
80
|
end
|