summaryrefslogtreecommitdiff
path: root/app/services/access_token_validation_service.rb
blob: 69449f3a445720f26e97e5b3babafd3e71e80597 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module 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

      elsif !self.sufficient_scope?(token, scopes)
        return INSUFFICIENT_SCOPE

      else
        return VALID
      end
    end

    # True if the token's scope contains any of the required scopes.
    def sufficient_scope?(token, required_scopes)
      if required_scopes.blank?
        true
      else
        # Check whether the token is allowed access to any of the required scopes.
        Set.new(required_scopes).intersection(Set.new(token.scopes)).present?
      end
    end
  end
end