Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions lib/apple_auth/token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
module AppleAuth
class Token
APPLE_AUD = 'https://appleid.apple.com'
APPLE_CONFIG = AppleAuth.config
APPLE_CODE_TYPE = 'authorization_code'
APPLE_ALG = 'ES256'

def initialize(code)
def initialize(code, config: AppleAuth.config)
@code = code
@config = config
end

# :reek:FeatureEnvy
Expand All @@ -21,14 +21,14 @@ def authenticate!

private

attr_reader :code
attr_reader :code, :config

def apple_token_params
{
client_id: APPLE_CONFIG.apple_team_id,
client_id: config.apple_client_id,
client_secret: client_secret_from_jwt,
grant_type: APPLE_CODE_TYPE,
redirect_uri: APPLE_CONFIG.redirect_uri,
redirect_uri: config.redirect_uri,
code: code
}
end
Expand All @@ -40,18 +40,18 @@ def client_secret_from_jwt
def claims
time_now = Time.now.to_i
{
iss: APPLE_CONFIG.apple_team_id,
iss: config.apple_team_id,
iat: time_now,
exp: time_now + 10.minutes.to_i,
aud: APPLE_AUD,
sub: APPLE_CONFIG.apple_client_id
sub: config.apple_client_id
}
end

def claims_headers
{
alg: APPLE_ALG,
kid: AppleAuth.config.apple_key_id
kid: config.apple_key_id
}
end

Expand All @@ -62,8 +62,8 @@ def request_header
end

def gen_private_key
key = AppleAuth.config.apple_private_key
key = OpenSSL::PKey::EC.new(key) unless key.class == OpenSSL::PKey::EC
key = config.apple_private_key
key = OpenSSL::PKey::EC.new(key) unless key.is_a?(OpenSSL::PKey::EC)
key
end

Expand All @@ -89,10 +89,10 @@ def reponse_hash(access_token)
end

def apple_access_token
client = ::OAuth2::Client.new(APPLE_CONFIG.apple_client_id,
client = ::OAuth2::Client.new(config.apple_client_id,
client_secret_from_jwt,
client_urls)
client.auth_code.get_token(code, { redirect_uri: APPLE_CONFIG.redirect_uri }, {})
client.auth_code.get_token(code, { redirect_uri: config.redirect_uri }, {})
end
end
end