Class: OAuth2::Client

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

Constant Summary collapse

DEFAULTS_PATHS =
{
  :authorize_path     => '/oauth2/authorize',
  :token_path         => '/oauth2/token',
  :device_path        => '/oauth2/device/code',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, client_id, client_secret, options = {}) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
19
20
21
22
23
# File 'lib/oauth2/client.rb', line 14

def initialize(host, client_id, client_secret, options={})
  @host               = host
  @client_id          = client_id
  @client_secret      = client_secret
  @connection_options = options.fetch(:connection_options, {})
  @connection_client  = options.fetch(:connection_client, OAuth2::HttpConnection)
  DEFAULTS_PATHS.keys.each do |key|
    instance_variable_set(:"@#{key}", options.fetch(key, DEFAULTS_PATHS[key]))
  end
end

Instance Attribute Details

#authorize_pathObject

Returns the value of attribute authorize_path.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def authorize_path
  @authorize_path
end

#client_idObject

Returns the value of attribute client_id.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def client_secret
  @client_secret
end

#connection_clientObject

Returns the value of attribute connection_client.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def connection_client
  @connection_client
end

#connection_optionsObject

Returns the value of attribute connection_options.



4
5
6
# File 'lib/oauth2/client.rb', line 4

def connection_options
  @connection_options
end

#device_pathObject

Returns the value of attribute device_path.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def device_path
  @device_path
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/oauth2/client.rb', line 4

def host
  @host
end

#token_pathObject

Returns the value of attribute token_path.



5
6
7
# File 'lib/oauth2/client.rb', line 5

def token_path
  @token_path
end

Instance Method Details

#authorization_codeObject



51
52
53
# File 'lib/oauth2/client.rb', line 51

def authorization_code
  OAuth2::Grant::AuthorizationCode.new(self)
end

#client_credentialsObject



59
60
61
# File 'lib/oauth2/client.rb', line 59

def client_credentials
  OAuth2::Grant::ClientCredentials.new(self)
end

#connectionObject



71
72
73
# File 'lib/oauth2/client.rb', line 71

def connection
  @connection ||= @connection_client.new(@host, @connection_options)
end

#device_codeObject



67
68
69
# File 'lib/oauth2/client.rb', line 67

def device_code
  OAuth2::Grant::DeviceCode.new(self)
end

#implicitObject



47
48
49
# File 'lib/oauth2/client.rb', line 47

def implicit
  OAuth2::Grant::Implicit.new(self)
end

#normalize_scope(scope, sep = ' ') ⇒ Object

Token scope definitions vary by service e.g Google uses space delimited scopes, while Github uses comma seperated scopes. This method allows us to normalize the token scopes



40
41
42
43
44
45
# File 'lib/oauth2/client.rb', line 40

def normalize_scope(scope, sep=' ')
  unless (scope.is_a?(String) || scope.is_a?(Array))
    raise ArgumentError.new("Expected scope of type String or Array but was: #{scope.class.name}")
  end
  (scope.is_a?(Array) && scope.join(sep)) || scope
end

#passwordObject



63
64
65
# File 'lib/oauth2/client.rb', line 63

def password
  OAuth2::Grant::Password.new(self)
end

#refresh_tokenObject



55
56
57
# File 'lib/oauth2/client.rb', line 55

def refresh_token
  OAuth2::Grant::RefreshToken.new(self)
end