Class: GoogleSAAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/google-sa-auth.rb,
lib/google-sa-auth/scope.rb,
lib/google-sa-auth/token.rb,
lib/google-sa-auth/client.rb

Defined Under Namespace

Classes: Client, Scope, Token

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ GoogleSAAuth

Returns a new instance of GoogleSAAuth.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/google-sa-auth.rb', line 9

def initialize(args)
  # Symbolize keys.
  args = args.inject({}){|item,(k,v)| item[k.to_sym] = v; item}

  # Remove unknown keys and make sure we have all the required keys.
  args.delete_if {|k,v| ![:email_address, :scope, :key, :password, :audience].include?(k)}
  [:email_address, :scope, :key].each do |required|
    raise RuntimeError, "Missing required argument key #{required}" unless args[required]
  end

  # Setup default password and audience.
  args[:password] ||= 'notasecret'
  args[:audience] ||= 'https://accounts.google.com/o/oauth2/token'

  # Create the claim set.
  self.claim_set = {:iss => args[:email_address], :aud => args[:audience]}

  # Determine the scope.
  if args[:scope].class == String
    if args[:scope] =~ /^http/i
      self.claim_set[:scope] = args[:scope]
    else
      self.claim_set[:scope] = GoogleSAAuth::Scope.new(args[:scope]).url
    end
  elsif args[:scope].class == Array
    scopes = args[:scope].each.collect do |scope|
      if scope =~ /http/i
        scope
      else
        GoogleSAAuth::Scope.new(scope).url
      end
    end
    self.claim_set[:scope] = scopes.join(' ')
  elsif args[:scope].class == Hash
    # Specify extension, e.g. => {:fusiontables => 'readonly'}
    scopes = []
    args[:scope].each do |scope,extension|
      if scope =~ /^http/i
        url = scope
      else
        url = GoogleSAAuth::Scope.new(scope).by_extension(extension)
      end
      scopes.push(url) unless url.nil?
    end
    self.claim_set[:scope] = scopes.join(' ')
  end

  # Set other attributes.
  self.pkcs12 = {:key => args[:key], :password => args[:password]}

  # Get our authorization.
  auth_token
  self
end

Instance Attribute Details

#claim_setObject

Returns the value of attribute claim_set.



8
9
10
# File 'lib/google-sa-auth.rb', line 8

def claim_set
  @claim_set
end

#pkcs12Object

Returns the value of attribute pkcs12.



8
9
10
# File 'lib/google-sa-auth.rb', line 8

def pkcs12
  @pkcs12
end

#tokenObject

Returns the value of attribute token.



8
9
10
# File 'lib/google-sa-auth.rb', line 8

def token
  @token
end

Instance Method Details

#auth_tokenObject



64
65
66
67
68
69
# File 'lib/google-sa-auth.rb', line 64

def auth_token
  # Make sure this token isn't expired.
  self.token = nil if self.token.nil? || self.token.expired?
  self.token ||= GoogleSAAuth::Token.new(jwt)
  self.token
end

#jwtObject



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/google-sa-auth.rb', line 76

def jwt
  # Make sure the jwt isn't already expired.
  @json_web_token = nil if @json_web_token.nil? || Time.now.to_i >= @json_web_token.claim_set[:exp]

  # (Re)create the JSON web token.
  @json_web_token ||= GoogleJWT.new(
    self.claim_set,
    self.pkcs12[:key],
    self.pkcs12[:password]
  )
  @json_web_token
end

#token_stringObject



71
72
73
74
# File 'lib/google-sa-auth.rb', line 71

def token_string
  # Return only the string for the token.
  auth_token.token
end