Sha256: f1e72e71e77dabe810322d6573b2d9ba856d23b01ce54b71d805ffbfb69b359f

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

module Imap::Backup
  module Configuration; end

  class Configuration::GmailOauth2
    BANNER = <<~BANNER.freeze
      GMail OAuth2 Setup

      You need to authorize imap_backup to get access to your email.
      To do so, please follow the instructions here:

      https://github.com/joeyates/imap-backup/docs/setting-up-gmail.md

    BANNER

    GMAIL_READ_SCOPE = "https://mail.google.com/".freeze
    OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze

    attr_reader :account
    attr_reader :client_id
    attr_reader :client_secret

    def initialize(account)
      @account = account
    end

    def run
      Kernel.system("clear")
      Kernel.puts BANNER
      @client_id = highline.ask("client_id: ")
      @client_secret = highline.ask("client_secret: ")

      Kernel.puts <<~MESSAGE

        Open the following URL in your browser

        #{authorization_url}

        Then copy the success code

      MESSAGE

      @code = highline.ask("success code: ")
      @credentials = authorizer.get_and_store_credentials_from_code(
        user_id: email, code: @code, base_url: OOB_URI
      )

      raise "Failed" if !@credentials

      token = JSON.parse(token_store.load(email))
      token["client_secret"] = client_secret
      token.to_json
    end

    private

    def email
      account[:username]
    end

    def highline
      Configuration::Setup.highline
    end

    def auth_client_id
      @auth_client_id = Google::Auth::ClientId.new(client_id, client_secret)
    end

    def authorizer
      @authorizer ||= Google::Auth::UserAuthorizer.new(
        auth_client_id, GMAIL_READ_SCOPE, token_store
      )
    end

    def token_store
      @token_store ||= Google::Auth::Stores::InMemoryTokenStore.new
    end

    def authorization_url
      authorizer.get_authorization_url(base_url: OOB_URI)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
imap-backup-3.2.0 lib/imap/backup/configuration/gmail_oauth2.rb
imap-backup-3.1.0 lib/imap/backup/configuration/gmail_oauth2.rb
imap-backup-3.0.0 lib/imap/backup/configuration/gmail_oauth2.rb
imap-backup-3.0.0.rc1 lib/imap/backup/configuration/gmail_oauth2.rb