Class: Tahweel::Authorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/tahweel/authorizer.rb

Overview

Handles the OAuth 2.0 Authorization flow for the application.

This class is responsible for:

  1. Determining the secure storage path for the token.

  2. Checking if valid credentials already exist.

  3. Initiating the OAuth 2.0 flow via a local web server if needed.

  4. Exchanging the authorization code for credentials and persisting them.

Constant Summary collapse

CLIENT_ID =
"512416833080-hptj9s5r92pjmdgigrcugbp40ng9isvj.apps.googleusercontent.com"
CLIENT_SECRET =
"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"
PORT =
3027
REDIRECT_URI =
"http://localhost:#{PORT}/".freeze
USER_ID =
"default"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthorizer

Initializes a new Authorizer instance. Sets up the Google Auth client and token store.



41
42
43
44
45
# File 'lib/tahweel/authorizer.rb', line 41

def initialize
  @client_id = Google::Auth::ClientId.new(CLIENT_ID, CLIENT_SECRET)
  @token_store = Google::Auth::Stores::FileTokenStore.new(file: token_path)
  @authorizer = Google::Auth::UserAuthorizer.new(@client_id, Google::Apis::DriveV3::AUTH_DRIVE_FILE, @token_store)
end

Class Method Details

.authorizeGoogle::Auth::UserRefreshCredentials

Convenience class method to authorize the user. Instantiates the Authorizer and calls #authorize.

Returns:

  • (Google::Auth::UserRefreshCredentials)

    The authorized credentials.



32
# File 'lib/tahweel/authorizer.rb', line 32

def self.authorize = new.authorize

.clear_credentialsvoid

This method returns an undefined value.

Convenience class method to clear stored credentials.



37
# File 'lib/tahweel/authorizer.rb', line 37

def self.clear_credentials = new.clear_credentials

Instance Method Details

#authorizeGoogle::Auth::UserRefreshCredentials

Performs the authorization process. Checks for existing valid credentials; if none are found, initiates the OAuth flow.

Returns:

  • (Google::Auth::UserRefreshCredentials)

    The valid user credentials.



51
52
53
54
55
56
# File 'lib/tahweel/authorizer.rb', line 51

def authorize
  credentials = @authorizer.get_credentials(USER_ID)
  return credentials if credentials

  perform_oauth_flow
end

#clear_credentialsvoid

This method returns an undefined value.

Clears the stored credentials file.



61
62
63
64
65
66
# File 'lib/tahweel/authorizer.rb', line 61

def clear_credentials
  path = token_path
  return unless File.exist?(path)

  File.delete(path)
end