Class: IndieAuth::TokenVerification

Inherits:
Object
  • Object
show all
Defined in:
lib/indie_auth/token_verification.rb,
lib/indie_auth/token_verification/version.rb

Defined Under Namespace

Classes: AccessTokenMissingError, ForbiddenUserError, IncorrectMeError, InsufficentScopeError, MissingDomainError, MissingTokenEndpointError

Constant Summary collapse

VERSION =
"0.2.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ TokenVerification

Returns a new instance of TokenVerification.



16
17
18
# File 'lib/indie_auth/token_verification.rb', line 16

def initialize(access_token)
  @access_token = access_token.to_s.strip.sub(/\ABearer\s*/, '')
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



14
15
16
# File 'lib/indie_auth/token_verification.rb', line 14

def access_token
  @access_token
end

Instance Method Details

#verify(desired_scope = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/indie_auth/token_verification.rb', line 20

def verify(desired_scope=nil)
  raise AccessTokenMissingError if access_token.nil? or access_token.empty?
  raise MissingDomainError if ENV.fetch('DOMAIN', nil).nil?
  raise MissingTokenEndpointError if ENV.fetch('TOKEN_ENDPOINT', nil).nil?

  response = validate_token
  raise ForbiddenUserError unless response.kind_of? Net::HTTPSuccess

  response_body = JSON.parse(response.body)
  if response_body.fetch('me', nil) != ENV['DOMAIN']
    raise IncorrectMeError, "Expected: '#{ENV['DOMAIN']}', Received: '#{response_body.fetch('me', nil)}'"
  end

  return true if desired_scope.nil?
  scope_included_in_response?(response_body, desired_scope)
end