whateverjustletmeinf

google oauth sinatra mkdocs

Sep 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.17 KB | None | 0 0
  1. require 'google/apis/gmail_v1'
  2. require 'google/api_client/client_secrets'
  3. require 'sinatra'
  4. require 'logger'
  5. require 'pp'
  6. require 'googleauth'
  7. require 'googleauth/stores/file_token_store'
  8. require 'fileutils'
  9.  
  10. enable :sessions
  11.  
  12. set :views, Proc.new { File.join(root, "site") }
  13.  
  14. def logger; settings.logger end
  15.  
  16. def user_credentials
  17.   @authorization ||= (
  18.     auth = settings.authorization.dup
  19.     auth.redirect_uri = to('/oauth2callback')
  20.     auth.update_token!(session)
  21.     auth
  22.   )
  23. end
  24. def authorize_gmail
  25.   client_id = Google::Auth::ClientId.from_file(CREDENTIALS_PATH)
  26.   token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
  27.   authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
  28.   user_id = 'default'
  29.   credentials = authorizer.get_credentials(user_id)
  30.   if credentials.nil?
  31.     url = authorizer.get_authorization_url(base_url: OOB_URI)
  32.     puts 'Open the following URL in the browser and enter the ' \
  33.          "resulting code after authorization:\n" + url
  34.     code = gets
  35.     credentials = authorizer.get_and_store_credentials_from_code(
  36.       user_id: user_id, code: code, base_url: OOB_URI
  37.     )
  38.   end
  39.   credentials
  40. end
  41. def whitelist
  42.  
  43. end
  44.  
  45. configure do
  46.   log_file = File.open('runtime.log', 'a+')
  47.   log_file.sync = true
  48.   logger = Logger.new(log_file)
  49.   logger.level = Logger::INFO
  50.  
  51.   Google::Apis::ClientOptions.default.application_name = 'Sinatra Identity'
  52.   Google::Apis::ClientOptions.default.application_version = '1.0.0'
  53.   client_secrets = Google::APIClient::ClientSecrets.load
  54.  
  55.   authorization = client_secrets.to_authorization
  56.   authorization.scope = 'email'
  57.  
  58.   set :authorization, authorization
  59.   set :logger, logger
  60. end
  61.  
  62. before do
  63.   unless user_credentials.access_token || request.path_info =~ /^\/oauth2/
  64.     redirect to('/oauth2authorize')
  65.   end
  66. end
  67.  
  68. after do
  69.   session[:access_token] = user_credentials.access_token
  70.   session[:refresh_token] = user_credentials.refresh_token
  71.   session[:expires_in] = user_credentials.expires_in
  72.   session[:issued_at] = user_credentials.issued_at
  73. end
  74.  
  75. get '/oauth2authorize' do
  76.   redirect user_credentials.authorization_uri.to_s, 303
  77. end
  78.  
  79. get '/oauth2callback' do
  80.   user_credentials.code = params[:code] if params[:code]
  81.   user_credentials.fetch_access_token!
  82.   redirect to('/')
  83. end
  84.  
  85. #### ROUTES
  86.  
  87. get '/' do
  88.   send_file 'site/index.html'
  89. end
  90. get '/adminflow/' do
  91.   send_file 'site/adminflow/index.html'
  92. end
  93.  
  94. ##### TEST GMAIL FILTER
  95.  
  96. get '/test/' do
  97.   OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
  98.   APPLICATION_NAME = 'Sinatra Identity'.freeze
  99.   CREDENTIALS_PATH = 'credentials.json'.freeze
  100.   TOKEN_PATH = 'token.yaml'.freeze
  101.   SCOPE = Google::Apis::GmailV1::AUTH_GMAIL_READONLY
  102.   # Initialize the API
  103.   service = Google::Apis::GmailV1::GmailService.new
  104.   service.client_options.application_name = APPLICATION_NAME
  105.   service.authorization = authorize_gmail
  106.   # get email
  107.   user_id = 'me'
  108.   result = service.get_user_profile(user_id)
  109.   # process email
  110.   pp result.email_address.to_s
  111.   user_domain = result.email_address.to_s.split("@").last.to_s
  112.   pp user_domain
  113.   #unless some bloody regex whatever
  114.   #send_file 'site/hi/index.html'
  115. end
Add Comment
Please, Sign In to add comment