Class: Android::Publisher::Secrets

Inherits:
Object
  • Object
show all
Defined in:
lib/android/publisher/secrets.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Secrets

Intialize OAuth client settings.

Parameters:

  • options (Hash) (defaults to: {})

    Parsed client secrets files



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/android/publisher/secrets.rb', line 48

def initialize(options={})
  @flow = options[:flow] || options.keys.first.to_s || 'web'
  fdata = options[@flow]

  # Client auth config
  @client_id = fdata[:client_id] || fdata["client_id"]
  @client_secret = fdata[:client_secret] || fdata["client_secret"]

  # Redirects info
  @redirect_uris      = fdata[:redirect_uris]       || fdata["redirect_uris"]     || [fdata[:redirect_uri]]
  @javascript_origins = fdata[:javascript_origins]  || fdata["javascript_origins"]|| [fdata[:javascript_origin]]

  # Endpoints info
  @authorization_uri    = URI.parse(fdata[:auth_uri]  || fdata["auth_uri"]  || fdata[:authorization_uri])
  @token_credential_uri = URI.parse(fdata[:token_uri] || fdata["token_uri"] || fdata[:token_credential_uri])

  # Associated token info
  @access_token   = fdata[:access_token]  || fdata["access_token"]
  @refresh_token  = fdata[:refresh_token] || fdata["refresh_token"]
  @id_token       = fdata[:id_token]      || fdata["id_token"]
  @expires_in     = fdata[:expires_in]    || fdata["expires_in"]
  @expires_at     = fdata[:expires_at]    || fdata["expires_at"]
  @issued_at      = fdata[:issued_at]     || fdata["issued_at"]
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def access_token
  @access_token
end

#authorization_uriObject (readonly)

Returns the value of attribute authorization_uri.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def authorization_uri
  @authorization_uri
end

#client_idObject (readonly)

Returns the value of attribute client_id.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def client_secret
  @client_secret
end

#expires_atObject (readonly)

Returns the value of attribute expires_at.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def expires_at
  @expires_at
end

#expires_inObject (readonly)

Returns the value of attribute expires_in.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def expires_in
  @expires_in
end

#flowObject (readonly)

Returns the value of attribute flow.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def flow
  @flow
end

#id_tokenObject (readonly)

Returns the value of attribute id_token.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def id_token
  @id_token
end

#issued_atObject (readonly)

Returns the value of attribute issued_at.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def issued_at
  @issued_at
end

#javascript_originsObject (readonly)

Returns the value of attribute javascript_origins.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def javascript_origins
  @javascript_origins
end

#redirect_urisObject (readonly)

Returns the value of attribute redirect_uris.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def redirect_uris
  @redirect_uris
end

#refresh_tokenObject (readonly)

Returns the value of attribute refresh_token.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def refresh_token
  @refresh_token
end

#token_credential_uriObject (readonly)

Returns the value of attribute token_credential_uri.



73
74
75
# File 'lib/android/publisher/secrets.rb', line 73

def token_credential_uri
  @token_credential_uri
end

Class Method Details

.load(filename = nil) ⇒ Object

Examples:

{
  "web": {
    "client_id": "asdfjasdljfasdkjf",
    "client_secret": "1912308409123890",
    "redirect_uris": ["https://www.example.com/oauth2callback"],
    "auth_uri": "https://accounts.android.com/o/oauth2/auth",
    "token_uri": "https://accounts.android.com/o/oauth2/token"
  }
}
{
  "installed": {
    "client_id": "837647042410-75ifg...usercontent.com",
    "client_secret":"asdlkfjaskd",
    "redirect_uris": ["http://localhost", "urn:ietf:oauth:2.0:oob"],
    "auth_uri": "https://accounts.android.com/o/oauth2/auth",
    "token_uri": "https://accounts.android.com/o/oauth2/token"
  }
}


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/android/publisher/secrets.rb', line 28

def self.load(filename=nil)
  while filename == nil
    search_path ||= File.expand_path('.')
    if File.exist?(File.join(search_path, 'client_secrets.json'))
      filename = File.join(search_path, 'client_secrets.json')
    else
      raise ArgumentError,
            'No client_secrets.json filename supplied ' +
              'and/or could not be found in search path.'
    end
  end
  data = File.open(filename, 'r') { |file| JSON.parse(file.read) }
  return self.new(data)
end

Instance Method Details

#to_authorized_connectionObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/android/publisher/secrets.rb', line 79

def to_authorized_connection
  # OAuth2::Client.new(
  #   "49761657086.apps.googleusercontent.com", "Cp43nWEtueuPVEKPDJhmr4Mb",
  #   {:site => "https://accounts.android.com/", :authorize_url=>"/o/oauth2/auth", :token_url=>"/o/oauth2/token"}
  # )
  params = {
    :site           =>  URI.join(authorization_uri, '/'),
    :authorize_url  =>  authorization_uri.path,
    :token_url      =>  token_credential_uri.path
  }
  client = OAuth2::Client.new(client_id, client_secret, params)
  token  = OAuth2::AccessToken.new(client, access_token, { :refresh_token => refresh_token })
  token.refresh!
end