Class: Doorkeeper::OAuth::Scopes

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
lib/doorkeeper/oauth/scopes.rb

Constant Summary collapse

DYNAMIC_SCOPE_WILDCARD =
"*"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScopes

Returns a new instance of Scopes.



34
35
36
# File 'lib/doorkeeper/oauth/scopes.rb', line 34

def initialize
  @scopes = []
end

Class Method Details

.from_array(array) ⇒ Object



26
27
28
29
30
# File 'lib/doorkeeper/oauth/scopes.rb', line 26

def self.from_array(array)
  new.tap do |scope|
    scope.add(*array)
  end
end

.from_string(string) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/doorkeeper/oauth/scopes.rb', line 11

def self.from_string(string)
  string ||= ""

  # A non-string scope (e.g. `scope[a]=b`, parsed by Rack into a Hash)
  # has no octets to split into tokens. Reject it as a malformed request
  # (RFC 6749 ยง3.3); at the token endpoint TokensController's rescue_from
  # turns this into invalid_request. Callers that always pass a string
  # (the model layer) never hit this.
  raise Errors::InvalidScopeParameter, "scope must be a String" unless string.is_a?(String)

  new.tap do |scope|
    scope.add(*string.split)
  end
end

Instance Method Details

#&(other) ⇒ Object

DEPRECATED: With dynamic scopes, #allowed should be called because A & B doesn't really make sense with dynamic scopes.

For example, if A = user:* and B is user:1, A & B = []. If we modified this method to take dynamic scopes into an account, then order becomes important, and this would violate the principle that A & B = B & A.



87
88
89
90
91
# File 'lib/doorkeeper/oauth/scopes.rb', line 87

def &(other)
  return allowed(other) if dynamic_scopes_enabled?

  self.class.from_array(all & to_array(other))
end

#+(other) ⇒ Object



69
70
71
# File 'lib/doorkeeper/oauth/scopes.rb', line 69

def +(other)
  self.class.from_array(all + to_array(other))
end

#<=>(other) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/doorkeeper/oauth/scopes.rb', line 73

def <=>(other)
  if other.respond_to?(:map)
    map(&:to_s).sort <=> other.map(&:to_s).sort
  else
    super
  end
end

#add(*scopes) ⇒ Object



50
51
52
53
# File 'lib/doorkeeper/oauth/scopes.rb', line 50

def add(*scopes)
  @scopes.push(*scopes.map(&:to_s))
  @scopes.uniq!
end

#allObject



55
56
57
# File 'lib/doorkeeper/oauth/scopes.rb', line 55

def all
  @scopes
end

#allowed(other) ⇒ Object

Returns a set of scopes that are allowed, taking dynamic scopes into account. This instance's scopes is taken as the allowed set, and the passed value is the set to filter.

Parameters:

  • other

    The set of scopes to filter



98
99
100
101
# File 'lib/doorkeeper/oauth/scopes.rb', line 98

def allowed(other)
  filtered_scopes = other.select { |scope| exists?(scope) }
  self.class.from_array(filtered_scopes)
end

#common(other) ⇒ Object

Returns the scopes present in both sets, taking dynamic scopes into account: a dynamic scope pattern (e.g. user:*) on either side matches the concrete scope (e.g. user:1) on the other, and the concrete scope is what ends up in the result. Unlike #allowed, the same scopes are returned regardless of which set is the receiver, though the order may differ: scopes matched from the argument come first, followed by any scope only the receiver contributes (a concrete scope matched by a pattern in the argument).

Parameters:

  • other

    The set of scopes to intersect with



113
114
115
116
117
118
# File 'lib/doorkeeper/oauth/scopes.rb', line 113

def common(other)
  other = self.class.from_array(to_array(other))
  matched = other.select { |scope| exists?(scope) } +
            select { |scope| other.exists?(scope) }
  self.class.from_array(matched)
end

#exists?(scope) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/doorkeeper/oauth/scopes.rb', line 38

def exists?(scope)
  scope = scope.to_s

  @scopes.any? do |allowed_scope|
    if dynamic_scopes_enabled? && dynamic_scopes_present?(allowed_scope, scope)
      dynamic_scope_match?(allowed_scope, scope)
    else
      allowed_scope == scope
    end
  end
end

#scopes?(scopes) ⇒ Boolean Also known as: has_scopes?

Returns:

  • (Boolean)


63
64
65
# File 'lib/doorkeeper/oauth/scopes.rb', line 63

def scopes?(scopes)
  scopes.all? { |scope| exists?(scope) }
end

#to_sObject



59
60
61
# File 'lib/doorkeeper/oauth/scopes.rb', line 59

def to_s
  @scopes.join(" ")
end