2014-12-19 14:15:29 +00:00
|
|
|
module Oauth2::AccessTokenValidationService
|
|
|
|
# Results:
|
|
|
|
VALID = :valid
|
|
|
|
EXPIRED = :expired
|
|
|
|
REVOKED = :revoked
|
|
|
|
INSUFFICIENT_SCOPE = :insufficient_scope
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def validate(token, scopes: [])
|
|
|
|
if token.expired?
|
|
|
|
return EXPIRED
|
|
|
|
|
|
|
|
elsif token.revoked?
|
|
|
|
return REVOKED
|
|
|
|
|
2015-01-18 15:29:37 +00:00
|
|
|
elsif !self.sufficient_scope?(token, scopes)
|
2014-12-19 14:15:29 +00:00
|
|
|
return INSUFFICIENT_SCOPE
|
|
|
|
|
|
|
|
else
|
|
|
|
return VALID
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2016-06-03 08:34:20 +00:00
|
|
|
|
2014-12-19 14:15:29 +00:00
|
|
|
# True if the token's scope is a superset of required scopes,
|
|
|
|
# or the required scopes is empty.
|
2015-01-18 15:29:37 +00:00
|
|
|
def sufficient_scope?(token, scopes)
|
2014-12-19 14:15:29 +00:00
|
|
|
if scopes.blank?
|
|
|
|
# if no any scopes required, the scopes of token is sufficient.
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
# If there are scopes required, then check whether
|
|
|
|
# the set of authorized scopes is a superset of the set of required scopes
|
|
|
|
required_scopes = Set.new(scopes)
|
|
|
|
authorized_scopes = Set.new(token.scopes)
|
|
|
|
|
|
|
|
return authorized_scopes >= required_scopes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-02-03 05:53:27 +00:00
|
|
|
end
|