Class: GcalMapper::Authentification

Inherits:
Object
  • Object
show all
Defined in:
lib/gcal_mapper/authentification.rb,
lib/gcal_mapper/authentification/base.rb,
lib/gcal_mapper/authentification/oauth2.rb,
lib/gcal_mapper/authentification/assertion.rb

Overview

Abstract which type of authentification is required

Defined Under Namespace

Classes: Assertion, Base, Oauth2

Constant Summary collapse

REQUEST_URL =

url where to request to authentificate

'https://accounts.google.com/o/oauth2/token'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, client_email = nil, user_email = nil, password = 'notasecret') ⇒ Authentification

intialize client info needed for connection to Oauth2.

Parameters:

  • file (String)

    path to the yaml or p12 file

  • client_email (String) (defaults to: nil)

    email from the api_consol (service account only)

  • user_email (String) (defaults to: nil)

    email from the impersonated user (service account only)

  • password (String) (defaults to: 'notasecret')

    p12 key password (service account only)

Raises:



21
22
23
24
25
26
27
# File 'lib/gcal_mapper/authentification.rb', line 21

def initialize(file, client_email=nil, user_email=nil, password='notasecret')
  @file = File.expand_path(file)
  @client_email = client_email
  @user_email = user_email
  @password = password
  raise GcalMapper::AuthFileError if !File.exist?(@file)
end

Instance Attribute Details

#client_emailObject (readonly)

for assertion authentification



12
13
14
# File 'lib/gcal_mapper/authentification.rb', line 12

def client_email
  @client_email
end

#fileObject (readonly)

file that is needed for authentification



11
12
13
# File 'lib/gcal_mapper/authentification.rb', line 11

def file
  @file
end

#passwordObject (readonly)

password for the p12 file



13
14
15
# File 'lib/gcal_mapper/authentification.rb', line 13

def password
  @password
end

Instance Method Details

#access_tokenstring

Gives the access token

Returns:

  • (string)

    the access token



45
46
47
# File 'lib/gcal_mapper/authentification.rb', line 45

def access_token
  @auth.access_token
end

#authenticateBool

do the authentification for one of the right authentification method

Returns:

  • (Bool)

    true if instantiation ok



32
33
34
35
36
37
38
39
40
# File 'lib/gcal_mapper/authentification.rb', line 32

def authenticate
  if client_email==nil
    @auth = Authentification::Oauth2.new(@file)
  else
    @auth = Authentification::Assertion.new(@file, @client_email, @user_email, @password)
  end

  !access_token.nil?
end

#refresh_tokenstring

refresh the access token

Returns:

  • (string)

    the access token



52
53
54
# File 'lib/gcal_mapper/authentification.rb', line 52

def refresh_token
  @auth.refresh_token
end