Class: Gravel::APNS::AutoToken

Inherits:
Object
  • Object
show all
Defined in:
lib/gravel/apns/auto_token.rb

Overview

Used internally to generate JWT tokens for APNS.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(team_id, key_id, key) ⇒ Gravel::APNS::AutoToken

Create a new AutoToken instance.

Parameters:

  • team_id (String)

    The team identifier.

  • key_id (String)

    The key identifier.

  • key (OpenSSL::PKey::EC)

    The private key.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gravel/apns/auto_token.rb', line 25

def initialize(team_id, key_id, key)
  unless team_id.is_a?(String)
    raise 'The team identifier must be a string.'
  end

  unless key_id.is_a?(String)
    raise 'The key identifier must be a string.'
  end

  unless key_id.length == 10
    raise 'The key identifier does not appear to be valid.'
  end

  unless key.is_a?(OpenSSL::PKey::EC)
    raise 'The key must be an elliptic curve key.'
  end

  unless key.private_key?
    raise 'The key must contain a private key.'
  end

  @key = key
  @key_id = key_id
  @team_id = team_id
  @token_generation_mutex = Mutex.new
end

Instance Attribute Details

#key_idString (readonly)

The identifier for the APNS key.

Returns:

  • (String)

    The key identifier.



16
17
18
# File 'lib/gravel/apns/auto_token.rb', line 16

def key_id
  @key_id
end

#team_idString (readonly)

The identifier for the developer’s team.

Returns:

  • (String)

    The team identifier.



10
11
12
# File 'lib/gravel/apns/auto_token.rb', line 10

def team_id
  @team_id
end

Instance Method Details

#bearer_tokenString

Generate a bearer token.

Returns:

  • (String)

    A bearer token.



76
77
78
# File 'lib/gravel/apns/auto_token.rb', line 76

def bearer_token
  'Bearer ' + token
end

#tokenString

Get the next token to use.

Returns:

  • (String)

    The next token.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gravel/apns/auto_token.rb', line 56

def token
  if require_token_generation?
    @token_generation_mutex.synchronize do
      # Double check if we need to regenerate the token.
      # This could happen if two threads try to concurrently access
      # the token after it has expired (or before initial generation).
      if require_token_generation?
        @last_generated = time
        @token = generate_token
      end
    end
  end

  @token
end