apple_id_token 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +56 -0
- data/Changelog.md +4 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/apple_id_token.gemspec +42 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/apple_id_token.rb +82 -0
- data/lib/apple_id_token/version.rb +3 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4939bceeb59fbaadb045a5fb1b52285d62791ed776af45a3a93b23eaf5bb8cdc
|
4
|
+
data.tar.gz: 2f6d35816d03f38bb2b20bb2fa7b2dc7a576a12b2083ee213691174bff6ceded
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e418ab733c53f46f448635b908f8b2784e2c73147785f1eb494b2b761c3204e81684d82364d6fc29336d39391bcb1911e7f500ec2bc5a91ce4c92e6c85403a94
|
7
|
+
data.tar.gz: 072ef93bf08e011f8b3fba113180fff018b2e885554367eeb42fa2aff54b4e96860644efd7dfb64c7d6cee1f09d9732822980e1977fc84b4806be6b84e621973
|
data/.gitignore
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
# Ignore Byebug command history file.
|
17
|
+
.byebug_history
|
18
|
+
|
19
|
+
## Specific to RubyMotion:
|
20
|
+
.dat*
|
21
|
+
.repl_history
|
22
|
+
build/
|
23
|
+
*.bridgesupport
|
24
|
+
build-iPhoneOS/
|
25
|
+
build-iPhoneSimulator/
|
26
|
+
|
27
|
+
## Specific to RubyMotion (use of CocoaPods):
|
28
|
+
#
|
29
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
30
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
31
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
32
|
+
#
|
33
|
+
# vendor/Pods/
|
34
|
+
|
35
|
+
## Documentation cache and generated files:
|
36
|
+
/.yardoc/
|
37
|
+
/_yardoc/
|
38
|
+
/doc/
|
39
|
+
/rdoc/
|
40
|
+
|
41
|
+
## Environment normalization:
|
42
|
+
/.bundle/
|
43
|
+
/vendor/bundle
|
44
|
+
/lib/bundler/man/
|
45
|
+
|
46
|
+
# for a library or gem, you might want to ignore these files since the code is
|
47
|
+
# intended to run in multiple environments; otherwise, check them in:
|
48
|
+
# Gemfile.lock
|
49
|
+
# .ruby-version
|
50
|
+
# .ruby-gemset
|
51
|
+
|
52
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
53
|
+
.rvmrc
|
54
|
+
|
55
|
+
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
56
|
+
# .rubocop-https?--*
|
data/Changelog.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2020 Samuel Villaescusa Vinader
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# AppleIdToken
|
2
|
+
|
3
|
+
This gem is a symple wrapper around Apple Sign In to validate provided tokens from https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens. You can also send tokens provided by official Apple library for iOS and Android applications.
|
4
|
+
|
5
|
+
We make use of JWT Ruby gem -> https://github.com/jwt/ruby-jwt to decode token provided by Apple and also it makes all the validations mentioned here -> https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/verifying_a_user to ensure integrity of provided token.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'apple_id_token'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install apple_id_token
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
To make use of the gem, just call `.validate` method of `AppleIdToken::Validator`.
|
26
|
+
You need to provide token issued by Apple and also your APP_ID generated here -> https://help.apple.com/developer-account/#/devde676e696 as audience.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
validator = AppleIdToken::Validator
|
30
|
+
begin
|
31
|
+
payload = validator.validate(token: token, aud: audience)
|
32
|
+
user_id = payload['sub']
|
33
|
+
email = payload['email']
|
34
|
+
rescue AppleIdToken::PublicKeysError => e
|
35
|
+
report "Provided keys are invalid: #{e}"
|
36
|
+
rescue AppleIdToken::ValidationError => e
|
37
|
+
report "Cannot validate: #{e}"
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
## Development
|
42
|
+
|
43
|
+
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.
|
44
|
+
|
45
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/PexegoUva/rails_apple_signin
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "apple_id_token/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "apple_id_token"
|
8
|
+
spec.version = AppleIdToken::VERSION
|
9
|
+
spec.authors = ["Samuel Villaescusa Vinader"]
|
10
|
+
spec.email = ["samuelvv22@gmail.com"]
|
11
|
+
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.summary = 'Apple Sign In Token utilities'
|
14
|
+
spec.description = 'Apple Sign In Token utilities; parse and check validity of token'
|
15
|
+
spec.homepage = "https://github.com/PexegoUva/rails_apple_signin"
|
16
|
+
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/PexegoUva/rails_apple_signin"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/PexegoUva/rails_apple_signin/blob/master/Changelog.md"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_runtime_dependency 'jwt', '>= 2.2.1'
|
36
|
+
spec.add_runtime_dependency 'httparty', '>= 0.17.3'
|
37
|
+
|
38
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
39
|
+
spec.add_development_dependency "rake", ">= 12.3.3"
|
40
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
41
|
+
spec.add_development_dependency 'fakeweb', ">= 1.3.0"
|
42
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "apple_id_token"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'apple_id_token/version'
|
2
|
+
require 'jwt'
|
3
|
+
require 'httparty'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module AppleIdToken
|
7
|
+
class PublicKeysError < StandardError; end
|
8
|
+
class ValidationError < StandardError; end
|
9
|
+
class JWTExpiredSignatureError < ValidationError; end
|
10
|
+
class InvalidPublicKeyError < ValidationError; end
|
11
|
+
class JWTSignatureError < ValidationError; end
|
12
|
+
class JWTAudienceError < ValidationError; end
|
13
|
+
|
14
|
+
class Validator
|
15
|
+
APPLE_ISSUER = 'https://appleid.apple.com'
|
16
|
+
APPLE_JWKS_URI = 'https://appleid.apple.com/auth/keys'
|
17
|
+
|
18
|
+
HTTP_OK = 200
|
19
|
+
|
20
|
+
JWT_RS256 = 'RS256'
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def validate(token:, aud:)
|
24
|
+
public_keys = get_public_keys
|
25
|
+
if public_keys
|
26
|
+
payload = check_against_certs(token, aud, public_keys)
|
27
|
+
|
28
|
+
unless payload
|
29
|
+
raise JWTSignatureError, 'Token not verified as issued by Apple'
|
30
|
+
end
|
31
|
+
else
|
32
|
+
raise PublicKeysError, 'Unable to retrieve Apple public keys'
|
33
|
+
end
|
34
|
+
|
35
|
+
payload
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def get_public_keys
|
41
|
+
response = HTTParty.get(APPLE_JWKS_URI)
|
42
|
+
return false unless response.code == HTTP_OK
|
43
|
+
|
44
|
+
json_body = JSON.parse(response.body)
|
45
|
+
json_body['keys']
|
46
|
+
end
|
47
|
+
|
48
|
+
def check_against_certs(token, aud, public_keys)
|
49
|
+
payload = nil
|
50
|
+
|
51
|
+
public_keys.each do |public_key|
|
52
|
+
# As jwk from jwt library needs Hashes with keys as symbols.
|
53
|
+
public_key = public_key.transform_keys(&:to_sym)
|
54
|
+
|
55
|
+
begin
|
56
|
+
jwk = JWT::JWK.import(public_key)
|
57
|
+
decoded_token = JWT.decode(token, jwk.public_key , !!public_key, {
|
58
|
+
algorithm: JWT_RS256,
|
59
|
+
iss: APPLE_ISSUER, verify_iss: true,
|
60
|
+
aud: aud, verify_aud: true
|
61
|
+
}
|
62
|
+
)
|
63
|
+
|
64
|
+
payload = decoded_token.first
|
65
|
+
rescue JWT::JWKError
|
66
|
+
raise InvalidPublicKeyError, 'Provided public key was invalid'
|
67
|
+
rescue JWT::ExpiredSignature
|
68
|
+
raise JWTExpiredSignatureError, 'Token signature is expired'
|
69
|
+
rescue JWT::InvalidIssuerError
|
70
|
+
raise JWTSignatureError, 'Token not verified as issued by Apple'
|
71
|
+
rescue JWT::InvalidAudError
|
72
|
+
raise JWTAudienceError, 'Token audience mismatch'
|
73
|
+
rescue JWT::DecodeError
|
74
|
+
nil # Try another public key.
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
payload
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: apple_id_token
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Villaescusa Vinader
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jwt
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.2.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.2.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: httparty
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.17.3
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.17.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.17'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.17'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 12.3.3
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 12.3.3
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: fakeweb
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 1.3.0
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 1.3.0
|
97
|
+
description: Apple Sign In Token utilities; parse and check validity of token
|
98
|
+
email:
|
99
|
+
- samuelvv22@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- Changelog.md
|
106
|
+
- Gemfile
|
107
|
+
- LICENSE
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- apple_id_token.gemspec
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/apple_id_token.rb
|
114
|
+
- lib/apple_id_token/version.rb
|
115
|
+
homepage: https://github.com/PexegoUva/rails_apple_signin
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata:
|
119
|
+
homepage_uri: https://github.com/PexegoUva/rails_apple_signin
|
120
|
+
source_code_uri: https://github.com/PexegoUva/rails_apple_signin
|
121
|
+
changelog_uri: https://github.com/PexegoUva/rails_apple_signin/blob/master/Changelog.md
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubygems_version: 3.0.3
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Apple Sign In Token utilities
|
141
|
+
test_files: []
|