faas 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/faas.gemspec +17 -0
  3. data/lib/faas.rb +140 -0
  4. data/lib/faas/version.rb +3 -0
  5. metadata +73 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4574515ccd9512d022458a47cf96f8a730d7c625
4
+ data.tar.gz: 3bfe50e26d8220b859ed5143370ee9530a6923eb
5
+ SHA512:
6
+ metadata.gz: bdfba44c3ee2afb5167bb6a979b1554d214552f500ee67d6d281a7c267f960fd880f6e37497c615ffb19d9ed2c6997eed243250f12aea1323d1329abd6e406fc
7
+ data.tar.gz: ef86b6a6f62eecb90209ff6c0d4deaf4720bb29dc1916fa4bfe2a96676ee24ee5f0d77288dec8e1c4b1849101382aee989a38bd958c3b6c2c5a1346b0f244187
@@ -0,0 +1,17 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+ require 'faas/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'faas'
6
+ spec.version = Faas::VERSION.dup
7
+ spec.summary = 'Simplified integration with cloud services'
8
+ spec.description = 'Simplified integration with cloud services'
9
+ spec.email = 'devops@faas.io'
10
+ spec.authors = ['Steven Bull']
11
+ spec.homepage = 'http://github.com/faas/faas-lib-ruby'
12
+
13
+ spec.files = `git ls-files`.split("\n")
14
+
15
+ spec.add_runtime_dependency('faraday', '~> 0.8')
16
+ spec.add_runtime_dependency('multi_json', '~> 1.0')
17
+ end
@@ -0,0 +1,140 @@
1
+ require 'faraday'
2
+ require 'multi_json'
3
+
4
+ class Faas
5
+
6
+ SESSION_COOKIE_NAME = 'faas_cookie'.freeze
7
+
8
+
9
+ # Faas.config do |config|
10
+ # config.api_key = ENV['FAAS_API_KEY']
11
+ # config.api_secret = ENV['FAAS_API_SECRET']
12
+ # end
13
+ class Config
14
+ attr_accessor :api_key, :api_secret, :cookie_signing_secret
15
+ end
16
+ def config
17
+ @config ||= Config.new # TODO: thread safety?
18
+ if block_given?
19
+ yield(@config)
20
+ else
21
+ @config
22
+ end
23
+ end
24
+
25
+
26
+ class << self
27
+
28
+ def method_missing(symbol, *args, &block)
29
+ if @faas.respond_to?(symbol)
30
+ @faas.send(symbol, *args, &block)
31
+ else
32
+ super
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ def current_user(request)
39
+ cookie = request.cookies[SESSION_COOKIE_NAME]
40
+ cookie && !cookie.empty? && validate_cookie(cookie)
41
+ end
42
+
43
+
44
+ private
45
+
46
+ def cookie_signing_secret
47
+ unless @cookie_signing_secret_is_set
48
+ # TODO: Race condition / thread safety.
49
+ @cookie_signing_secret ||=
50
+ begin
51
+ if config.cookie_signing_secret
52
+ config.cookie_signing_secret
53
+ elsif config.api_key && config.api_secret
54
+ params = {
55
+ api_key: config.api_key,
56
+ # api_secret: config.api_secret,
57
+ app_secret: config.api_secret,
58
+ }
59
+ conn = Faraday.new('https://api-beta.faas.io/')
60
+ response = conn.post('bouncer/getCookieSharedSecret') do |request|
61
+ request.headers[:content_type] = 'application/json'
62
+ request.body = MultiJson.dump(params)
63
+ end
64
+ response_json = MultiJson.load(response.body)
65
+ response_json['data'] && response_json['data']['cookie_shared_secret']
66
+ else
67
+ nil
68
+ end
69
+ end
70
+ @cookie_signing_secret_is_set = true
71
+ end
72
+ @cookie_signing_secret
73
+ end
74
+
75
+ def validate_cookie(cookie)
76
+ obj = nil
77
+ val_sig = cookie[0,3] == 's:{' ? cookie[2..-1] : nil
78
+ val, dot, sig = val_sig.rpartition('.') if val_sig
79
+ if val && !val.empty?
80
+ if cookie_signing_secret
81
+ compare_sig = compute_signature(val)
82
+ if compare_sig == sig
83
+ trusted_val = val
84
+ end
85
+ else
86
+ raise "Unable to retrieve cookie signing secret: api_key is #{config.api_key && '' || 'NOT '}set, api_secret is #{config.api_secret && '' || 'NOT '}set."
87
+ # else # validateSignedCookie API - BAD :(
88
+ # params = {
89
+ # api_key: API_KEY,
90
+ # signed_cookie: CGI.escape(cookie),
91
+ # }
92
+ # conn = Faraday.new('https://api-beta.faas.io/')
93
+ # response = conn.post('bouncer/validateSignedCookie') do |request|
94
+ # request.headers[:content_type] = 'application/json'
95
+ # request.body = MultiJson.dump(params)
96
+ # end
97
+ # response_json = MultiJson.load(response.body)
98
+ # if response_json['result']
99
+ # trusted_val = val
100
+ # end
101
+ end
102
+ if trusted_val
103
+ obj = User.new(MultiJson.load(trusted_val)) # Require dependency
104
+ end
105
+ end
106
+ obj
107
+ end
108
+
109
+ def compute_signature(val)
110
+ key = cookie_signing_secret
111
+ if key
112
+ @digest ||= OpenSSL::Digest::Digest.new('sha256') # Dependencies?
113
+ sig = OpenSSL::HMAC.digest(@digest, key, val)
114
+ sig = Base64.encode64(sig).sub(/=*\s*$/,'')
115
+ sig
116
+ end
117
+ end
118
+
119
+
120
+ class User
121
+
122
+ def initialize(hash=nil)
123
+ @hash = hash || {}
124
+ end
125
+
126
+ def method_missing(symbol, *args)
127
+ key = symbol.to_s
128
+ if args.empty? && @hash.has_key?(symbol.to_s)
129
+ @hash[symbol.to_s]
130
+ else
131
+ super
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+
138
+ @faas = new
139
+
140
+ end
@@ -0,0 +1,3 @@
1
+ class Faas
2
+ VERSION = '0.0.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faas
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Steven Bull
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description: Simplified integration with cloud services
42
+ email: devops@faas.io
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - faas.gemspec
48
+ - lib/faas.rb
49
+ - lib/faas/version.rb
50
+ homepage: http://github.com/faas/faas-lib-ruby
51
+ licenses: []
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.0.3
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Simplified integration with cloud services
73
+ test_files: []