motion-authentication 1.0.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: 88c921f20aa4544926a47cae648753a20dbbab4a
4
+ data.tar.gz: bb8c436ffc5f4116084f3540a7f5f7c53a5d2cbb
5
+ SHA512:
6
+ metadata.gz: f79627619ffade80b261dcb7038e85c96d98556baeef095402e0cccd79ddee84b35ee34c1ade5202ebc50389f8d00bcdf0ce5e39df9d8e2217d1c96d8b93aa81
7
+ data.tar.gz: 4c56cc20639741eac36e5868f4310a52fdbf68067c29a8edd4c540eb97a5a17fe68d9d404b1afd907d4fea2c8ea7eb4bb82a6a0636891f2c9f86f422d37a5db3
data/README.md ADDED
@@ -0,0 +1,112 @@
1
+ # motion-authentication
2
+
3
+ `motion-authentication` aims to provide a simple, standardized authentication helper for common authentication strategies.
4
+
5
+ Currently, this library only supports iOS, but could easily support other platforms. Please submit an issue (or PR :D) for the platform you would like to support.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's `Gemfile`, then run `bundle install`:
10
+
11
+ gem 'motion-authentication'
12
+
13
+ Next, run `rake pod:install` to install the CocoaPod dependencies.
14
+
15
+ ## Usage
16
+
17
+ Start by subclassing `Motion::Authentication` to create your own `Auth` class. Specify your authentication strategy and your sign in URL:
18
+
19
+ ```ruby
20
+ class Auth < Motion::Authentication
21
+ strategy DeviseTokenAuth
22
+ sign_in_url "http://localhost:3000/api/v1/users/sign_in"
23
+ end
24
+ ```
25
+
26
+ Available strategies:
27
+
28
+ * `DeviseTokenAuth` - This authentication strategy takes `email` and `password`, makes a POST request to the `sign_in_url`, and expects the response to include `email` and `token` keys in the JSON response object.
29
+
30
+ ### `.sign_in`
31
+
32
+ Using your `Auth` class, call `.sign_in` and pass a hash of credentials:
33
+
34
+ ```ruby
35
+ Auth.sign_in(email: email, password: password) do |result|
36
+ if result.success?
37
+ # authentication successful!
38
+ else
39
+ app.alert "Invalid login?"
40
+ end
41
+ end
42
+ ```
43
+
44
+ A successful sign in will securely store the current user's auth token.
45
+
46
+ ### `.signed_in?`
47
+
48
+ You can check if an auth token has previously been stored by using `signed_in?`. For example, in your App Delegate, you might want to open your sign in screen when the app is opened:
49
+
50
+ ```ruby
51
+ def on_load(options)
52
+ if Auth.signed_in?
53
+ open DashboardScreen
54
+ else
55
+ open SignInScreen
56
+ end
57
+ end
58
+ ```
59
+
60
+ ### `.authorization_header`
61
+
62
+ After signing in, and receiving the auth token, you will probably want to configure your API client to use your auth token in your API requests in an authorization header. Call `.authorization_header` to return the header value specific to the strategy that you are using. Two common places would be upon sign in, and when your app is launched.
63
+
64
+ ```ruby
65
+ # app_delegate.rb
66
+ def on_load(options)
67
+ if Auth.signed_in?
68
+ MyApiClient.update_auth_header(Auth.authorization_header)
69
+ # ...
70
+ end
71
+ end
72
+
73
+ # sign_in_screen.rb
74
+ def on_load(options)
75
+ Auth.sign_in(data) do |result|
76
+ if result.success?
77
+ MyApiClient.update_auth_header(Auth.authorization_header)
78
+ # ...
79
+ end
80
+ end
81
+ end
82
+ ```
83
+
84
+ ### `.sign_out`
85
+
86
+ At some point, you're going to need to sign out. This method will clear the stored auth token, but also allows you to pass a block to be called after the token has been cleared.
87
+
88
+ ```ruby
89
+ Auth.sign_out do
90
+ open HomeScreen
91
+ end
92
+ ```
93
+
94
+ ### `.current_user`
95
+
96
+ `motion-authentication` provides a `current_user` attribute. It has no effect on authentication, so you can do whatever you want with it.
97
+
98
+ ```ruby
99
+ Auth.sign_in(data) do |result|
100
+ if result.success?
101
+ Auth.current_user = User.new(result.object)
102
+ end
103
+ end
104
+ ```
105
+
106
+ ## Contributing
107
+
108
+ 1. Fork it
109
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
110
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
111
+ 4. Push to the branch (`git push origin my-new-feature`)
112
+ 5. Create new Pull Request
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ unless defined?(Motion::Project::Config)
4
+ raise "This file must be required within a RubyMotion project Rakefile."
5
+ end
6
+
7
+ require 'motion-cocoapods'
8
+ require 'motion-keychain'
9
+
10
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
11
+ Motion::Project::App.setup do |app|
12
+ app.files.unshift(Dir.glob(File.join(lib_dir_path, "project/**/*.rb")))
13
+ end
@@ -0,0 +1,33 @@
1
+ class Motion
2
+ class Authentication
3
+ class << self
4
+ attr_accessor :current_user
5
+
6
+ def strategy(val = nil)
7
+ @strategy = val unless val.nil?
8
+ @strategy ||= DeviseTokenAuth
9
+ end
10
+
11
+ def sign_in_url(val = nil)
12
+ @sign_in_url = val unless val.nil?
13
+ @sign_in_url
14
+ end
15
+
16
+ def sign_in(params, &block)
17
+ strategy.sign_in(sign_in_url, params, &block)
18
+ end
19
+
20
+ def authorization_header
21
+ strategy.authorization_header
22
+ end
23
+
24
+ def signed_in?
25
+ strategy.signed_in?
26
+ end
27
+
28
+ def sign_out(&block)
29
+ strategy.sign_out(&block)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ module Motion
2
+ class Authentication
3
+ class DeviseTokenAuth
4
+ class << self
5
+ def sign_in(sign_in_url, params, &block)
6
+ AFMotion::JSON.post(sign_in_url, user: params) do |response|
7
+ if response.success?
8
+ store_auth_tokens(response.object)
9
+ end
10
+ block.call(response)
11
+ end
12
+ end
13
+
14
+ def store_auth_tokens(data)
15
+ MotionKeychain.set :auth_email, data["email"]
16
+ MotionKeychain.set :auth_token, data["token"]
17
+ end
18
+
19
+ def authorization_header
20
+ token = MotionKeychain.get :auth_token
21
+ email = MotionKeychain.get :auth_email
22
+ %Q|Token token="#{token}", email="#{email}"|
23
+ end
24
+
25
+ def signed_in?
26
+ !! MotionKeychain.get(:auth_token)
27
+ end
28
+
29
+ def sign_out(&block)
30
+ MotionKeychain.remove :auth_email
31
+ MotionKeychain.remove :auth_token
32
+ block.call
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-authentication
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Havens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: motion-cocoapods
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: motion-keychain
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A simple, standardized authentication helper for common authentication
56
+ strategies for RubyMotion apps.
57
+ email:
58
+ - email@andrewhavens.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - README.md
64
+ - lib/motion-authentication.rb
65
+ - lib/project/motion-authentication.rb
66
+ - lib/project/strategies/devise_token_auth.rb
67
+ homepage: https://github.com/andrewhavens/motion-authentication
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.5.1
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: A simple, standardized authentication helper for common authentication strategies
91
+ for RubyMotion apps.
92
+ test_files: []