Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'google/apis/gmail_v1'
- require 'google/api_client/client_secrets'
- require 'sinatra'
- require 'logger'
- require 'pp'
- require 'googleauth'
- require 'googleauth/stores/file_token_store'
- require 'fileutils'
- enable :sessions
- set :views, Proc.new { File.join(root, "site") }
- def logger; settings.logger end
- def user_credentials
- @authorization ||= (
- auth = settings.authorization.dup
- auth.redirect_uri = to('/oauth2callback')
- auth.update_token!(session)
- auth
- )
- end
- def authorize_gmail
- client_id = Google::Auth::ClientId.from_file(CREDENTIALS_PATH)
- token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
- authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
- user_id = 'default'
- credentials = authorizer.get_credentials(user_id)
- if credentials.nil?
- url = authorizer.get_authorization_url(base_url: OOB_URI)
- puts 'Open the following URL in the browser and enter the ' \
- "resulting code after authorization:\n" + url
- code = gets
- credentials = authorizer.get_and_store_credentials_from_code(
- user_id: user_id, code: code, base_url: OOB_URI
- )
- end
- credentials
- end
- def whitelist
- end
- configure do
- log_file = File.open('runtime.log', 'a+')
- log_file.sync = true
- logger = Logger.new(log_file)
- logger.level = Logger::INFO
- Google::Apis::ClientOptions.default.application_name = 'Sinatra Identity'
- Google::Apis::ClientOptions.default.application_version = '1.0.0'
- client_secrets = Google::APIClient::ClientSecrets.load
- authorization = client_secrets.to_authorization
- authorization.scope = 'email'
- set :authorization, authorization
- set :logger, logger
- end
- before do
- unless user_credentials.access_token || request.path_info =~ /^\/oauth2/
- redirect to('/oauth2authorize')
- end
- end
- after do
- session[:access_token] = user_credentials.access_token
- session[:refresh_token] = user_credentials.refresh_token
- session[:expires_in] = user_credentials.expires_in
- session[:issued_at] = user_credentials.issued_at
- end
- get '/oauth2authorize' do
- redirect user_credentials.authorization_uri.to_s, 303
- end
- get '/oauth2callback' do
- user_credentials.code = params[:code] if params[:code]
- user_credentials.fetch_access_token!
- redirect to('/')
- end
- #### ROUTES
- get '/' do
- send_file 'site/index.html'
- end
- get '/adminflow/' do
- send_file 'site/adminflow/index.html'
- end
- ##### TEST GMAIL FILTER
- get '/test/' do
- OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
- APPLICATION_NAME = 'Sinatra Identity'.freeze
- CREDENTIALS_PATH = 'credentials.json'.freeze
- TOKEN_PATH = 'token.yaml'.freeze
- SCOPE = Google::Apis::GmailV1::AUTH_GMAIL_READONLY
- # Initialize the API
- service = Google::Apis::GmailV1::GmailService.new
- service.client_options.application_name = APPLICATION_NAME
- service.authorization = authorize_gmail
- # get email
- user_id = 'me'
- result = service.get_user_profile(user_id)
- # process email
- pp result.email_address.to_s
- user_domain = result.email_address.to_s.split("@").last.to_s
- pp user_domain
- #unless some bloody regex whatever
- #send_file 'site/hi/index.html'
- end
Add Comment
Please, Sign In to add comment