gamora 0.6.0 → 0.7.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: dac3585239f26c73703bedba78b8fb157fcae96e795606833510820dcc541635
4
- data.tar.gz: b12bd007f45d5b97861254c0e7418faa14554eadc31fc0b7fa196932abb8915d
3
+ metadata.gz: e164eceeb7c3a744ed43be46877cb904fff4add871d4a7b4fac25e4837f2f731
4
+ data.tar.gz: cff5f7b7f3ffd07b13d6e6cc0e476ae86c87788bdf47a4e9c9ff4703dfe54d64
5
5
  SHA512:
6
- metadata.gz: 4b824e97d834065f0beb2d0a6d30d343f02edb70d1cecbc7bcd598166474a32e71849b45e46289745ce2d279166a810ed9572cfe6688e48c0e6a9f496cb7e6dd
7
- data.tar.gz: 6fd882d104aa35374d94e40f1b98b9dc708e26689da28ec9a0736ad4f7b7cba6bf177ecc18ddc8e0c8c5303a087404ec95ead3a20cf8cc2405be0eb49128594b
6
+ metadata.gz: b5da9c118a7f13441147ded22bb7bc237e7b51cd583e7355fa65370ef4e71dfa5256150f6ff0b6aad0f0169a1113dc9a8af2514aecff33454bb571391dea74de
7
+ data.tar.gz: ad67d0b0be691f544bd2ffcc277b6a70f036bd20ddbabfd9a48b1f92023f84023e8eaa715b6ccc82a9f8a90f4be975b0ac54a657bc18a14e6fd6e40f4bff6f23
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2022 Alejandro Gutiérrez
3
+ Copyright (c) 2023 Alejandro Gutiérrez
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -38,6 +38,41 @@ end
38
38
  To see the full list of configuration options please check your gamora
39
39
  initializer.
40
40
 
41
+ ## Mount Gamora Engine
42
+
43
+ In order to have the authorization and callback endpoints mount the
44
+ engine in the `config/routes.rb` file:
45
+
46
+ ```ruby
47
+ Rails.application.routes.draw do
48
+ ...
49
+ mount Gamora::Engine => "/auth"
50
+
51
+ ...
52
+ end
53
+ ```
54
+
55
+ This will enable the following routes in the parent application:
56
+
57
+ #### `gamora.authorization_path`
58
+
59
+ This endpoint will redirect users to the IDP generating url and query
60
+ params based on the configuration. This endpoint is called automatically
61
+ when the user is not logged in and the application requires users to be
62
+ authenticated.
63
+
64
+ #### `gamora.logout_path`
65
+
66
+ This endpoint allows users to be logged out from the application and the
67
+ IDP. It removes the access and refresh tokens and redirects to IDP in order
68
+ to force users to authenticate again.
69
+
70
+ #### `gamora.callback_path`
71
+
72
+ This endpoint is the responsible to received the auth code provided by
73
+ the IDP and generate and access token. This endpoint is called automatically
74
+ once the user authenticates successfully in the IDP.
75
+
41
76
  ## User authentication
42
77
 
43
78
  ### Web-based applications
@@ -1,7 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Gamora
2
4
  module AuthorizationUrl
3
5
  def authorization_url(params, extra_params = {})
