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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b68d123a84af65f59b0b60725fdbd3dfc7b9929a39d61d4b44a128690e0122ec
|
4
|
+
data.tar.gz: e0d237a3775a09b5a40206ee42f9c84baa61cf98209a54eb5e8c39bae464a4cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
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
|
-
|
73
|
-
context.
|
74
|
-
|
75
|
-
|
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
|
|