Class: Mapkit

Inherits:
Object
  • Object
show all
Defined in:
lib/mapkit_token/app/models/mapkit.rb

Constant Summary collapse

@@instance =
Mapkit.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tokenObject



5
6
7
# File 'lib/mapkit_token/app/models/mapkit.rb', line 5

def self.token
  return @@instance
end

Instance Method Details

#generate(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mapkit_token/app/models/mapkit.rb', line 9

def generate *args
  return nil unless (creds = args.first).is_a?(Hash)

  auth_key = creds[:auth_key]
  auth_key_id = creds[:auth_key_id]
  apple_team_id = creds[:apple_team_id]
  base_url = creds[:base_url]
  lifespan_seconds = creds[:lifespan] ||= 1800

  header = {kid: auth_key_id, typ: "JWT", alg: "ES256"}
  payload = {iss: apple_team_id, iat: DateTime.now.to_i, exp: DateTime.now.to_i + lifespan_seconds, origin: base_url}.compact
  private_key = OpenSSL::PKey::EC.new(auth_key)
  return JWT.encode payload, private_key, algorithm='ES256', header
end