4
- default_params = {
6
+ data =
7
+ default_params
8
+ .merge(extra_params)
9
+ .merge(authorization_params(params))
10
+ .compact_blank
11
+
12
+ Client.from_config.auth_code.authorize_url(data)
13
+ end
14
+
15
+ private
16
+
17
+ def default_params
18
+ {
5
19
  scope: Configuration.default_scope,
6
20
  theme: Configuration.default_theme,
7
21
  prompt: Configuration.default_prompt,
@@ -9,18 +23,8 @@ module Gamora
9
23
  branding: Configuration.default_branding,
10
24
  ui_locales: Configuration.ui_locales.call
11
25
  }
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
26
  end
21
27
 
22
- private
23
-
24
28
  def authorization_params(params)
25
29
  params.permit(
26
30
  :scope,
@@ -6,8 +6,8 @@ module Gamora
6
6
 
7
7
  def show
8
8
  redirect_to authorization_url(params),
9
- allow_other_host: true,
10
- status: :see_other
9
+ allow_other_host: true,
10
+ status: :see_other
11
11
  end
12
12
  end
13
13
  end
@@ -7,7 +7,6 @@ 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
-
11
10
  rescue OAuth2::Error
12
11
  render plain: "Invalid authorization code"
13
12
  end
@@ -9,8 +9,8 @@ module Gamora
9
9
  session[:refresh_token] = nil
10
10
 
11
11
  redirect_to authorization_url(params, { max_age: 0 }),
12
- allow_other_host: true,
13
- status: :see_other
12
+ allow_other_host: true,
13
+ status: :see_other
14
14
  end
15
15
  end
16
16
  end
@@ -3,6 +3,16 @@
3
3
  module Gamora
4
4
  module Authentication
5
5
  module Base
6
+ CLAIMS = {
7
+ sub: :id,
8
+ email: :email,
9
+ given_name: :first_name,
10
+ family_name: :last_name,
11
+ phone_number: :phone_number,
12
+ email_verified: :email_verified,
13
+ phone_number_verified: :phone_number_verified
14
+ }.freeze
15
+
6
16
  def authenticate_user!
7
17
  claims = resource_owner_claims(access_token)
8
18
  assign_current_user_from_claims(claims) if claims.present?
@@ -33,20 +43,12 @@ module Gamora
33
43
  end
34
44
 
35
45
  def user_attributes_from_claims(claims)
36
- claims.transform_keys do |key|
37
- case key
38
- when :sub then :id
39
- when :email then :email
40
- when :given_name then :first_name
41
- when :family_name then :last_name
42
- when :phone_number then :phone_number
43
- else key
44
- end
45
- end
46
+ claims.slice(*CLAIMS.keys).transform_keys(CLAIMS)
46
47
  end
47
48
 
48
49
  def resource_owner_claims(access_token)
49
50
  return {} if access_token.blank?
51
+
50
52
  resource_owner_claims!(access_token)
51
53
  end
52
54
 
@@ -9,6 +9,7 @@ module Gamora
9
9
 
10
10
  def validate_authentication!
11
11
  return if current_user.present?
12
+
12
13
  user_authentication_failed!
13
14
  end
14
15
 
@@ -16,6 +17,7 @@ module Gamora
16
17
  pattern = /^Bearer /
17
18
  header = request.headers["Authorization"]
18
19
  return unless header&.match(pattern)
20
+
19
21
  header.gsub(pattern, "")
20
22
  end
21
23
 
@@ -13,6 +13,7 @@ module Gamora
13
13
 
14
14
  def validate_authentication!
15
15
  return if current_user.present?
16
+
16
17
  session["gamora.origin"] = request.original_url
17
18
  user_authentication_failed!
18
19
  end
data/lib/gamora/client.rb CHANGED
@@ -2,10 +2,18 @@
2
2
 
3
3
  module Gamora
4
4
  class Client < OAuth2::Client
5
- def self.from_config
6
- new(
7
- Configuration.client_id,
8
- Configuration.client_secret,
5
+ class << self
6
+ def from_config
7
+ new(
8
+ Configuration.client_id,
9
+ Configuration.client_secret,
10
+ client_options
11
+ )
12
+ end
13
+
14
+ private
15
+
16
+ def client_options
9
17
  {
10
18
  site: Configuration.site,
11
19
  token_url: Configuration.token_url,
@@ -14,7 +22,7 @@ module Gamora
14
22
  userinfo_url: Configuration.userinfo_url,
15
23
  authorize_url: Configuration.authorize_url
16
24
  }
17
- )
25
+ end
18
26
  end
19
27
 
20
28
  def userinfo(access_token)
data/lib/gamora/user.rb CHANGED
@@ -8,6 +8,8 @@ module Gamora
8
8
  :email,
9
9
  :last_name,
10
10
  :first_name,
11
- :phone_number
11
+ :phone_number,
12
+ :email_verified,
13
+ :phone_number_verified
12
14
  end
13
15
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gamora
4
- VERSION = "0.6.0"
4
+ VERSION = "0.7.0"
5
5
  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.6.0
4
+ version: 0.7.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: 2023-01-11 00:00:00.000000000 Z
11
+ date: 2023-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
- rubygems_version: 3.3.7
94
+ rubygems_version: 3.4.17
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: OpenID Connect Relying Party for rails apps.