Class: GoogleAuthenticationBridge::GoogleAuthentication
- Inherits:
-
Object
- Object
- GoogleAuthenticationBridge::GoogleAuthentication
- Defined in:
- lib/google_auth_bridge.rb
Constant Summary collapse
- GOOGLE_REDIRECT_URI =
'urn:ietf:wg:oauth:2.0:oob'
Class Method Summary collapse
Instance Method Summary collapse
- #get_auth_url ⇒ Object
- #get_oauth2_access_token(authorization_code = nil) ⇒ Object
- #get_tokens(authorization_code = nil) ⇒ Object
-
#initialize(scope, client_id, client_secret, token_file) ⇒ GoogleAuthentication
constructor
A new instance of GoogleAuthentication.
- #load_token_from_file ⇒ Object
- #save_token_to_file(refresh_token) ⇒ Object
- #token_file_exists? ⇒ Boolean
Constructor Details
#initialize(scope, client_id, client_secret, token_file) ⇒ GoogleAuthentication
Returns a new instance of GoogleAuthentication.
23 24 25 26 27 28 |
# File 'lib/google_auth_bridge.rb', line 23 def initialize(scope, client_id, client_secret, token_file) @scope = scope @client_id = client_id @client_secret = client_secret @token_file = token_file end |
Class Method Details
.create_from_config_file(scope, file_name, token_file) ⇒ Object
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/google_auth_bridge.rb', line 8 def self.create_from_config_file(scope, file_name, token_file) config = load_file(file_name) client_id, client_secret = config[:google_client_id], config[:google_client_secret] raise InvalidFileFormatError.new(token_file) if client_id.nil? or client_secret.nil? GoogleAuthentication.new( scope, client_id, client_secret, token_file) end |
.load_file(file_name) ⇒ Object
19 20 21 |
# File 'lib/google_auth_bridge.rb', line 19 def self.load_file(file_name) YAML.load_file(file_name) end |
Instance Method Details
#get_auth_url ⇒ Object
44 45 46 |
# File 'lib/google_auth_bridge.rb', line 44 def get_auth_url "https://accounts.google.com/o/oauth2/auth?response_type=code&scope=#{URI::encode(@scope)}&redirect_uri=#{GOOGLE_REDIRECT_URI}&client_id=#{URI::encode(@client_id)}" end |
#get_oauth2_access_token(authorization_code = nil) ⇒ Object
40 41 42 |
# File 'lib/google_auth_bridge.rb', line 40 def get_oauth2_access_token(=nil) OAuth2::AccessToken.from_hash(get_oauth2_client, get_tokens()) end |
#get_tokens(authorization_code = nil) ⇒ Object
34 35 36 37 38 |
# File 'lib/google_auth_bridge.rb', line 34 def get_tokens(=nil) client = Google::APIClient.new setup_credentials(client, ) refresh_tokens(client) end |
#load_token_from_file ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/google_auth_bridge.rb', line 48 def load_token_from_file raise FileNotFoundError.new(@token_file) unless token_file_exists? begin token_data = GoogleAuthentication.load_file(@token_file) token_data[:refresh_token] or raise rescue raise InvalidFileFormatError.new(@token_file) end end |
#save_token_to_file(refresh_token) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/google_auth_bridge.rb', line 59 def save_token_to_file(refresh_token) raise InvalidTokenError.new(refresh_token) if refresh_token.nil? File.open(@token_file, 'w') { |f| f.write(YAML.dump({:refresh_token => refresh_token})) } end |
#token_file_exists? ⇒ Boolean
30 31 32 |
# File 'lib/google_auth_bridge.rb', line 30 def token_file_exists? File.exists? @token_file end |