apple_auth 0.1.0 → 1.0.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
  SHA256:
3
- metadata.gz: 6c6e2838dce13def54be641c346b9618bb3e3bdc10288b98a2accb639e3ea107
4
- data.tar.gz: c6a16b50b7d59bdf788385d4718d6664e94d6206fbcb2febbe73e92ec4e25440
3
+ metadata.gz: 2325fb23f530c7f590aa0b2fa733af052923478e81a6ccc82c4d86963514a7da
4
+ data.tar.gz: 9551b96c8c9a1feebaa2ffc6f64fe9f66e6a36787d63805bff81c315a43897d4
5
5
  SHA512:
6
- metadata.gz: 18707eddc8b74cc651477b0464dd890c83b4d8c5acb3080d0943c5a6eb6fa5a2c8f808c4e2c8ca9ab9c757f89d1c2494a04f4ff044e5f5ae8484893a5745d6fb
7
- data.tar.gz: 8abc50e0cef074c7228ab3e7e2fe64822161f9b1c724e2b62877ecd8b23caf8862ff305d1b8025bbf0e8892a38559aef90c736aead513aacd4f801e9685cec2d
6
+ metadata.gz: aa21c140b1c060c38b6fa56f76339b3572f8d4e1d4deca9c761e1a58467950d89244937fa185d22872595ea49215200603efc06a50761712f82f7beac79574a6
7
+ data.tar.gz: bc1edcfca29b8176ecfec0fdf74c50065e1d1c313974a8bb8a441fa3300037c495b1ae013a71145e9d9def053de9ec002134289209250ad288b95baa87969dd9
data/.reek.yml CHANGED
@@ -126,3 +126,5 @@ detectors:
126
126
 
127
127
  exclude_paths:
128
128
  - config
