mobius-client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 01c4b81fee09290fed2e1935eabea312c6a18624
4
+ data.tar.gz: 6e74efa3ed49a49c1e999729f116a764146f01ab
5
+ SHA512:
6
+ metadata.gz: cc4271c42a49e56503aae69b79d160e771031431d29d8680c4504a899540a4f28317d7d6dc5a8ef1680c80764c30559987b93069e71b936fc314761da47519a9
7
+ data.tar.gz: ba78a18c7728a53f9df79c689d393638c7dd2460eae24f71f41659e20f5fd03317126d0e2ed4243e3c8163b647aaedb2f70a2166943e7bc4ee9019635711e21d
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
10
+ /coverage
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
15
+ # Ignore other unneeded files.
16
+ *.swp
17
+ *~
18
+ .project
19
+ .DS_Store
20
+ /.idea
21
+ .env
22
+ .env.*
23
+ /spec/examples.txt
24
+ dev-wallet.html
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,84 @@
1
+ ---
2
+ require: rubocop-rspec
3
+
4
+ AllCops:
5
+ Include:
6
+ - ./Gemfile
7
+ - ./config.ru
8
+ Exclude:
9
+ - db/**/*
10
+ - config/**/*
11
+ - script/**/*
12
+ - bin/*
13
+ - vendor/**/*
14
+ - lib/tasks/**/*
15
+ - tmp/**/*
16
+ TargetRubyVersion: 2.3
17
+
18
+ Documentation:
19
+ Enabled: false
20
+
21
+ Lint/AmbiguousBlockAssociation:
22
+ Enabled: false # this is a whole damned mess
23
+
24
+ Metrics/LineLength:
25
+ Max: 120
26
+
27
+ Naming/FileName:
28
+ Exclude:
29
+ - "*.*"
30
+
31
+ RSpec/FilePath:
32
+ Enabled: false
33
+
34
+ RSpec/InstanceVariable:
35
+ Enabled: false
36
+
37
+ RSpec/VerifiedDoubles:
38
+ Enabled: false
39
+
40
+ Style/Alias:
41
+ Enabled: false
42
+
43
+ Style/AndOr:
44
+ Enabled: false
45
+
46
+ Style/AsciiComments:
47
+ Enabled: false
48
+
49
+ Style/BlockComments:
50
+ Enabled: false
51
+
52
+ Style/ClassAndModuleChildren:
53
+ EnforcedStyle: compact
54
+
55
+ Style/DoubleNegation:
56
+ Enabled: false
57
+
58
+ Style/FrozenStringLiteralComment:
59
+ Enabled: false
60
+
61
+ Style/Lambda:
62
+ Enabled: false
63
+
64
+ Style/NumericLiterals:
65
+ Enabled: false
66
+
67
+ Style/PercentLiteralDelimiters:
68
+ Enabled: false
69
+
70
+ Style/RescueModifier:
71
+ Enabled: false
72
+
73
+ Style/StringLiterals:
74
+ EnforcedStyle: double_quotes
75
+
76
+ Style/SingleLineBlockParams:
77
+ Enabled: false
78
+
79
+ Style/ClassAndModuleChildren:
80
+ Enabled: false
81
+
82
+ Metrics/BlockLength:
83
+ Exclude:
84
+ - "spec/**/*.*"
data/.travis.yml ADDED
@@ -0,0 +1,22 @@
1
+ env:
2
+ global:
3
+ - CC_TEST_REPORTER_ID=b73e557e34c0841f1f534b98bb01a3346cdad5eb9913416dca9ec1350af6ac09
4
+
5
+ language: ruby
6
+ rvm:
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
10
+ cache: bundler
11
+ before_install:
12
+ - gem update --system && gem install bundler
13
+ before_script:
14
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
15
+ - chmod +x ./cc-test-reporter
16
+ - ./cc-test-reporter before-build
17
+ after_script:
18
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
19
+ script:
20
+ - bundle exec rake
21
+ - bundle exec bundle-audit update && bundle exec bundle-audit check
22
+ - bundle exec rubocop
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at gzigzigzeo@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mobius-client.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Viktor Sokolov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,259 @@
1
+ [![Build Status](https://travis-ci.org/mobius-network/mobius-client-ruby.svg?branch=master)](https://travis-ci.org/mobius-network/mobius-client-ruby)
2
+ [![Maintainability](https://api.codeclimate.com/v1/badges/a99a88d28ad37a79dbf6/maintainability)](https://codeclimate.com/github/codeclimate/codeclimate/maintainability)
3
+
4
+ # Mobius DApp Store Ruby SDK
5
+
6
+ The Mobius DApp Store Ruby SDK makes it easy to integrate Mobius DApp Store MOBI payments into any Ruby application.
7
+
8
+ A big advantage of the Mobius DApp Store over centralized competitors such as the Apple App Store or Google Play Store is significantly lower fees - currently 0% compared to 30% - for in-app purchases.
9
+
10
+ ## DApp Store Overview
11
+
12
+ The Mobius DApp Store will be an open-source, non-custodial "wallet" interface for easily sending crypto payments to apps. You can think of the DApp Store like https://stellarterm.com/ or https://www.myetherwallet.com/ but instead of a wallet interface it is an App Store interface.
13
+
14
+ The DApp Store is non-custodial meaning Mobius never holds the secret key of either the user or developer.
15
+
16
+ An overview of the DApp Store architecture is:
17
+
18
+ - Every application holds the private key for the account where it receives MOBI.
19
+ - An application specific unique account where a user deposits MOBI for use with the application is generated for each app based on the user's seed phrase.
20
+ - When a user opens an app through the DApp Store:
21
+ 1) Adds the application's public key as a signer so the application can access the MOBI and
22
+ 2) Signs a challenge transaction from the app with its secret key to authenticate that this user owns the account. This prevents a different person from pretending they own the account and spending the MOBI (more below under Authentication).
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'mobius-client'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself with:
37
+
38
+ $ gem install mobius-client
39
+
40
+ ### Setting up the developer's application account
41
+
42
+ Run:
43
+
44
+ $ mobius-cli create dapp-account
45
+
46
+ Creates a new Stellar account with 1,000 test-net MOBI.
47
+
48
+ You can also obtain free test network MOBI from https://mobius.network/friendbot
49
+
50
+ ### Setting up test user accounts
51
+
52
+ 1. Create empty Stellar account without a MOBI trustline.
53
+ ```
54
+ $ mobius-cli create account
55
+ ```
56
+ 2. Create stellar account with 1,000 test-net MOBI
57
+ ```
58
+ $ mobius-cli create dapp-account
59
+ ```
60
+ 3. Create stellar account with 1,000 test-net MOBI and the specified application public key added as a signer
61
+ ```
62
+ $ mobius-cli create dapp-account -a <Your application public key>
63
+ ```
64
+
65
+ ### Account Creation Wizard
66
+
67
+ Below command will create and setup the 4 account types above for testing and generate a simple HTML test interface that simulates the DApp Store authentication functionality (obtaining a challenge request from an app, signing it, and then openining the specified app passing in a JWT encoded token the application will use to verify this request is from the user that owns the specified MOBI account).
68
+
69
+ ```
70
+ $ mobius-cli create dev-wallet
71
+ ```
72
+
73
+ ## Authentication
74
+
75
+ ### Explanation
76
+
77
+ When a user opens an app through the DApp Store it tells the app what Mobius account it should use for payment.
78
+
79
+ The application needs to ensure that the user actually owns the secret key to the Mobius account and that this isn't a replay attack from a user who captured a previous request and is replyaing it.
80
+
81
+ This authentication is accomplished through the following process:
82
+
83
+ * When the user opens an app in the DApp Store it requests a challenge from the application.
84
+ * The challenge is a payment transaction of 1 XLM from and to the application account. It is never sent to the network - it is just used for authentication.
85
+ * The application generates the challenge transaction on request, signs it with itss own private key, and sends it to user.
86
+ * User receives the challenge transaction, verifies it is signed by the application's secret key by checking it the application's published public key that it receives through the DApp Store, and then signs the transaction which its own private key and sends it back to application along with its public key.
87
+ * Application checks that challenge transaction is now signed by itself and the public key that was passed in. Time bounds are also checked to make sure this isn't a replay attack. If everything passes the server replies with a token the application can pass in to "login" with the specified public key and use it for payment (it would have previously given the app access to the public key by adding the app's public key as a signer).
88
+
89
+ Note: the challenge transaction also has time bounds to restrict the time window when it can be used.
90
+
91
+ See demo at:
92
+
93
+ $ git clone git@github.com/mobius-network/mobius-client-ruby.git
94
+ $ cd mobius-client-ruby && bundle
95
+ $ cd examples/auth && bundle && ruby auth.rb
96
+
97
+ ### Sample Server Implementation
98
+
99
+ ```
100
+ class AuthController < ApplicationController
101
+ skip_before_action :verify_authenticity_token, :only => [:authenticate]
102
+
103
+ # GET /auth
104
+ # Generates and returns challenge transaction XDR signed by application to user
105
+ def challenge
106
+ render plain: Mobius::Client::Auth::Challenge.call(
107
+ Rails.application.secrets.app[:secret_key], # SA2VTRSZPZ5FIC.....I4QD7LBWUUIK
108
+ 12.hours # Session duration
109
+ )
110
+ end
111
+
112
+ # POST /auth
113
+ # Validates challenge transaction. It must be:
114
+ # - Signed by application and requesting user.
115
+ # - Not older than 10 seconds from now (see Mobius::Client.strict_interval`)
116
+ def authenticate
117
+ token = Mobius::Client::Auth::Token.new(
118
+ Rails.application.secrets.app[:secret_key], # SA2VTRSZPZ5FIC.....I4QD7LBWUUIK
119
+ params[:xdr], # Challenge transaction
120
+ params[:public_key] # User's public key
121
+ )
122
+
123
+ # Important! Otherwise, token will be considered valid.
124
+ token.validate!
125
+
126
+ # Converts issued token into JWT and sends it to user.
127
+ #
128
+ # Note: this is not the requirement. Instead of JWT, application might save token.hash along
129
+ # with time frame and public key to local database and validate over it.
130
+ render plain: Mobius::Client::Auth::Jwt.new(
131
+ Rails.application.secrets.app[:jwt_secret]
132
+ ).encode(token)
133
+ rescue Mobius::Client::Error::Unauthorized
134
+ # Signatures are invalid
135
+ render plain: "Access denied!"
136
+ rescue Mobius::Client::Error::TokenExpired
137
+ # Current time is outside session time bounds
138
+ render plain: "Session expired!"
139
+ rescue Mobius::Client::Error::TokenTooOld
140
+ # Challenge transaction was issued more than 10 seconds ago
141
+ render plain: "Challenge tx expired!"
142
+ end
143
+ end
144
+ ```
145
+
146
+ ## Payment
147
+
148
+ ### Explanation
149
+
150
+ After the user completes the authentication process they have a token T. They now pass it to the application to "login" which tells the application which Mobius account to withdraw MOBI from (the user public key) when a payment is needed. For a web application the token is generally passed in via a `token` request parameter. Upon opening the website/loading the application it checks that the token is valid (within time bounds etc) and the account in the token has added the app as a signer so it can withraw MOBI from it.
151
+
152
+
153
+ See demo at:
154
+
155
+ $ git clone git@github.com/mobius-network/mobius-client-ruby.git
156
+ $ cd mobius-client-ruby && bundle
157
+ $ cd examples/app && bundle && ruby app.rb
158
+
159
+ ### Sample Server Implementation
160
+
161
+ ```
162
+ class AppController < ApplicationController
163
+ skip_before_action :verify_authenticity_token, :only => [:pay]
164
+
165
+ ROUND_PRICE = 5
166
+
167
+ # GET /
168
+ # User opens the application passing in the token variable.
169
+ def index
170
+ # User has opened application page without a token
171
+ return render plain: "Visit https://store.mobius.network to register in the DApp Store" unless app
172
+
173
+ # User has not granted access to his MOBI account so we can't use it for payments
174
+ return render plain: "Visit https://store.mobius.network and open our app" unless app.authorized?
175
+
176
+ # token is valid - should render the application or redirect to the main application page etc
177
+ end
178
+
179
+ # GET /balance
180
+ def balance
181
+ render plain: app.balance
182
+ end
183
+
184
+ # POST /pay
185
+ def pay
186
+ app.pay(ROUND_PRICE)
187
+ render plain: app.balance
188
+ rescue Mobius::Client::Error::InsufficientFunds
189
+ render :gone
190
+ end
191
+
192
+ private
193
+
194
+ def token_s
195
+ session[:token] = params[:token] || session[:token]
196
+ end
197
+
198
+ def token
199
+ @token ||= Mobius::Client::Auth::Jwt.new(Rails.application.secrets.app[:jwt_secret]).decode!(token_s)
200
+ rescue Mobius::Client::Error
201
+ nil # We treat all invalid tokens as missing
202
+ end
203
+
204
+ def app
205
+ @app ||= token && Mobius::Client::App.new(
206
+ Rails.application.secrets.app[:secret_key], # SA2VTRSZPZ5FIC.....I4QD7LBWUUIK
207
+ token.public_key # Current user
208
+ )
209
+ end
210
+ end
211
+ ```
212
+
213
+ ## Sample Application
214
+
215
+ [Flappy Bird](https://github.com/mobius-network/flappy-bird-dapp) has been reimplemented using this new arhictecture and the above simple server code!
216
+
217
+ ## CLI Test Implementation
218
+
219
+ Normally, as mentioned the Mobius DApp Store will request a challenge, validate and sign it, pass it back to the application to obtain an access token, and then open the application and pass in the token.
220
+
221
+ For development purposes you can use the simple HTML test interface generated via `mobius-cli create dev-wallet` as mentioned above in the "Account Creation Wizard" section or you can use the these CLI commands.
222
+
223
+ ```
224
+ # Fetch token from working application
225
+ # mobius-cli auth fetch <URL> <User secret> <App public>
226
+ $ mobius-cli auth fetch -j secret \
227
+ http://localhost:4567/auth SA2VTRSZPZ5FIC.....I4QD7LBWUUIK GCWYXW7RXJ5.....SV4AK32ECXFJ
228
+
229
+ # Generate token locally using the provided app secret
230
+ # mobius-cli auth token <User secret> <App secret>
231
+ $ mobius-cli auth token -j secret \
232
+ SA2VTRSZPZ5FIC.....I4QD7LBWUUIK SGZKDAKASDSD.....I4QD7LBWUUIK
233
+ ```
234
+
235
+ Use `-j` if you want to return JWT token, otherwise transaction hash will be returned.
236
+
237
+ Check `lib/mobius/cli/auth.rb` for details.
238
+
239
+ ## Documentation
240
+
241
+ [[RDoc.info](http://www.rubydoc.info/github/mobius-network/mobius-client-ruby/master)]
242
+
243
+ ## Development
244
+
245
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
246
+
247
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
248
+
249
+ ## Contributing
250
+
251
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mobius-network/mobius-client-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
252
+
253
+ ## License
254
+
255
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
256
+
257
+ ## Code of Conduct
258
+
259
+ Everyone interacting in the Mobius::Client project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/mobius-client/blob/master/CODE_OF_CONDUCT.md).