yiffspace 0.0.1
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 +0 -0
- data/LICENSE +20 -0
- data/README.md +25 -0
- data/Rakefile +10 -0
- data/app/assets/config/yiff_space_manifest.js +1 -0
- data/app/assets/stylesheets/yiff_space/application.css +30 -0
- data/app/controllers/yiff_space/application_controller.rb +6 -0
- data/app/helpers/yiff_space/application_helper.rb +6 -0
- data/app/helpers/yiff_space/auth_helper.rb +6 -0
- data/app/models/yiff_space/application_record.rb +7 -0
- data/app/views/layouts/yiff_space/application.html.erb +17 -0
- data/app/views/yiff_space/error.html.erb +3 -0
- data/engines/auth/app/controllers/yiff_space/auth/application_controller.rb +11 -0
- data/engines/auth/app/controllers/yiff_space/auth/root_controller.rb +67 -0
- data/engines/auth/app/views/yiff_space/auth/root/permissions.html.erb +14 -0
- data/engines/auth/config/routes.rb +11 -0
- data/lib/tasks/yiff_space_tasks.rake +6 -0
- data/lib/yiffspace/auth/auth_info/anonymous.rb +64 -0
- data/lib/yiffspace/auth/auth_info.rb +74 -0
- data/lib/yiffspace/auth/client.rb +58 -0
- data/lib/yiffspace/auth/discord_info.rb +63 -0
- data/lib/yiffspace/auth/engine.rb +57 -0
- data/lib/yiffspace/auth/helper.rb +167 -0
- data/lib/yiffspace/auth/permissions.rb +85 -0
- data/lib/yiffspace/auth/set_client_name.rb +15 -0
- data/lib/yiffspace/auth/user_info/anonymous.rb +72 -0
- data/lib/yiffspace/auth/user_info.rb +91 -0
- data/lib/yiffspace/auth.rb +84 -0
- data/lib/yiffspace/config_builder.rb +189 -0
- data/lib/yiffspace/configuration/images.rb +22 -0
- data/lib/yiffspace/configuration.rb +30 -0
- data/lib/yiffspace/core_ext/action_dispatch/set_auth_client/scoped.rb +13 -0
- data/lib/yiffspace/core_ext/action_dispatch/set_auth_client.rb +13 -0
- data/lib/yiffspace/core_ext/active_record/all.rb +3 -0
- data/lib/yiffspace/core_ext/active_record/where_chain.rb +6 -0
- data/lib/yiffspace/core_ext/all.rb +8 -0
- data/lib/yiffspace/core_ext/arel/all.rb +4 -0
- data/lib/yiffspace/core_ext/arel/cross_join_lateral.rb +21 -0
- data/lib/yiffspace/core_ext/arel/left_join_lateral.rb +21 -0
- data/lib/yiffspace/core_ext/enumerable/all.rb +3 -0
- data/lib/yiffspace/core_ext/enumerable/parallel.rb +7 -0
- data/lib/yiffspace/core_ext/hash/all.rb +3 -0
- data/lib/yiffspace/core_ext/hash/to_open_hash.rb +7 -0
- data/lib/yiffspace/core_ext/open_hash.rb +3 -0
- data/lib/yiffspace/core_ext/string/all.rb +5 -0
- data/lib/yiffspace/core_ext/string/sql.rb +7 -0
- data/lib/yiffspace/core_ext/string/to_b.rb +7 -0
- data/lib/yiffspace/core_ext/string/truthy_falsy.rb +7 -0
- data/lib/yiffspace/extensions/action_dispatch/set_auth_client/scoped.rb +15 -0
- data/lib/yiffspace/extensions/action_dispatch/set_auth_client.rb +13 -0
- data/lib/yiffspace/extensions/active_record/where_chain.rb +107 -0
- data/lib/yiffspace/extensions/arel/nodes/cross_join_lateral.rb +17 -0
- data/lib/yiffspace/extensions/arel/nodes/left_join_lateral.rb +17 -0
- data/lib/yiffspace/extensions/arel/table/cross_join_lateral.rb +15 -0
- data/lib/yiffspace/extensions/arel/table/left_join_lateral.rb +15 -0
- data/lib/yiffspace/extensions/arel/visitors/postgresql/cross_join_lateral.rb +19 -0
- data/lib/yiffspace/extensions/arel/visitors/postgresql/left_join_lateral.rb +23 -0
- data/lib/yiffspace/extensions/enumerable/parallel.rb +39 -0
- data/lib/yiffspace/extensions/hash/to_open_hash.rb +15 -0
- data/lib/yiffspace/extensions/string/sql.rb +21 -0
- data/lib/yiffspace/extensions/string/to_b.rb +13 -0
- data/lib/yiffspace/extensions/string/truthy_falsy.rb +17 -0
- data/lib/yiffspace/images/avatar/base.rb +26 -0
- data/lib/yiffspace/images/avatar/discord.rb +37 -0
- data/lib/yiffspace/images/avatar/gravatar.rb +21 -0
- data/lib/yiffspace/images/avatar.rb +29 -0
- data/lib/yiffspace/images/banner/base.rb +26 -0
- data/lib/yiffspace/images/banner/discord.rb +37 -0
- data/lib/yiffspace/images/banner.rb +29 -0
- data/lib/yiffspace/images.rb +6 -0
- data/lib/yiffspace/utils/open_hash.rb +54 -0
- data/lib/yiffspace/utils/set_env_constraint.rb +18 -0
- data/lib/yiffspace/utils.rb +6 -0
- data/lib/yiffspace/version.rb +5 -0
- data/lib/yiffspace.rb +11 -0
- metadata +176 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d76f09af3606e844dc35e1d97ce80a14eb6d99a989d421ec0e8a81ba6a3735f7
|
|
4
|
+
data.tar.gz: b81ec5a4ad23d5e0606fd8247f5b09537078fb9fe271c3a72ac3442963b3554f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 39fcd8410ca585b80edd98ce465d62c188c54a967db2cd98158306c313deaf6b8ea73e3ab3208b2e3cf69100fad641a763a25eecefbbf4a1cc31474851619b3d
|
|
7
|
+
data.tar.gz: 776dd1d3c6c53d1746fd7f671a2ce9667263ee031a34a7329054e753d4415bb0f1cf3d5432c34c258059aeab3bc652fccd57c1b7ed6e6226aaddee94fc93b5cc
|
data/CHANGELOG.md
ADDED
|
File without changes
|
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright Donovan_DMC
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# YiffSpace
|
|
2
|
+
|
|
3
|
+
Collection of ruby code for https://yiff.space and related projects
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem "yiffspace"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
$ bundle install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Contributing
|
|
20
|
+
|
|
21
|
+
Go away
|
|
22
|
+
|
|
23
|
+
## License
|
|
24
|
+
|
|
25
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//= link_directory ../stylesheets/yiffspace .css
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
|
3
|
+
* listed below.
|
|
4
|
+
*
|
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
|
7
|
+
*
|
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
|
11
|
+
* It is generally better to create a new file per style scope.
|
|
12
|
+
*
|
|
13
|
+
*= require_tree .
|
|
14
|
+
*= require_self
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
body {
|
|
18
|
+
background-color: #2C2F33;
|
|
19
|
+
color: #FFFDD0;
|
|
20
|
+
font-size: 2rem;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
body div#page {
|
|
24
|
+
text-align: center;
|
|
25
|
+
|
|
26
|
+
ul.permissions-list {
|
|
27
|
+
display: inline-block;
|
|
28
|
+
text-align: left;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>YiffSpace</title>
|
|
5
|
+
<%= csrf_meta_tags %>
|
|
6
|
+
<%= csp_meta_tag %>
|
|
7
|
+
|
|
8
|
+
<%= stylesheet_link_tag("yiffspace/application", media: "all") %>
|
|
9
|
+
</head>
|
|
10
|
+
<body>
|
|
11
|
+
|
|
12
|
+
<div id="page">
|
|
13
|
+
<%= yield %>
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
</body>
|
|
17
|
+
</html>
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module YiffSpace
|
|
4
|
+
module Auth
|
|
5
|
+
class RootController < ApplicationController
|
|
6
|
+
include(Helper)
|
|
7
|
+
|
|
8
|
+
def show
|
|
9
|
+
state = generate_state!
|
|
10
|
+
self.return_path = params[:path]
|
|
11
|
+
redirect_to(auth_client_config.url(state: state), allow_other_host: true)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def cb
|
|
15
|
+
return render("yiffspace/error", locals: { message: "missing code in request" }, status: :bad_request) if params[:code].blank?
|
|
16
|
+
return render("yiffspace/error", locals: { message: "missing state in request" }, status: :bad_request) if params[:state].blank?
|
|
17
|
+
return render("yiffspace/error", locals: { message: "invalid state in request" }, status: :bad_request) if params[:state] != state
|
|
18
|
+
|
|
19
|
+
reset_state!
|
|
20
|
+
exchange = auth_client_config.exchange(params[:code])
|
|
21
|
+
self.auth = exchange.auth
|
|
22
|
+
self.user = exchange.user
|
|
23
|
+
path = return_path
|
|
24
|
+
action = auth_client_config.after_auth_action
|
|
25
|
+
reset_return_path!
|
|
26
|
+
if action.is_a?(Proc)
|
|
27
|
+
instance_exec(*(action.arity.zero? ? [] : [path]), &action)
|
|
28
|
+
return if performed?
|
|
29
|
+
elsif action.is_a?(String)
|
|
30
|
+
return redirect_to(action)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
redirect_to(path || "/")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def permissions; end
|
|
37
|
+
|
|
38
|
+
def logout
|
|
39
|
+
return redirect_back_or_to("/") if auth.blank? && user.blank?
|
|
40
|
+
|
|
41
|
+
self.return_path = params[:path] # sanitization
|
|
42
|
+
path = return_path
|
|
43
|
+
action = auth_client_config.after_logout_action
|
|
44
|
+
full_reset!
|
|
45
|
+
if action.is_a?(Proc)
|
|
46
|
+
action.call(*[self, path].slice(0, action.arity))
|
|
47
|
+
return if performed?
|
|
48
|
+
elsif action.is_a?(String)
|
|
49
|
+
return redirect_to(action)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
redirect_to(path || "/")
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def debug
|
|
56
|
+
return render("yiffspace/error", locals: { message: "Access Denied" }, status: :forbidden) unless YiffSpace::Auth.enable_debug_action?
|
|
57
|
+
|
|
58
|
+
render(json: {
|
|
59
|
+
env: request.env.select { |env| env.start_with?("yiffspace.") },
|
|
60
|
+
params: params,
|
|
61
|
+
session: session,
|
|
62
|
+
client: auth_client_config.as_json.merge(client_secret: "[REDACTED]"),
|
|
63
|
+
})
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<h1 class="title">Permissions</h1>
|
|
2
|
+
<% if auth? %>
|
|
3
|
+
<% if auth.permissions.any? %>
|
|
4
|
+
<ul class="permissions-list">
|
|
5
|
+
<% auth.permissions.each do |perm| %>
|
|
6
|
+
<li class="permission"><%= perm %></li>
|
|
7
|
+
<% end %>
|
|
8
|
+
</ul>
|
|
9
|
+
<% else %>
|
|
10
|
+
<p class="message">You have no permissions.</p>
|
|
11
|
+
<% end %>
|
|
12
|
+
<% else %>
|
|
13
|
+
<p class="message">You are not logged in.</p>
|
|
14
|
+
<% end %>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
YiffSpace::Auth::Engine.routes.draw do
|
|
4
|
+
constraints(YiffSpace::Auth::SetClientName.default) do
|
|
5
|
+
get(:cb, controller: :root)
|
|
6
|
+
get(:logout, controller: :root)
|
|
7
|
+
get(:permissions, controller: :root)
|
|
8
|
+
get(:debug, controller: :root) if YiffSpace::Auth.enable_debug_action?
|
|
9
|
+
root(action: :show, controller: :root, as: :auth)
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require("singleton")
|
|
4
|
+
|
|
5
|
+
module YiffSpace
|
|
6
|
+
module Auth
|
|
7
|
+
class AuthInfo
|
|
8
|
+
class Anonymous
|
|
9
|
+
include(::Singleton)
|
|
10
|
+
|
|
11
|
+
%i[id token].each do |attr|
|
|
12
|
+
define_method(attr) { |*, **| raise(NotImplementedError, "#{attr} is not present on anonymous auth") }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def entitlements
|
|
16
|
+
[]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def roles
|
|
20
|
+
[]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def permissions
|
|
24
|
+
Permissions.new([])
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def has_permission?(*)
|
|
28
|
+
false
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def anonymous?
|
|
32
|
+
true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# this feels wrong, but it hopefully shouldn't break anything
|
|
36
|
+
def present?
|
|
37
|
+
false
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def blank?
|
|
41
|
+
true
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def serializable_hash(*)
|
|
45
|
+
nil
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def to_session
|
|
49
|
+
serializable_hash
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.from_json(*)
|
|
53
|
+
Anonymous.new
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.from_session(data)
|
|
57
|
+
return nil if data.blank?
|
|
58
|
+
|
|
59
|
+
from_json(data)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module YiffSpace
|
|
4
|
+
module Auth
|
|
5
|
+
class AuthInfo
|
|
6
|
+
attr_reader(:id, :token, :entitlements, :roles, :permissions)
|
|
7
|
+
|
|
8
|
+
# @param id String
|
|
9
|
+
# @param roles Array(String)
|
|
10
|
+
# @param entitlements Array(String)
|
|
11
|
+
# @param token OpenIDConnect::AccessToken
|
|
12
|
+
def initialize(id:, entitlements:, roles:, token:)
|
|
13
|
+
raise(ArgumentError, "no id provided") if id.blank?
|
|
14
|
+
raise(ArgumentError, "no token provided") if token.blank?
|
|
15
|
+
|
|
16
|
+
@id = id
|
|
17
|
+
@token = token
|
|
18
|
+
@entitlements = Array(entitlements)
|
|
19
|
+
@roles = Array(roles)
|
|
20
|
+
@permissions = Permissions.new(@entitlements)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def anonymous?
|
|
24
|
+
false
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# this feels wrong, but it hopefully shouldn't break anything
|
|
28
|
+
def present?
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def blank?
|
|
33
|
+
false
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def has_permission?(name)
|
|
37
|
+
permissions.include?(name.to_s)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def serializable_hash(*)
|
|
41
|
+
{
|
|
42
|
+
"id" => id,
|
|
43
|
+
"token" => ::YiffSpace::Auth.serialize_token(token),
|
|
44
|
+
"entitlements" => entitlements,
|
|
45
|
+
"roles" => roles,
|
|
46
|
+
}
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def to_session
|
|
50
|
+
serializable_hash
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.from_json(data)
|
|
54
|
+
raise(ArgumentError, "invalid data") if data.blank?
|
|
55
|
+
|
|
56
|
+
data = JSON.parse(data) if data.is_a?(String)
|
|
57
|
+
data = ::YiffSpace::Utils::OpenHash.from(data)
|
|
58
|
+
|
|
59
|
+
new(
|
|
60
|
+
id: data.id,
|
|
61
|
+
token: ::YiffSpace::Auth.unserialize_token(data.token),
|
|
62
|
+
entitlements: data.entitlements,
|
|
63
|
+
roles: data.roles,
|
|
64
|
+
)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def self.from_session(data)
|
|
68
|
+
return nil if data.blank?
|
|
69
|
+
|
|
70
|
+
from_json(data)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module YiffSpace
|
|
4
|
+
module Auth
|
|
5
|
+
class Client
|
|
6
|
+
attr_accessor(:client_name, :client_id, :client_secret, :redirect_uri,
|
|
7
|
+
:server_url, :scopes, :auth_session_key, :user_session_key,
|
|
8
|
+
:token_session_key, :return_path_session_key, :state_session_key,
|
|
9
|
+
:state_generator, :after_auth_action, :after_logout_action,
|
|
10
|
+
:update_discord_images)
|
|
11
|
+
|
|
12
|
+
attr_reader(:name)
|
|
13
|
+
|
|
14
|
+
def initialize(name)
|
|
15
|
+
@name = name.to_sym
|
|
16
|
+
@server_url = "https://auth.yiff.space"
|
|
17
|
+
@redirect_uri = "http://127.0.0.1:3000/auth/cb"
|
|
18
|
+
@scopes = %i[openid discord offline_access entitlements]
|
|
19
|
+
@auth_session_key = :"yiffspace_auth_#{name}"
|
|
20
|
+
@user_session_key = :"yiffspace_user_#{name}"
|
|
21
|
+
@token_session_key = :"yiffspace_token_#{name}"
|
|
22
|
+
@return_path_session_key = :"yiffspace_return_path_#{name}"
|
|
23
|
+
@state_session_key = :"yiffspace_state_#{name}"
|
|
24
|
+
@state_generator = -> { SecureRandom.hex(48) }
|
|
25
|
+
@update_discord_images = true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def openid_config
|
|
29
|
+
@openid_config ||= OpenIDConnect::Discovery::Provider::Config.discover!("#{server_url}/application/o/#{client_name}/")
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def oidc_client
|
|
33
|
+
@oidc_client ||= OpenIDConnect::Client.new(
|
|
34
|
+
identifier: client_id,
|
|
35
|
+
secret: client_secret,
|
|
36
|
+
redirect_uri: redirect_uri,
|
|
37
|
+
authorization_endpoint: openid_config.authorization_endpoint,
|
|
38
|
+
token_endpoint: openid_config.token_endpoint,
|
|
39
|
+
userinfo_endpoint: openid_config.userinfo_endpoint,
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def url(state: nil)
|
|
44
|
+
oidc_client.authorization_uri(scope: scopes, state: state)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def exchange(code)
|
|
48
|
+
oidc_client.authorization_code = code
|
|
49
|
+
token = oidc_client.access_token!
|
|
50
|
+
user = token.userinfo!
|
|
51
|
+
id = user.raw_attributes["discord"]["id"]
|
|
52
|
+
authinfo = AuthInfo.new(token: token, entitlements: user.raw_attributes["entitlements"], roles: user.raw_attributes["roles"], id: id)
|
|
53
|
+
userinfo = UserInfo.new(user: user, id: id, discord: user.raw_attributes["discord"], client_id: oidc_client.identifier)
|
|
54
|
+
Auth::ExchangeResponse.new(authinfo, userinfo)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module YiffSpace
|
|
4
|
+
module Auth
|
|
5
|
+
class DiscordInfo
|
|
6
|
+
ATTRIBUTES = %i[id flags avatar banner locale username avatar_url banner_url global_name mfa_enabled public_flags discriminator].freeze
|
|
7
|
+
attr_reader(*ATTRIBUTES)
|
|
8
|
+
|
|
9
|
+
def initialize(options = {}, **kwargs)
|
|
10
|
+
options = options.merge(kwargs).with_indifferent_access
|
|
11
|
+
ATTRIBUTES.each do |attr|
|
|
12
|
+
instance_variable_set("@#{attr}", options[attr])
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
return unless YiffSpace.config.auth.update_discord_images
|
|
16
|
+
|
|
17
|
+
last_avatar = Rails.cache.fetch("yiffspace:auth:discord_avatar_update:#{id}")
|
|
18
|
+
last_banner = Rails.cache.fetch("yiffspace:auth:discord_banner_update:#{id}")
|
|
19
|
+
|
|
20
|
+
if last_avatar != avatar
|
|
21
|
+
Rails.cache.write("yiffspace:auth:discord_avatar_update:#{id}", avatar, expires_in: 30.days)
|
|
22
|
+
update_avatar
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
return unless last_banner != banner
|
|
26
|
+
|
|
27
|
+
Rails.cache.write("yiffspace:auth:discord_banner_update:#{id}", banner, expires_in: 30.days)
|
|
28
|
+
update_banner
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def update_avatar
|
|
32
|
+
Images::Avatar.get_for(id, :discord).update(avatar)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def update_banner
|
|
36
|
+
Images::Banner.get_for(id, :discord).update(banner)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def serializable_hash
|
|
40
|
+
ATTRIBUTES.to_h { |key| [key.to_s, instance_variable_get("@#{key}")] }
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def to_session
|
|
44
|
+
serializable_hash
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.from_json(data)
|
|
48
|
+
raise(ArgumentError, "invalid data") if data.blank?
|
|
49
|
+
|
|
50
|
+
data = JSON.parse(data) if data.is_a?(String)
|
|
51
|
+
data = ::YiffSpace::Utils::OpenHash.from(data)
|
|
52
|
+
|
|
53
|
+
new(data)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def self.from_session(data)
|
|
57
|
+
return nil if data.blank?
|
|
58
|
+
|
|
59
|
+
from_json(data)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require("rails")
|
|
4
|
+
|
|
5
|
+
module YiffSpace
|
|
6
|
+
module Auth
|
|
7
|
+
class Engine < ::Rails::Engine
|
|
8
|
+
isolate_namespace(YiffSpace::Auth)
|
|
9
|
+
config.root = File.expand_path("../../../engines/auth", __dir__)
|
|
10
|
+
shared_root = File.expand_path("../../../app", __dir__)
|
|
11
|
+
shared_controllers = "#{shared_root}/controllers"
|
|
12
|
+
shared_helpers = "#{shared_root}/helpers"
|
|
13
|
+
shared_models = "#{shared_root}/models"
|
|
14
|
+
shared_views = "#{shared_root}/views"
|
|
15
|
+
shared_assets = "#{shared_root}/assets"
|
|
16
|
+
|
|
17
|
+
paths["app/controllers"] << shared_controllers
|
|
18
|
+
paths["app/helpers"] << shared_helpers
|
|
19
|
+
paths["app/models"] << shared_models
|
|
20
|
+
paths["app/views"] << shared_views
|
|
21
|
+
paths["app/assets"] << shared_assets
|
|
22
|
+
|
|
23
|
+
config.autoload_paths += [shared_controllers, shared_helpers, shared_models]
|
|
24
|
+
config.eager_load_paths += [shared_controllers, shared_helpers, shared_models]
|
|
25
|
+
|
|
26
|
+
initializer("yiffspace.auth.asset_paths") do |app|
|
|
27
|
+
if app.config.respond_to?(:assets)
|
|
28
|
+
app.config.assets.paths << "#{shared_assets}/config"
|
|
29
|
+
app.config.assets.precompile += %w[yiffspace/application.css]
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class << self
|
|
34
|
+
def for(name)
|
|
35
|
+
@instances ||= {}
|
|
36
|
+
@instances[name.to_sym] ||= begin
|
|
37
|
+
subclass = Class.new(self)
|
|
38
|
+
subclass.engine_name("yiffspace_auth_#{name}")
|
|
39
|
+
# Inherit isolation settings that aren't copied from the parent class
|
|
40
|
+
subclass.instance_variable_set(:@isolated, true)
|
|
41
|
+
subclass.routes.default_scope = { module: "yiffspace/auth" }
|
|
42
|
+
subclass.routes.draw do
|
|
43
|
+
constraints(SetClientName.new(name)) do
|
|
44
|
+
get(:cb, controller: :root)
|
|
45
|
+
get(:logout, controller: :root)
|
|
46
|
+
get(:permissions, controller: :root)
|
|
47
|
+
get(:debug, controller: :root) if ::YiffSpace::Auth.enable_debug_action?
|
|
48
|
+
root(action: :show, controller: :root, as: :auth)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
subclass
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|