sk_sdk 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/sk_sdk/oauth.rb +54 -0
- data/lib/sk_sdk/signed_request.rb +50 -0
- data/lib/sk_sdk.rb +5 -0
- data/spec/oauth_spec.rb +0 -0
- metadata +115 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Georg Leciejewski
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= SalesKing SDK
|
2
|
+
|
3
|
+
A bunch of classes to get a quick start creating SalesKing App's, using oAuth and
|
4
|
+
the API.
|
5
|
+
|
6
|
+
Still in a very early stage
|
7
|
+
|
8
|
+
== Install
|
9
|
+
|
10
|
+
|
11
|
+
gem install sk_sdk
|
12
|
+
|
13
|
+
Dependencies (gem's):
|
14
|
+
|
15
|
+
* activeresupport
|
16
|
+
* curb
|
17
|
+
|
18
|
+
Copyright (c) 2011 Georg Leciejewski, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "sk_sdk"
|
10
|
+
gem.summary = %Q{SalesKing SDK Ruby}
|
11
|
+
gem.description = %Q{Connect your business world with SalesKing. This gem gives ruby developers a jump-start for building SalesKing Business Apps. Under the hood it provides classes to handle oAuth, make RESTfull API requests and parses JSON Schema }
|
12
|
+
gem.email = "gl@salesking.eu"
|
13
|
+
gem.homepage = "http://github.com/salesking/sk_sdk"
|
14
|
+
gem.authors = ["Georg Leciejewski"]
|
15
|
+
gem.add_dependency 'curb'
|
16
|
+
gem.add_dependency 'activesupport'
|
17
|
+
# gem.add_dependency 'sk_api_schema'
|
18
|
+
# gem.add_dependency 'sk_api_builder'
|
19
|
+
# gem.add_dependency 'activeresource'
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
end
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Default: run specs.'
|
28
|
+
task :default => :spec
|
29
|
+
|
30
|
+
spec_files = Rake::FileList["spec/**/*_spec.rb"]
|
31
|
+
|
32
|
+
desc "Run specs"
|
33
|
+
Spec::Rake::SpecTask.new do |t|
|
34
|
+
t.spec_files = spec_files
|
35
|
+
t.spec_opts = ["-c"]
|
36
|
+
end
|
37
|
+
|
38
|
+
desc "Generate code coverage"
|
39
|
+
Spec::Rake::SpecTask.new(:coverage) do |t|
|
40
|
+
t.spec_files = spec_files
|
41
|
+
t.rcov = true
|
42
|
+
t.rcov_opts = ['--exclude', 'spec,/var/lib/gems']
|
43
|
+
end
|
44
|
+
|
45
|
+
desc 'Generate documentation.'
|
46
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = 'SalesKing SDK'
|
49
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
50
|
+
rdoc.rdoc_files.include('README')
|
51
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
52
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/lib/sk_sdk/oauth.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'curb'
|
3
|
+
module SK::SDK
|
4
|
+
# Authenticate your SalesKing App using oAuth2. This class holds common methods
|
5
|
+
class Oauth
|
6
|
+
|
7
|
+
attr_reader :app_id, :app_secret, :app_redirect_url
|
8
|
+
attr_accessor :sub_domain
|
9
|
+
|
10
|
+
def initialize(opts)
|
11
|
+
@app_id = opts['app_id']
|
12
|
+
@app_secret = opts['app_secret']
|
13
|
+
@app_scope = opts['app_scope']
|
14
|
+
@app_redirect_url = opts['app_redirect_url']
|
15
|
+
@app_canvas_slug = opts['app_canvas_slug']
|
16
|
+
@sk_url = opts['sk_url']
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def auth_dialog
|
21
|
+
# params = { :client_id => @app_id,
|
22
|
+
# :redirect_uri=> @app_redirect_url,
|
23
|
+
# :scope => @app_scope }
|
24
|
+
"#{sk_url}/oauth/authorize?client_id=#{@app_id}&redirect_uri=#{CGI::escape @app_redirect_url}&scope=#{CGI::escape @app_scope}"
|
25
|
+
end
|
26
|
+
|
27
|
+
# return the app's canvas url: the url inside SalesKing
|
28
|
+
def sk_canvas_url
|
29
|
+
"#{sk_url}/app/#{@app_canvas_slug}"
|
30
|
+
end
|
31
|
+
|
32
|
+
# Makes a GET request to the access_token endpoint in SK and receives the
|
33
|
+
# oauth/access token
|
34
|
+
def get_token(code)
|
35
|
+
# params = { :client_id => @app_id,
|
36
|
+
# :client_secret => @app_secret,
|
37
|
+
# :redirect_uri => @app_redirect_url,
|
38
|
+
# :code => code }
|
39
|
+
url = "#{sk_url}/oauth/access_token?code=#{code}&client_id=#{@app_id}&client_secret=#{@app_secret}&redirect_uri=#{CGI::escape @app_redirect_url }"
|
40
|
+
c = Curl::Easy.perform(url)
|
41
|
+
# grab token from response body, containing json string
|
42
|
+
ActiveSupport::JSON.decode(c.body_str)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Each company has it's own subdomain so the url must be dynamic.
|
46
|
+
# This is achived by replacing the * with the subdomain from the session
|
47
|
+
# === Returns
|
48
|
+
# <String>:: url
|
49
|
+
def sk_url
|
50
|
+
@sk_url.gsub('*', sub_domain)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require "active_support/json"
|
3
|
+
require 'openssl'
|
4
|
+
module SK::SDK
|
5
|
+
# Decode and validate signed requests which Salesking sends to canvas pages
|
6
|
+
# and PubSub subscribers
|
7
|
+
class SignedRequest
|
8
|
+
|
9
|
+
attr_reader :signed_request, :app_secret, :data, :payload, :sign
|
10
|
+
|
11
|
+
def initialize(signed_request, app_secret)
|
12
|
+
@signed_request = signed_request
|
13
|
+
@app_secret = app_secret
|
14
|
+
decode_payload
|
15
|
+
end
|
16
|
+
|
17
|
+
# Populate @data and @sign(ature) by splitting and decoding the incoming
|
18
|
+
# signed_request
|
19
|
+
def decode_payload
|
20
|
+
@sign, @payload = @signed_request.split('.')
|
21
|
+
@data = ActiveSupport::JSON.decode base64_url_decode(@payload)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Decode a base64URL encoded string: replace - with + and _ with /
|
25
|
+
# Also add padding so ruby's Base64 can decode it
|
26
|
+
# === returns
|
27
|
+
# <String>:: the plain string decoded
|
28
|
+
def base64_url_decode(str)
|
29
|
+
encoded_str = str.tr('-_', '+/')
|
30
|
+
encoded_str += '=' while !(encoded_str.size % 4).zero?
|
31
|
+
Base64.decode64(encoded_str)
|
32
|
+
end
|
33
|
+
|
34
|
+
# A request is valid if the new hmac created from the incoming string matches
|
35
|
+
# the new one, created with the apps secret
|
36
|
+
def valid?
|
37
|
+
return false if @data['algorithm'].to_s.upcase != 'HMAC-SHA256'
|
38
|
+
@sign == OpenSSL::HMAC.hexdigest('sha256', @app_secret, @payload)
|
39
|
+
end
|
40
|
+
|
41
|
+
def encode(str)
|
42
|
+
# base65 url encode the json, remove trailing-padding =
|
43
|
+
enc_str = [str].pack('m').tr('+/','-_').gsub("\n",'').gsub(/=+$/, '' )
|
44
|
+
# create hmac signature
|
45
|
+
hmac_sig = OpenSSL::HMAC.hexdigest('sha256',@app_secret, enc_str)
|
46
|
+
# glue together and return
|
47
|
+
[hmac_sig, enc_str].join('.')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/oauth_spec.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sk_sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Georg Leciejewski
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-09 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: curb
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: activesupport
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :development
|
62
|
+
version_requirements: *id003
|
63
|
+
description: "Connect your business world with SalesKing. This gem gives ruby developers a jump-start for building SalesKing Business Apps. Under the hood it provides classes to handle oAuth, make RESTfull API requests and parses JSON Schema "
|
64
|
+
email: gl@salesking.eu
|
65
|
+
executables: []
|
66
|
+
|
67
|
+
extensions: []
|
68
|
+
|
69
|
+
extra_rdoc_files:
|
70
|
+
- README.rdoc
|
71
|
+
files:
|
72
|
+
- MIT-LICENSE
|
73
|
+
- README.rdoc
|
74
|
+
- Rakefile
|
75
|
+
- VERSION
|
76
|
+
- lib/sk_sdk.rb
|
77
|
+
- lib/sk_sdk/oauth.rb
|
78
|
+
- lib/sk_sdk/signed_request.rb
|
79
|
+
- spec/oauth_spec.rb
|
80
|
+
has_rdoc: true
|
81
|
+
homepage: http://github.com/salesking/sk_sdk
|
82
|
+
licenses: []
|
83
|
+
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 3
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
version: "0"
|
107
|
+
requirements: []
|
108
|
+
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.5.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: SalesKing SDK Ruby
|
114
|
+
test_files:
|
115
|
+
- spec/oauth_spec.rb
|