Class: SK::SDK::Oauth

Inherits:
Object
  • Object
show all
Defined in:
lib/sk_sdk/oauth.rb

Overview

Authenticate your SalesKing App using oAuth2. This class provides helpers to create the token & dialog url and build the params to get an access token.

Example

Using httparty gem:

require 'sk_sdk/oauth'
require 'httparty'

auth = SK::SDK::Oauth.new(sk_app_settings)
resp = HTTParty.post( auth.token_url,
                      body: auth.token_params(code),
                      basic_auth: auth.basic_params )

Of course you can use curb or any other http lib. Just make sure to read their docs about POST params, HTTP BASIC Auth and https handling

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Oauth

Setup a new oAuth connection requires you to set some default:

@param opts containing id, secrete, scope, url of

your app

provided his subdomain

Parameters:

  • [String] (Hash)

    a customizable set of options

  • [String|Array[String]] (Hash)

    a customizable set of options



36
37
38
39
40
41
42
43
44
# File 'lib/sk_sdk/oauth.rb', line 36

def initialize(opts)
  @id           = opts['id']
  @secret       = opts['secret']
  @scope        = opts['scope']
  @redirect_url = opts['redirect_url']
  @canvas_slug  = opts['canvas_slug']
  @sk_url       = opts['sk_url'] || "https://*.salesking.eu"
  @sub_domain   = opts['sub_domain']
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



21
22
23
# File 'lib/sk_sdk/oauth.rb', line 21

def id
  @id
end

#redirect_urlObject (readonly)

Returns the value of attribute redirect_url.



21
22
23
# File 'lib/sk_sdk/oauth.rb', line 21

def redirect_url
  @redirect_url
end

#secretObject (readonly)

Returns the value of attribute secret.



21
22
23
# File 'lib/sk_sdk/oauth.rb', line 21

def secret
  @secret
end

#sub_domainObject

Returns the value of attribute sub_domain.



22
23
24
# File 'lib/sk_sdk/oauth.rb', line 22

def sub_domain
  @sub_domain
end

Instance Method Details

#auth_dialogString

Returns URL with parameter to show the auth dialog to the user.

Returns:

  • (String)

    URL with parameter to show the auth dialog to the user



47
48
49
50
51
52
53
# File 'lib/sk_sdk/oauth.rb', line 47

def auth_dialog
  scope_string = Array === @scope ? @scope.join(' ') : @scope
  params = { :client_id   => @id,
             :redirect_uri=> @redirect_url,
             :scope       => scope_string }
  "#{sk_url}/oauth/authorize?#{to_url_params(params)}"
end

#basic_paramsObject

HTTP BASIC Auth Params used in the POST request to /token e.g with httparty



80
81
82
# File 'lib/sk_sdk/oauth.rb', line 80

def basic_params
  { username: @id, password: @secret }
end

#sk_api_urlString

Returns base api url my-sub.salesking.eu/api.

Returns:

  • (String)

    base api url my-sub.salesking.eu/api



85
86
87
# File 'lib/sk_sdk/oauth.rb', line 85

def sk_api_url
  "#{sk_url}/api"
end

#sk_canvas_urlString

Returns app’s canvas url inside SalesKing.

Returns:

  • (String)

    app’s canvas url inside SalesKing



56
57
58
# File 'lib/sk_sdk/oauth.rb', line 56

def sk_canvas_url
  "#{sk_url}/app/#{@canvas_slug}"
end

#sk_urlString

Each company has it’s own subdomain so the url must be dynamic. This is achieved by replacing the * with the subdomain in the instance if a sub_domain was given. Else the SalesKing domain MUST include the subdomain

Returns:

  • (String)

    url



93
94
95
96
# File 'lib/sk_sdk/oauth.rb', line 93

def sk_url
  return @sk_url unless sub_domain
  @sk_url.gsub('*', sub_domain).gsub(/\/\z/, '' )
end

#to_url_params(params_hash) ⇒ Object



98
99
100
# File 'lib/sk_sdk/oauth.rb', line 98

def to_url_params(params_hash)
  params_hash.map { |k,v| "#{CGI::escape k.to_s}=#{CGI::escape v.to_s}" }.join('&')
end

#token_params(code) ⇒ Object

Params used in the POST request to /token e.g see httparty example on top. Using the client_secret in the params is DEPRECATED. Instead use HTTP Basic Auth header with client_id:client_secret like provided by #basic_params @returns params used to get the real access-token

Parameters:

  • code (String)

    to exchange for the access token



72
73
74
75
76
77
# File 'lib/sk_sdk/oauth.rb', line 72

def token_params(code)
  { client_id: @id,
    grant_type: 'authorization_code',
    redirect_uri: CGI::escape(@redirect_url),
    code: code }
end

#token_urlObject

URL to get the access_token, used in the second step after you have requested the authorization and gotten a code The token url is located at /oauth/token



63
64
65
# File 'lib/sk_sdk/oauth.rb', line 63

def token_url
  "#{sk_url}/oauth/token"
end