grape_simple_auth 0.2.1 → 0.3.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: 514832426298a7161edf6a2ee9eecc2744da6c345f5f5abb0a0c2f2bc0f7849d
4
- data.tar.gz: 925214598e994294a61ef684a08b9b385d8e6d1ae437128b658f89621a87de5d
3
+ metadata.gz: b68d123a84af65f59b0b60725fdbd3dfc7b9929a39d61d4b44a128690e0122ec
4
+ data.tar.gz: e0d237a3775a09b5a40206ee42f9c84baa61cf98209a54eb5e8c39bae464a4cf
5
5
  SHA512:
6
- metadata.gz: 3300fdfa4bfbfdfd7f82449ca3ebb7584392d707bb4752b885c20f337afdc7860a159dfabaf11b23afd0cfe5a67f62cece011cf91455fa91df49ce74745ca38c
7
- data.tar.gz: 7bb618d60232690f1923744dccb5bdaf4603ca8c5038f7fe2f6135906bb9410f93e9ee0e361d596d291f763e42f2c07821b60e8e27509e8d0ffdaaf362693dd4
6
+ metadata.gz: 909788b220f755a20544bbc7d3c2a84056c17b7b9cb3282bb88ca5ae1cffa4547bc35d746d56a36ba2bfd45e6f771c856d018958d466e71662a8281588592f53
7
+ data.tar.gz: 745d15bdcb80b2f3db01c62d71081ff778d4087abfd4aacc914f18aa43f677a00160fb2184ca20c80709c508bb40019a8ba42a3dcf9bed53eba3933e52fed610
@@ -10,6 +10,14 @@ module GrapeSimpleAuth
10
10
  @protected_endpoint || false
11
11
  end
12
12
 
13
+ def optional_endpoint=(opt)
14
+ @optional_endpoint = opt
15
+ end
16
+
17
+ def optional_endpoint?
18
+ @optional_endpoint || false
19
+ end
20
+
13
21
  def the_access_token
14
22
  @_the_access_token
15
23
  end
@@ -2,6 +2,10 @@ module GrapeSimpleAuth
2
2
  module AuthStrategies
3
3
  class Swagger < GrapeSimpleAuth::BaseStrategy
4
4
 
5
+ def optional_endpoint?
6
+ has_authorizations? && !!optional_oauth2
7
+ end
8
+
5
9
  def endpoint_protected?
6
10
  has_authorizations? && !!authorization_type_oauth2
7
11
  end
@@ -11,7 +15,11 @@ module GrapeSimpleAuth
11
15
  end
12
16
 
13
17
  def auth_scopes
14
- authorization_type_oauth2.map { |hash| hash[:scope].to_sym }
18
+ if optional_endpoint?
19
+ optional_oauth2.map { |hash| hash[:scope].to_sym }
20
+ else
21
+ authorization_type_oauth2.map { |hash| hash[:scope].to_sym }
22
+ end
15
23
  end
16
24
 
17
25
  private
@@ -28,6 +36,10 @@ module GrapeSimpleAuth
28
36
  endpoint_authorizations[:oauth2]
29
37
  end
30
38
 
39
+ def optional_oauth2
40
+ endpoint_authorizations[:optional_oauth2]
41
+ end
42
+
31
43
  end
32
44
  end
33
45
  end
@@ -12,6 +12,16 @@ module GrapeSimpleAuth
12
12
  description[:authorizations] = { oauth2: scopes.map { |x| { scope: x } } }
13
13
  end
14
14
 
15
+ def optional_oauth2(*scopes)
16
+ description = if respond_to?(:route_setting) # >= grape-0.10.0
17
+ route_setting(:description) || route_setting(:description, {})
18
+ else
19
+ @last_description ||= {}
20
+ end
21
+
22
+ description[:authorizations] = { optional_oauth2: scopes.map { |x| { scope: x } } }
23
+ end
24
+
15
25
  Grape::API.extend self
16
26
  end
17
27
  end
@@ -39,6 +39,10 @@ module GrapeSimpleAuth
39
39
  auth_strategy.endpoint_protected?
40
40
  end
41
41
 
42
+ def optional_endpoint?
43
+ auth_strategy.optional_endpoint?
44
+ end
45
+
42
46
  def auth_scopes
43
47
  return *nil unless auth_strategy.has_auth_scopes?
44
48
  auth_strategy.auth_scopes
@@ -66,13 +70,20 @@ module GrapeSimpleAuth
66
70
  context.extend(GrapeSimpleAuth::AuthMethods)
67
71
 
68
72
  context.protected_endpoint = endpoint_protected?
69
- return unless context.protected_endpoint?
73
+ context.optional_endpoint = optional_endpoint?
70
74
 
75
+ return unless context.protected_endpoint? || context.optional_endpoint?
76
+
71
77
  self.the_request = env
72
- resp = authorize!(*auth_scopes)
73
- context.the_access_token = token
74
- context.current_user = resp.parsed_response["data"]["info"] rescue nil
75
- context.credentials = resp.parsed_response["data"]["credential"] rescue nil
78
+
79
+ if token.present? && (context.protected_endpoint? || context.optional_endpoint?)
80
+ resp = authorize!(*auth_scopes)
81
+ context.the_access_token = token
82
+ context.current_user = resp.parsed_response["data"]["info"] rescue nil
83
+ context.credentials = resp.parsed_response["data"]["credential"] rescue nil
84
+ elsif token.nil? && context.protected_endpoint?
85
+ raise GrapeSimpleAuth::Errors::InvalidToken
86
+ end
76
87
  end
77
88
 
78
89
 
@@ -1,3 +1,3 @@
1
1
  module GrapeSimpleAuth
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape_simple_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yunan Helmy