Class: Micropub::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/micropub/token.rb

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Token

Returns a new instance of Token.



8
9
10
# File 'lib/micropub/token.rb', line 8

def initialize(token)
  @token = token
end

Instance Method Details

#configObject



51
52
53
# File 'lib/micropub/token.rb', line 51

def config
  Micropub.configuration
end

#meObject



33
34
35
36
37
38
39
# File 'lib/micropub/token.rb', line 33

def me
  begin
    URI.parse(response["me"])
  rescue URI::InvalidURIError
    raise ValidationError, "The token endpoint did not contain a valid 'me' URI"
  end
end

#requestObject



47
48
49
# File 'lib/micropub/token.rb', line 47

def request
  HTTPClient.get(config.token_endpoint, {}, {'Authorization' => "Bearer #{@token}"})
end

#responseObject



41
42
43
44
45
# File 'lib/micropub/token.rb', line 41

def response
  @response ||= begin
    URI::decode_www_form(request.body).to_h
  end
end

#scopeObject



29
30
31
# File 'lib/micropub/token.rb', line 29

def scope
  response["scope"].split(",").map(&:to_sym)
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
# File 'lib/micropub/token.rb', line 12

def valid?
  begin
    valid_host? &&
    valid_scope?
  rescue ValidationError
    false
  end
end

#valid_host?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/micropub/token.rb', line 25

def valid_host?
  me.host == config.me.host
end

#valid_scope?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/micropub/token.rb', line 21

def valid_scope?
  (config.allowed_scopes & scope).any?
end