Class: GcalMapper::Authentification::Assertion

Inherits:
Base
  • Object
show all
Defined in:
lib/gcal_mapper/authentification/assertion.rb

Overview

make the authentification for service account and request data from google calendar.

Constant Summary collapse

ASSERTION_SCOPE =

scope for assertion

'https://www.google.com/calendar/feeds/'
JWT_HEADER =

header of the jwt to send

{'alg' => 'RS256', 'typ' => 'JWT'}
ASSERTION_TYPE =

url to specify which type of assertion

'http://oauth.net/grant_type/jwt/1.0/bearer'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(p12_file, client_email, user_email, password) ⇒ Assertion

New object

Parameters:

  • p12_file (String)

    the path to the p12 key file

  • client_email (String)

    email from the api_consol

  • user_email (String)

    email from the impersonated user

  • password (String)

    p12 key password



24
25
26
27
28
29
30
31
# File 'lib/gcal_mapper/authentification/assertion.rb', line 24

def initialize(p12_file, client_email, user_email, password)
  @client_email = client_email
  @p12_file = p12_file
  @user_email = user_email
  @password = password
  request_token
  @validity = Time.now.getutc.to_i
end

Instance Attribute Details

#client_emailObject

the email given by google for the service account



15
16
17
# File 'lib/gcal_mapper/authentification/assertion.rb', line 15

def client_email
  @client_email
end

#user_emailObject

the email of the user to impersonate



16
17
18
# File 'lib/gcal_mapper/authentification/assertion.rb', line 16

def user_email
  @user_email
end

Instance Method Details

#access_tokenString

give the acess token for th application and refresh if it is outdated

Returns:

  • (String)

    access token



36
37
38
39
40
41
42
# File 'lib/gcal_mapper/authentification/assertion.rb', line 36

def access_token
  if Time.now.getutc.to_i - @validity > 3600
    refresh_token
  end

  @access_token
end

#refresh_tokenString

refresh the token by asking for a new one

Returns:

  • (String)

    access token



47
48
49
# File 'lib/gcal_mapper/authentification/assertion.rb', line 47

def refresh_token
  request_token
end