class AppleAuth::Token

Constants

APPLE_ALG
APPLE_AUD
APPLE_CODE_TYPE
APPLE_CONFIG

Attributes

code[R]

Public Class Methods

new(code) click to toggle source
# File lib/apple_auth/token.rb, line 10
def initialize(code)
  @code = code
end

Public Instance Methods

authenticate!() click to toggle source

:reek: FeatureEnvy

# File lib/apple_auth/token.rb, line 15
def authenticate!
  access_token = apple_access_token
  access_token.refresh! if access_token.expired?

  reponse_hash(access_token)
end

Private Instance Methods

apple_access_token() click to toggle source
# File lib/apple_auth/token.rb, line 91
def apple_access_token
  client = ::OAuth2::Client.new(APPLE_CONFIG.apple_client_id,
                                client_secret_from_jwt,
                                client_urls)
  client.auth_code.get_token(code, { redirect_uri: APPLE_CONFIG.redirect_uri }, {})
end
apple_token_params() click to toggle source
# File lib/apple_auth/token.rb, line 26
def apple_token_params
  {
    client_id: APPLE_CONFIG.apple_team_id,
    client_secret: client_secret_from_jwt,
    grant_type: APPLE_CODE_TYPE,
    redirect_uri: APPLE_CONFIG.redirect_uri,
    code: code
  }
end
claims() click to toggle source
# File lib/apple_auth/token.rb, line 40
def claims
  time_now = Time.now.to_i
  {
    iss: APPLE_CONFIG.apple_team_id,
    iat: time_now,
    exp: time_now + 10.minutes.to_i,
    aud: APPLE_AUD,
    sub: APPLE_CONFIG.apple_client_id
  }
end
claims_headers() click to toggle source
# File lib/apple_auth/token.rb, line 51
def claims_headers
  {
    alg: APPLE_ALG,
    kid: AppleAuth.config.apple_key_id
  }
end
client_secret_from_jwt() click to toggle source
# File lib/apple_auth/token.rb, line 36
def client_secret_from_jwt
  JWT.encode(claims, gen_private_key, APPLE_ALG, claims_headers)
end
client_urls() click to toggle source
# File lib/apple_auth/token.rb, line 70
def client_urls
  {
    site: APPLE_AUD,
    authorize_url: '/auth/authorize',
    token_url: '/auth/token'
  }
end
gen_private_key() click to toggle source
# File lib/apple_auth/token.rb, line 64
def gen_private_key
  key = AppleAuth.config.apple_private_key
  key = OpenSSL::PKey::EC.new(key) unless key.class == OpenSSL::PKey::EC
  key
end
reponse_hash(access_token) click to toggle source
# File lib/apple_auth/token.rb, line 78
def reponse_hash(access_token)
  token_hash = { access_token: access_token.token }

  expires = access_token.expires?
  if expires
    token_hash[:expires_at] = access_token.expires_at
    refresh_token = access_token.refresh_token
    token_hash[:refresh_token] = refresh_token if refresh_token
  end

  token_hash
end
request_header() click to toggle source
# File lib/apple_auth/token.rb, line 58
def request_header
  {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
end