Sha256: fd4adb1656a23c94198ed017fb84ba6e64442b7bedc6cc889d3fba0763f94540

Contents?: true

Size: 1.5 KB

Versions: 10

Compression:

Stored size: 1.5 KB

Contents

module AppleID
  class Client < OpenIDConnect::Client
    class Error < Rack::OAuth2::Client::Error; end

    attr_required :team_id, :key_id, :private_key

    def initialize(attributes)
      attributes_with_default = {
        authorization_endpoint: File.join(ISSUER, '/auth/authorize'),
        token_endpoint:         File.join(ISSUER, '/auth/token'),
        revocation_endpoint:    File.join(ISSUER, '/auth/revoke'),
      }.merge(attributes)
      super attributes_with_default
    end

    def access_token!(options = {})
      self.secret = client_secret_jwt
      super :body, options
    end

    def revoke!(options = {})
      self.secret = client_secret_jwt
      super :body, options
    end

    private

    def client_secret_jwt
      jwt = JSON::JWT.new(
        iss: team_id,
        aud: ISSUER,
        sub: identifier,
        iat: Time.now,
        exp: 1.minutes.from_now
      )
      jwt.kid = key_id
      jwt.sign(private_key)
    end

    def setup_required_scope(scopes)
      # NOTE:
      #  openid_connect gem add `openid` scope automatically.
      #  However, it's not required for Sign-in with Apple.
      scopes
    end

    def handle_success_response(response)
      token_hash = JSON.parse(response.body).with_indifferent_access
      AccessToken.new token_hash.delete(:access_token), token_hash.merge(client: self)
    end

    def handle_error_response(response)
      error = JSON.parse(response.body).with_indifferent_access
      raise Error.new(response.status, error)
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
apple_id-forestv8_compatibility-1.5.3 lib/apple_id/client.rb
apple_id-1.6.0 lib/apple_id/client.rb
apple_id-1.5.2 lib/apple_id/client.rb
apple_id-1.5.1 lib/apple_id/client.rb
apple_id-1.5.0 lib/apple_id/client.rb
apple_id-1.4.3 lib/apple_id/client.rb
apple_id-1.4.2 lib/apple_id/client.rb
apple_id-1.4.1 lib/apple_id/client.rb
apple_id-1.4.0 lib/apple_id/client.rb
apple_id-1.3.0 lib/apple_id/client.rb