-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from kdgm/validate-server-to-server-callbacks
Handle server to server notifications
- Loading branch information
Showing
11 changed files
with
310 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: false | ||
|
||
module AppleAuth | ||
class JWTDecoder | ||
APPLE_KEY_URL = 'https://appleid.apple.com/auth/keys'.freeze | ||
|
||
attr_reader :jwt | ||
|
||
def initialize(jwt) | ||
@jwt = jwt | ||
end | ||
|
||
def call | ||
decoded.first | ||
end | ||
|
||
private | ||
|
||
def decoded | ||
key_hash = apple_key_hash(jwt) | ||
apple_jwk = JWT::JWK.import(key_hash) | ||
JWT.decode(jwt, apple_jwk.public_key, true, algorithm: key_hash['alg']) | ||
end | ||
|
||
def apple_key_hash(jwt) | ||
response = Net::HTTP.get(URI.parse(APPLE_KEY_URL)) | ||
certificate = JSON.parse(response) | ||
matching_key = certificate['keys'].select { |key| key['kid'] == jwt_kid(jwt) } | ||
ActiveSupport::HashWithIndifferentAccess.new(matching_key.first) | ||
end | ||
|
||
def jwt_kid(jwt) | ||
header = JSON.parse(Base64.decode64(jwt.split('.').first)) | ||
header['kid'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: false | ||
|
||
module AppleAuth | ||
class JWTServerConditions | ||
include Conditions | ||
|
||
CONDITIONS = [ | ||
AudCondition, | ||
IatCondition, | ||
IssCondition | ||
].freeze | ||
|
||
attr_reader :decoded_jwt | ||
|
||
def initialize(decoded_jwt) | ||
@decoded_jwt = decoded_jwt | ||
end | ||
|
||
def validate! | ||
JWT::ClaimsValidator.new(decoded_jwt).validate! && jwt_conditions_validate! | ||
rescue JWT::InvalidPayload => e | ||
raise JWTValidationError, e.message | ||
end | ||
|
||
private | ||
|
||
def jwt_conditions_validate! | ||
conditions_results = CONDITIONS.map do |condition| | ||
condition.new(decoded_jwt).validate! | ||
end | ||
conditions_results.all? { |value| value == true } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module AppleAuth | ||
class ServerIdentity | ||
attr_reader :jwt | ||
|
||
def initialize(jwt) | ||
@jwt = jwt | ||
end | ||
|
||
def validate! | ||
token_data = JWTDecoder.new(jwt).call | ||
|
||
JWTServerConditions.new(token_data).validate! | ||
|
||
token_data.symbolize_keys | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe AppleAuth::JWTServerConditions do | ||
let(:jwt_sub) { '820417.faa325acbc78e1be1668ba852d492d8a.0219' } | ||
let(:jwt_iss) { 'https://appleid.apple.com' } | ||
let(:jwt_aud) { 'com.apple_auth' } | ||
let(:jwt_iat) { Time.now.to_i } | ||
let(:jwt) do | ||
{ | ||
iss: jwt_iss, | ||
aud: jwt_aud, | ||
iat: jwt_iat, | ||
events: '{ | ||
"type": "email-enabled", | ||
"sub": "820417.faa325acbc78e1be1668ba852d492d8a.0219", | ||
"email": "[email protected]", | ||
"is_private_email": "true", | ||
"event_time": 1508184845 | ||
}' | ||
} | ||
end | ||
|
||
let(:decoded_jwt) { ActiveSupport::HashWithIndifferentAccess.new(jwt) } | ||
|
||
before do | ||
AppleAuth.config.apple_client_id = 'com.apple_auth' | ||
end | ||
|
||
subject(:jwt_conditions_helper) { described_class.new(decoded_jwt) } | ||
|
||
context '#valid?' do | ||
context 'when decoded jwt attributes are valid' do | ||
it 'returns true' do | ||
expect(jwt_conditions_helper.validate!).to eq(true) | ||
end | ||
end | ||
|
||
context 'when jwt has incorrect type attributes' do | ||
context 'when iat is not a integer' do | ||
let(:jwt_iat) { Time.now } | ||
|
||
it 'raises an exception' do | ||
expect { jwt_conditions_helper.validate! }.to raise_error( | ||
AppleAuth::Conditions::JWTValidationError | ||
) | ||
end | ||
end | ||
end | ||
|
||
context 'when jwt_aud is different to apple_client_id' do | ||
let(:jwt_aud) { 'net.apple_auth' } | ||
|
||
it 'raises an exception' do | ||
expect { jwt_conditions_helper.validate! }.to raise_error( | ||
AppleAuth::Conditions::JWTValidationError, 'jwt_aud is different to apple_client_id' | ||
) | ||
end | ||
end | ||
|
||
context 'when jwt_iss is different to apple_iss' do | ||
let(:jwt_iss) { 'https://appleid.apple.net' } | ||
|
||
it 'raises an exception' do | ||
expect { jwt_conditions_helper.validate! }.to raise_error( | ||
AppleAuth::Conditions::JWTValidationError, 'jwt_iss is different to apple_iss' | ||
) | ||
end | ||
end | ||
|
||
context 'when jwt_iat is greater than now' do | ||
let(:jwt_iat) { (Time.now + 5.minutes).to_i } | ||
|
||
it 'raises an exception' do | ||
expect { jwt_conditions_helper.validate! }.to raise_error( | ||
AppleAuth::Conditions::JWTValidationError, 'jwt_iat is greater than now' | ||
) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.