Class: Gravel::APNS::AutoToken
- Inherits:
-
Object
- Object
- Gravel::APNS::AutoToken
- Defined in:
- lib/gravel/apns/auto_token.rb
Overview
Used internally to generate JWT tokens for APNS.
Instance Attribute Summary collapse
-
#key_id ⇒ String
readonly
The identifier for the APNS key.
-
#team_id ⇒ String
readonly
The identifier for the developer’s team.
Instance Method Summary collapse
-
#bearer_token ⇒ String
Generate a bearer token.
-
#initialize(team_id, key_id, key) ⇒ Gravel::APNS::AutoToken
constructor
Create a new AutoToken instance.
-
#token ⇒ String
Get the next token to use.
Constructor Details
#initialize(team_id, key_id, key) ⇒ Gravel::APNS::AutoToken
Create a new AutoToken instance.
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_id ⇒ String (readonly)
The identifier for the APNS key.
16 17 18 |
# File 'lib/gravel/apns/auto_token.rb', line 16 def key_id @key_id end |
#team_id ⇒ String (readonly)
The identifier for the developer’s team.
10 11 12 |
# File 'lib/gravel/apns/auto_token.rb', line 10 def team_id @team_id end |
Instance Method Details
#bearer_token ⇒ String
Generate a bearer token.
76 77 78 |
# File 'lib/gravel/apns/auto_token.rb', line 76 def bearer_token 'Bearer ' + token end |
#token ⇒ String
Get the next token to use.
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 |