gamora 0.2.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecbfbd0d21575131451160045532e1cb22cb5706a872c0019166e7647a2421cd
4
- data.tar.gz: 2014f6cd02eb8210cff2d58da6add30296bd0bf64d3baef379fccdf5d74b0f9a
3
+ metadata.gz: 563e700187ac79aef45f7d11aad774aee205f7917870f5431d3f1d6d63d7850b
4
+ data.tar.gz: 9817af7d6c4eb85abb6bdaa76d53efc0f1016f6516df95c674366b89ab920b49
5
5
  SHA512:
6
- metadata.gz: af6267f7be0f268be3cfecbf1f2adab95b95d7c4bffb0d0f57673acf65dc1a420656b786d52484dc37cfa546ffd9f67e8d770c1d4abe5423073be65d69b44512
7
- data.tar.gz: 2fa9ca42659f67422a8223ad0131e86e3354f2cb2b3b7d613fd638f0c51398f4a849f6eab0a42c7d75aec5d229fb05f59fbfed0b2c6ad018560836fdc7d8bef1
6
+ metadata.gz: 34de3119b475580ec5b65cab413d8f6ca7aed17fe433542ea81c6ef5ba314a67d9a29c384dd2aaafc9a9ff267510efbf6649a33f43fb7d41a15a0d3b7c308661
7
+ data.tar.gz: 2848933923aee39b310b107e77c6ec219b6642f0bda21c0e61677fbf056f3f80629f0d9bed0dbb81ea2637483d333de77292a871d86a358f19d98e1bade10b69
@@ -0,0 +1,37 @@
1
+ module Gamora
2
+ module AuthorizationUrl
3
+ def authorization_url(params, extra_params = {})
4
+ default_params = {
5
+ scope: Configuration.default_scope,
6
+ theme: Configuration.default_theme,
7
+ prompt: Configuration.default_prompt,
8
+ strategy: Configuration.default_strategy,
9
+ branding: Configuration.default_branding,
10
+ ui_locales: Configuration.ui_locales.call
11
+ }
12
+
13
+ data =
14
+ default_params.
15
+ merge(extra_params).
16
+ merge(authorization_params(params)).
17
+ compact_blank
18
+
19
+ Client.from_config.auth_code.authorize_url(data)
20
+ end
21
+
22
+ private
23
+
24
+ def authorization_params(params)
25
+ params.permit(
26
+ :scope,
27
+ :state,
28
+ :theme,
29
+ :prompt,
30
+ :max_age,
31
+ :strategy,
32
+ :branding,
33
+ :ui_locales
34
+ )
35
+ end
36
+ end
37
+ end
@@ -2,32 +2,12 @@
2
2
 
3
3
  module Gamora
4
4
  class AuthenticationController < ApplicationController
5
- def show
6
- redirect_to authorization_url, allow_other_host: true
7
- end
8
-
9
- private
5
+ include AuthorizationUrl
10
6
 
11
- def authorization_url
12
- Client.from_config.auth_code.authorize_url({
13
- scope: Configuration.default_scope,
14
- theme: Configuration.default_theme,
15
- prompt: Configuration.default_prompt,
16
- strategy: Configuration.default_strategy,
17
- ui_locales: Configuration.ui_locales.call
18
- }.merge(authorization_params).compact_blank)
19
- end
20
-
21
- def authorization_params
22
- params.permit(
23
- :scope,
24
- :state,
25
- :theme,
26
- :prompt,
27
- :max_age,
28
- :strategy,
29
- :ui_locales
30
- )
7
+ def show
8
+ redirect_to authorization_url(params),
9
+ allow_other_host: true,
10
+ status: :see_other
31
11
  end
32
12
  end
33
13
  end
@@ -7,6 +7,7 @@ module Gamora
7
7
  session[:access_token] = access_token.token
8
8
  session[:refresh_token] = access_token.refresh_token
9
9
  redirect_to session.delete("gamora.origin") || main_app.root_path
10
+
10
11
  rescue OAuth2::Error
11
12
  render plain: "Invalid authorization code"
12
13
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gamora
4
+ class UnauthenticationController < ApplicationController
5
+ include AuthorizationUrl
6
+
7
+ def show
8
+ session[:access_token] = nil
9
+ session[:refresh_token] = nil
10
+
11
+ redirect_to authorization_url(params, { max_age: 0 }),
12
+ allow_other_host: true,
13
+ status: :see_other
14
+ end
15
+ end
16
+ end
data/config/routes.rb CHANGED
@@ -2,5 +2,6 @@
2
2
 
3
3
  Gamora::Engine.routes.draw do
4
4
  get "amco", to: "authentication#show", as: :authentication
5
+ get "logout", to: "unauthentication#show", as: :logout
5
6
  get "amco/callback", to: "callback#show", as: :callback
6
7
  end
@@ -47,12 +47,26 @@ module Gamora
47
47
 
48
48
  def resource_owner_claims(access_token)
49
49
  return {} if access_token.blank?
50
- oauth_client.userinfo(access_token)
50
+ resource_owner_claims!(access_token)
51
+ end
52
+
53
+ def resource_owner_claims!(access_token)
54
+ Rails.cache.fetch(cache_key(access_token), cache_options) do
55
+ oauth_client.userinfo(access_token)
56
+ end
51
57
  end
52
58
 
53
59
  def oauth_client
54
60
  Client.from_config
55
61
  end
62
+
63
+ def cache_options
64
+ { expires_in: Configuration.userinfo_cache_expires_in }
65
+ end
66
+
67
+ def cache_key(access_token)
68
+ "userinfo:#{Digest::SHA256.hexdigest(access_token)}"
69
+ end
56
70
  end
57
71
  end
58
72
  end
@@ -13,8 +13,10 @@ module Gamora
13
13
  mattr_accessor :default_scope, default: "openid profile email"
14
14
  mattr_accessor :default_prompt, default: nil
15
15
  mattr_accessor :default_strategy, default: "default"
16
+ mattr_accessor :default_branding, default: "amco"
16
17
  mattr_accessor :default_theme, default: "default"
17
18
  mattr_accessor :ui_locales, default: -> { I18n.locale }
19
+ mattr_accessor :userinfo_cache_expires_in, default: 0.seconds
18
20
 
19
21
  def setup
20
22
  yield(self) if block_given?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gamora
4
- VERSION = "0.2.0"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -15,6 +15,8 @@ Gamora.setup do |config|
15
15
  # config.default_scope = "openid profile email"
16
16
  # config.default_prompt = nil
17
17
  # config.default_strategy = "default"
18
+ # config.default_branding = "amco"
18
19
  # config.default_theme = "default"
19
20
  # config.ui_locales = -> { I18n.locale }
21
+ # config.userinfo_cache_expires_in = 0.seconds
20
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gamora
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Gutiérrez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-28 00:00:00.000000000 Z
11
+ date: 2022-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -49,9 +49,11 @@ files:
49
49
  - MIT-LICENSE
50
50
  - README.md
51
51
  - Rakefile
52
+ - app/controllers/concerns/gamora/authorization_url.rb
52
53
  - app/controllers/gamora/application_controller.rb
53
54
  - app/controllers/gamora/authentication_controller.rb
54
55
  - app/controllers/gamora/callback_controller.rb
56
+ - app/controllers/gamora/unauthentication_controller.rb
55
57
  - app/models/gamora/application_record.rb
56
58
  - config/routes.rb
57
59
  - lib/gamora.rb