Class: Simple::Oauth2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-oauth2.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props = {}) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simple-oauth2.rb', line 22

def initialize(props={})
  @client_id = props[:client_id]
  @client_secret = props[:client_secret]

  @redirect_uri = props[:redirect_uri]
  @base_uri = props[:base_uri]

  @auth_endpoint = props[:auth_endpoint]
  @token_endpoint = props[:token_endpoint]

  @access_token = props[:access_token]
  @refresh_token = props[:refresh_token]
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



10
11
12
# File 'lib/simple-oauth2.rb', line 10

def access_token
  @access_token
end

#auth_endpointObject

Returns the value of attribute auth_endpoint.



10
11
12
# File 'lib/simple-oauth2.rb', line 10

def auth_endpoint
  @auth_endpoint
end

#base_uriObject

Returns the value of attribute base_uri.



10
11
12
# File 'lib/simple-oauth2.rb', line 10

def base_uri
  @base_uri
end

#client_idObject

Returns the value of attribute client_id.



10
11
12
# File 'lib/simple-oauth2.rb', line 10

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



10
11
12
# File 'lib/simple-oauth2.rb', line 10

def client_secret
  @client_secret
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



10
11
12
# File 'lib/simple-oauth2.rb', line 10

def redirect_uri
  @redirect_uri
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



10
11
12
# File 'lib/simple-oauth2.rb', line 10

def refresh_token
  @refresh_token
end

#token_endpointObject

Returns the value of attribute token_endpoint.



10
11
12
# File 'lib/simple-oauth2.rb', line 10

def token_endpoint
  @token_endpoint
end

Instance Method Details

#exchange(code) ⇒ Object

Exchanges a verification code for an access token.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/simple-oauth2.rb', line 53

def exchange(code)
  params = Addressable::URI.new
  params.query_values = {
    type: 'web_server',
    client_id: @client_id,
    redirect_uri: @redirect_uri,
    client_secret: @client_secret,
    code: code
  }
  uri = "#{@base_uri}#{@token_endpoint}?#{params.query}"
  conn = Faraday.new(:url => uri)
  response = conn.post uri
  binding.pry
  Response.new(response.body)
end

#new_access_tokenObject

Returns a new access token.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/simple-oauth2.rb', line 37

def new_access_token
  params = Addressable::URI.new
  params.query_values = {
    type: 'refresh',
    refresh_token: @refresh_token,
    client_id: @client_id,
    redirect_uri: @redirect_uri,
    client_secret: @client_secret
  }
  url = "#{@base_uri}#{@token_endpoint}?#{params.query}"
  conn = Faraday.new(:url => url)
  response = conn.post url
  Response.new(response.body)
end

#redirect_urlObject

Returns redirect url.



70
71
72
73
74
75
76
77
78
# File 'lib/simple-oauth2.rb', line 70

def redirect_url
  params = Addressable::URI.new
  params.query_values = {
    type: 'web_server',
    client_id: @client_id,
    redirect_uri: @redirect_uri,
  }
  "#{@base_uri}#{@auth_endpoint}?#{params.query}"
end