farmbot-resource 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0965243df1655e7a1156774fbc842f4188aea81c
4
- data.tar.gz: 3548156c249e6696bfd977ce8ac0134ba6fadc13
3
+ metadata.gz: 4811aa1f4ed868f628643ed48f776a9ff9211ebe
4
+ data.tar.gz: b05169269961b8f17ccd290ba5c7adff7b6d2f44
5
5
  SHA512:
6
- metadata.gz: a60fe235033b33a52e71a1f9f651191345adc04a65b03ca961e5ffaeade3e5a7e27da086592f175f0fdd9ab8d185dd85c9ecbc83cfbf1a1ec4f9b3fa1b400e33
7
- data.tar.gz: 693fbbff6a7b75a1690c827d431f649b93c4fe6ef3bbda9848636864b0ec3d03551326370f87ad75d5a4fe17a2b6b2bcc6701c52ca90278865f23bff7b74da23
6
+ metadata.gz: 18b5028d03d05edd060254b7ed4355a9ac0daeab2c65107fef790df4862867e00ddde9e84831a772d4a07e981bd84f386f4ff93632a1891773a76686ebab9dd2
7
+ data.tar.gz: 63e0a8e50269ee87a8f52034ce3871e5014e2a9a3671c4d6147afa3c4e0bc2cfa6f50924a72db7a8fbb7f621039f7a08d0e50f1dd85375fc880e7a76b0146d2c
data/README.md CHANGED
@@ -42,7 +42,10 @@ schedules = client.schedules.all
42
42
  # Another use case: cache busting with fetch()
43
43
  # Results are always cached until fetch() is called
44
44
  sequences = client.sequences.fetch.all
45
-
46
45
  ```
47
46
 
47
+ # Encryption
48
+
49
+ The Farmbot web app issues an RSA public key. If you need to sign stuff, the key is accessible via `client.public_key`
50
+
48
51
  **See example.rb for a runnable sample**.
data/example.rb CHANGED
@@ -21,3 +21,18 @@ puts("Grabbing devices")
21
21
  puts client.device.current.to_s
22
22
  puts "Grabbing public key"
23
23
  puts client.public_key.to_pem
24
+ puts "Done using email/password. Attempting with credentials file now."
25
+ # Just a fake credentials file, as generated by Farmbot.
26
+ example = File.read("example_credentials.txt")
27
+ # An alternative means of generating a token without a password.
28
+ alt_token = FbResource::Client.get_token(credentials: example,
29
+ url: "http://localhost:3000")
30
+ alt_client = FbResource::Client.new do |config|
31
+ # Note for users that self host a Farmbot API:
32
+ # FbResource will grab the URL from the token's "ISS" claim.
33
+ config.token = token
34
+ end
35
+ puts("Grabbing devices with credentials")
36
+ puts client.device.current.to_s
37
+ puts "Grabbing public key with credentials"
38
+ puts client.public_key.to_pem
@@ -0,0 +1,6 @@
1
+ v5UA4zaAfXcZqXfIFVoWxewGELVhx5cTsQkK5hnj/JY9pGAy4PrmKHL07dqb
2
+ J8h+QNmeKvb0QxX0yZDCZEB2d6rP0ya15CU6qEzY8PRHMoZtvxD1OWN9Lwc4
3
+ O+Mo9sMKGVTSUrUWRhNA1qPA41vQ9iWXcUua48rfMvhN4dvcOwz6IkC9DaSu
4
+ 5CmbrtUfaVV+92qN318Qr+WAq/wCb/lX/bxHKU95n9WV4pgUKQKlUN5Cgf86
5
+ ptdq5YrDYhMwFz6F9FBRJ/tQ0gOynMyb2obSa/zJFim8NgxHMpbTPLZd7ZY5
6
+ uQp/DPjRjNEP4/tKNEQtwiPcG2U1i+28hG7MmEWsqw==
@@ -12,15 +12,18 @@ module FbResource
12
12
  post_config
13
13
  end
14
14
 
15
- def self.get_token(url: "http://my.farmbot.io", email:, password:)
15
+ def self.get_token(url: "http://my.farmbot.io",
16
+ email: "EMAIL NOT SET",
17
+ password: "PASSWORD NOT SET",
18
+ credentials: nil)
16
19
  # TODO handle auth errors in a more civilized manner.
17
20
  resource_url = url + "/api/tokens"
18
- payload = {user: {email: email, password: password}}
19
- result = RestClient.post(resource_url, payload)
20
- json = JSON.parse(result)
21
- token = json["token"]
22
- string = token["encoded"]
23
- string
21
+ if(credentials)
22
+ result = from_credentials(resource_url, credentials)
23
+ else
24
+ result = from_email_and_password(resource_url, email, password)
25
+ end
26
+ parse_token(result)
24
27
  end
25
28
 
26
29
  # Useful when you need to sign arbitrary secrets and give them to the server
@@ -51,7 +54,14 @@ module FbResource
51
54
  end
52
55
 
53
56
  def api_url
54
- JSON.parse(Base64.decode64(config.token.split(".")[1]))["iss"]
57
+ raw = config.token.split(".")[1]
58
+ token_string = Base64.decode64(raw)
59
+ hash = JSON.parse(token_string)
60
+ hash["iss"]
61
+ rescue JSON::ParserError
62
+ msg = "Farmbot Resource couldn't parse the auth token provided:\n\n"
63
+ msg += token_string + "\n\n"
64
+ raise InvalidConfig, msg
55
65
  end
56
66
 
57
67
  private
@@ -64,5 +74,22 @@ module FbResource
64
74
  invalidate("config needs `token` attribute") unless @config.token
65
75
  @config.url = api_url
66
76
  end
77
+
78
+ def self.from_credentials(resource_url, credentials)
79
+ payload = {user: {credentials: credentials}}
80
+ RestClient.post(resource_url, payload)
81
+ end
82
+
83
+ def self.from_email_and_password(resource_url, email, password)
84
+ payload = {user: {email: email, password: password}}
85
+ RestClient.post(resource_url, payload)
86
+ end
87
+
88
+ def self.parse_token(result)
89
+ json = JSON.parse(result)
90
+ token = json["token"]
91
+ string = token["encoded"]
92
+ string
93
+ end
67
94
  end
68
95
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: farmbot-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Carlino
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-25 00:00:00.000000000 Z
11
+ date: 2016-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -108,6 +108,7 @@ files:
108
108
  - README.md
109
109
  - Rakefile
110
110
  - example.rb
111
+ - example_credentials.txt
111
112
  - lib/client.rb
112
113
  - lib/config.rb
113
114
  - lib/farmbot-resource.rb