129
+ - lib/generators/apple_auth/apple_auth_controller/templates
130
+
@@ -1,3 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - lib/generators/apple_auth/apple_auth_controller/templates/**
5
+
1
6
  Style/Documentation:
2
7
  Enabled: false
3
8
 
data/README.md CHANGED
@@ -1,16 +1,12 @@
1
1
  # AppleAuth
2
2
 
3
- [![CI](https://travis-ci.org/rootstrap/apple_sign_in.svg?branch=master)](https://travis-ci.org/rootstrap/apple_sign_in)
3
+ [![CI](https://api.travis-ci.org/rootstrap/apple_auth.svg?branch=master)](https://travis-ci.org/github/rootstrap/apple_auth)
4
4
  [![Maintainability](https://api.codeclimate.com/v1/badges/78453501221a76e3806e/maintainability)](https://codeclimate.com/github/rootstrap/apple_sign_in/maintainability)
5
5
  [![Test Coverage](https://api.codeclimate.com/v1/badges/78453501221a76e3806e/test_coverage)](https://codeclimate.com/github/rootstrap/apple_sign_in/test_coverage)
6
6
 
7
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/apple_auth`. To experiment with that code, run `bin/console` for an interactive prompt.
8
-
9
- TODO: Delete this and the text above, and describe your gem
10
-
11
7
  ## Installation
12
8
 
13
- Add this line to your application's Gemfile:
9
+ Add this line to your Gemfile:
14
10
 
15
11
  ```ruby
16
12
  gem 'apple_auth'
@@ -20,18 +16,18 @@ And then execute:
20
16
 
21
17
  $ bundle install
22
18
 
23
- Or install it yourself as:
19
+ Or install it yourself:
24
20
 
25
21
  $ gem install apple_auth
26
22
 
27
23
  ------------------
28
24
 
29
- After installing the gem, you need to run the generator.
25
+ After installing the gem, you need to run this generator.
30
26
 
31
27
  $ rails g apple_auth:config
32
28
 
33
- This will generate a new initializer: `apple_auth.rb` containing the following default configuration:
34
- ```
29
+ This will generate a new initializer: `apple_auth.rb` with the following default configuration:
30
+ ```ruby
35
31
  AppleAuth.configure do |config|
36
32
  # config.apple_client_id = <Your client_id in your Apple Developer account>
37
33
  # config.apple_private_key = <Your private key provided by Apple>
@@ -42,9 +38,11 @@ end
42
38
  ```
43
39
  Set your different credentials in the file by uncommenting the lines and adding your keys.
44
40
 
41
+ ------------------
42
+
45
43
  ## Usage
46
44
 
47
- This show you an example of a settings
45
+ Here's an example of how to configure the gem:
48
46
 
49
47
  ```ruby
50
48
  AppleAuth.configure do |config|
@@ -56,13 +54,13 @@ AppleAuth.configure do |config|
56
54
  end
57
55
  ```
58
56
 
59
- We strongly recommend to use environment variable when you add this values.
57
+ We strongly recommend to use environment variables for these values.
60
58
 
61
- Apple sign in workflow:
59
+ Apple sign-in workflow:
62
60
 
63
61
  ![alt text](https://docs-assets.developer.apple.com/published/360d59b776/rendered2x-1592224731.png)
64
62
 
65
- For more information, check the [Apple oficial documentation](https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api)
63
+ For more information, check the [Apple oficial documentation](https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api).
66
64
 
67
65
  Validate JWT token and get user information:
68
66
 
@@ -81,14 +79,39 @@ AppleAuth::UserIdentity.new(user_id, invalid_jwt_token).validate!
81
79
  >> AppleAuth::Conditions::JWTValidationError
82
80
  ```
83
81
 
84
- Verify user identity and get token:
82
+ Verify user identity and get access and refresh tokens:
85
83
 
86
84
  ```ruby
87
85
  code = 'cfb77c21ecd444390a2c214cd33decdfb.0.mr...'
88
- AppleAuth::Token.new(code).authenticate
86
+ AppleAuth::Token.new(code).authenticate!
89
87
  >> { access_token: "a7058d...", expires_at: 1595894672, refresh_token: "r8f1ce..." }
90
88
  ```
91
89
 
90
+ ## Using with Devise
91
+
92
+ If you are using devise_token_auth gem, run this generator.
93
+
94
+ $ rails g apple_sign_in:appple_auth_controller [scope]
95
+
96
+ In the scope you need to write your path from controllers to your existent devise controllers.
97
+ An example `$ rails g apple_auth:appple_auth_controller api/v1/`
98
+ This will generate a new controller: `controllers/api/v1/apple_auth_controller.rb`.
99
+
100
+ You should configure the route, you can wrap it in the devise_scope block like:
101
+ ```
102
+ devise_scope :user do
103
+ resource :user, only: %i[update show] do
104
+ controller :apple_auth do
105
+ post :apple_auth, on: :collection, to: 'apple_auth#create'
106
+ end
107
+ end
108
+ end
109
+ ```
110
+
111
+ ## Demo
112
+
113
+ You can find a full implementation of this gem in [this demo](https://github.com/rootstrap/apple-sign-in-rails).
114
+
92
115
  ## Development
93
116
 
94
117
  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.
@@ -97,7 +120,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
97
120
 
98
121
  ## Contributing
99
122
 
100
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/apple_sign_in. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/apple_auth/blob/master/CODE_OF_CONDUCT.md).
123
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rootstrap/apple_auth/issues. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/rootstrap/apple_auth/blob/master/CODE_OF_CONDUCT.md).
101
124
 
102
125
 
103
126
  ## License
@@ -106,11 +129,11 @@ The gem is available as open source under the terms of the [MIT License](https:/
106
129
 
107
130
  ## Code of Conduct
108
131
 
109
- Everyone interacting in the AppleAuth project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/apple_auth/blob/master/CODE_OF_CONDUCT.md).
132
+ Everyone interacting in the AppleAuth project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rootstrap/apple_auth/blob/master/CODE_OF_CONDUCT.md).
110
133
 
111
134
  ## Credits
112
135
 
113
- apple_auth is maintained by [Rootstrap](http://www.rootstrap.com) with the help of our
114
- [contributors](https://github.com/rootstrap/apple_sign_in/contributors).
136
+ apple_auth gem is maintained by [Rootstrap](http://www.rootstrap.com) with the help of our
137
+ [contributors](https://github.com/rootstrap/apple_auth/contributors).
115
138
 
116
139
  [<img src="https://s3-us-west-1.amazonaws.com/rootstrap.com/img/rs.png" width="100"/>](http://www.rootstrap.com)
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ['Timothy Peraza, Antonieta Alvarez, Martín Morón']
9
9
  spec.email = ['timothy@rootstrap.com, antonieta.alvarez@rootstrap.com, martin.jaime@rootstrap.com']
10
10
 
11
- spec.summary = 'Integration with Apple Sign In'
11
+ spec.summary = 'Integration with Apple Sign In and Devise for backend. Validate and Verify user token.'
12
12
  spec.homepage = 'https://github.com/rootstrap/apple_auth'
13
13
  spec.license = 'MIT'
14
14
  spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
38
38
  spec.add_development_dependency 'reek', '~> 5.6'
39
39
  spec.add_development_dependency 'rspec', '~> 3.9'
40
40
  spec.add_development_dependency 'rubocop', '~> 0.80'
41
+ spec.add_development_dependency 'parser', '~> 2.7.1.1'
41
42
  spec.add_development_dependency 'simplecov', '~> 0.17.1'
42
43
  spec.add_development_dependency 'webmock', '~> 3.8'
43
44
  end
@@ -27,3 +27,4 @@ require 'apple_auth/user_identity'
27
27
  require 'apple_auth/token'
28
28
 
29
29
  require 'generators/apple_auth/config/config_generator'
30
+ require 'generators/apple_auth/apple_auth_controller/apple_auth_controller_generator'
@@ -2,6 +2,6 @@
2
2
 
3
3
  module AppleAuth
4
4
  module Base
5
- VERSION = '0.1.0'
5
+ VERSION = '1.0.0'
6
6
  end
7
7
  end
@@ -12,7 +12,7 @@ module AppleAuth
12
12
  end
13
13
 
14
14
  # :reek:FeatureEnvy
15
- def authenticate
15
+ def authenticate!
16
16
  access_token = apple_access_token
17
17
  access_token.refresh! if access_token.expired?
18
18
 
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module AppleAuth
4
+ module Generators
5
+ class AppleAuthControllerGenerator < Rails::Generators::Base
6
+ source_root File.expand_path('templates', __dir__)
7
+ argument :scope, required: false, default: ''
8
+
9
+ def copy_apple_auth_controller_file
10
+ @scope_prefix = scope.blank? ? '' : scope.camelize
11
+ template 'apple_auth_controller.rb',
12
+ "app/controllers/#{scope}apple_auth_controller.rb"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= @scope_prefix %>AppleAuthController < DeviseTokenAuth::SessionsController
4
+ protect_from_forgery with: :null_session
5
+ skip_before_action :verify_authenticity_token
6
+ before_action :skip_session_storage
7
+ before_action :check_json_request
8
+
9
+ def create
10
+ apple_params = apple_validate
11
+ @resource = sign_in_with_apple(apple_params)
12
+ custom_sign_in
13
+ rescue AppleAuth::Conditions::JWTValidationError, OAuth2::Error, JWT::ExpiredSignature => e
14
+ render_error(:bad_request, e.message)
15
+ end
16
+
17
+ private
18
+
19
+ def apple_validate
20
+ data = AppleAuth::UserIdentity.new(
21
+ apple_sign_in_params[:user_identity],
22
+ apple_sign_in_params[:jwt]
23
+ ).validate!
24
+ AppleAuth::Token.new(apple_sign_in_params[:code]).authenticate!
25
+
26
+ data.slice(:email)
27
+ end
28
+
29
+ def custom_sign_in
30
+ sign_in(:api_v1_user, @resource)
31
+ new_auth_header = @resource.create_new_auth_token
32
+ response.headers.merge!(new_auth_header)
33
+ render_create_success
34
+ end
35
+
36
+ def sign_in_with_apple(user_params)
37
+ user = User.where(provider: 'apple', uid: user_params[:email]).first_or_create!
38
+ user.password = Devise.friendly_token[0, 20]
39
+ user.assign_attributes user_params.except('id')
40
+ user
41
+ end
42
+
43
+ def apple_sign_in_params
44
+ params.permit(:user_identity, :jwt, :code)
45
+ end
46
+
47
+ def check_json_request
48
+ return if request_content_type&.match?(/json/)
49
+
50
+ render json: { error: I18n.t('api.errors.invalid_content_type') }, status: :not_acceptable
51
+ end
52
+
53
+ def render_create_success
54
+ render json: { user: resource_data }
55
+ end
56
+
57
+ def render_error(status, message, _data = nil)
58
+ response = {
59
+ error: message
60
+ }
61
+ render json: response, status: status
62
+ end
63
+
64
+ def skip_session_storage
65
+ # Devise stores the cookie by default, so in api requests, it is disabled
66
+ # http://stackoverflow.com/a/12205114/2394842
67
+ request.session_options[:skip] = true
68
+ end
69
+
70
+ def request_content_type
71
+ request.content_type
72
+ end
73
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apple_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timothy Peraza, Antonieta Alvarez, Martín Morón
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-29 00:00:00.000000000 Z
11
+ date: 2020-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: '0.80'
139
+ - !ruby/object:Gem::Dependency
140
+ name: parser
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 2.7.1.1
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 2.7.1.1
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: simplecov
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -197,6 +211,8 @@ files:
197
211
  - lib/apple_auth/helpers/jwt_conditions.rb
198
212
  - lib/apple_auth/token.rb
199
213
  - lib/apple_auth/user_identity.rb
214
+ - lib/generators/apple_auth/apple_auth_controller/apple_auth_controller_generator.rb
215
+ - lib/generators/apple_auth/apple_auth_controller/templates/apple_auth_controller.rb
200
216
  - lib/generators/apple_auth/config/config_generator.rb
201
217
  - lib/generators/apple_auth/config/templates/config.rb
202
218
  homepage: https://github.com/rootstrap/apple_auth
@@ -224,5 +240,6 @@ requirements: []
224
240
  rubygems_version: 3.0.3
225
241
  signing_key:
226
242
  specification_version: 4
227
- summary: Integration with Apple Sign In
243
+ summary: Integration with Apple Sign In and Devise for backend. Validate and Verify
244
+ user token.
228
245
  test_files: []