From 18cb3ebbcafe09a194c938e8884f9d3f3753f635 Mon Sep 17 00:00:00 2001 From: thea Date: Fri, 2 Sep 2016 15:16:15 +0200 Subject: moved bearer token script included passing bearer token into secrets-file --- script/generate_bearer_token.rb | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 script/generate_bearer_token.rb (limited to 'script') diff --git a/script/generate_bearer_token.rb b/script/generate_bearer_token.rb new file mode 100644 index 0000000..d3a1e4a --- /dev/null +++ b/script/generate_bearer_token.rb @@ -0,0 +1,53 @@ +require "net/http" +require "uri" +require "json" +require "base64" +require "optparse" + +options = {} + +option_parser = OptionParser.new do |opts| + opts.banner = "Create your bearer_token for twitter by including following [options]:" + + opts.on("--key KEY", "consumer_key of your twitter application") do |key| + options[:conkey] = key + end + + opts.on("--secret SECRET", "consumer_secret of your twitter application") do |secret| + options[:consec] = secret + end + + opts.on("--file FILE", "file where the bearer_token should be stored to (e.g. config/secrets.yml)") do |file| + options[:file] = file + end + +end + +option_parser.parse! + +if options[:conkey].nil? || options[:consec].nil? then + puts option_parser + exit +else + consumer_key = options[:conkey] + consumer_secret = options[:consec] +end + +uri = URI("https://api.twitter.com/oauth2/token") +data = "grant_type=client_credentials" +cre = Base64.strict_encode64("#{consumer_key}:#{consumer_secret}") +authorization_headers = { "Authorization" => "Basic #{cre}"} + +Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| + response = http.request_post(uri, data, authorization_headers) + token_hash = JSON.parse(response.body) + @bearer_token = token_hash["access_token"] +end + +if options[:file].nil? then + puts @bearer_token +else + if options[:file] == "config/secrets.yml" + Rails.application.secrets.twitter['bearer_token'] = @bearer_token + end +end -- cgit v1.2.3 From 3c617c92ef3c71b93ed8f68b107d4da080e28872 Mon Sep 17 00:00:00 2001 From: thea Date: Tue, 6 Sep 2016 17:28:21 +0200 Subject: deleted script/generate_bearer_token.rb, it will be replaced by script/generate_bearer_token --- script/generate_bearer_token.rb | 53 ----------------------------------------- 1 file changed, 53 deletions(-) delete mode 100644 script/generate_bearer_token.rb (limited to 'script') diff --git a/script/generate_bearer_token.rb b/script/generate_bearer_token.rb deleted file mode 100644 index d3a1e4a..0000000 --- a/script/generate_bearer_token.rb +++ /dev/null @@ -1,53 +0,0 @@ -require "net/http" -require "uri" -require "json" -require "base64" -require "optparse" - -options = {} - -option_parser = OptionParser.new do |opts| - opts.banner = "Create your bearer_token for twitter by including following [options]:" - - opts.on("--key KEY", "consumer_key of your twitter application") do |key| - options[:conkey] = key - end - - opts.on("--secret SECRET", "consumer_secret of your twitter application") do |secret| - options[:consec] = secret - end - - opts.on("--file FILE", "file where the bearer_token should be stored to (e.g. config/secrets.yml)") do |file| - options[:file] = file - end - -end - -option_parser.parse! - -if options[:conkey].nil? || options[:consec].nil? then - puts option_parser - exit -else - consumer_key = options[:conkey] - consumer_secret = options[:consec] -end - -uri = URI("https://api.twitter.com/oauth2/token") -data = "grant_type=client_credentials" -cre = Base64.strict_encode64("#{consumer_key}:#{consumer_secret}") -authorization_headers = { "Authorization" => "Basic #{cre}"} - -Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| - response = http.request_post(uri, data, authorization_headers) - token_hash = JSON.parse(response.body) - @bearer_token = token_hash["access_token"] -end - -if options[:file].nil? then - puts @bearer_token -else - if options[:file] == "config/secrets.yml" - Rails.application.secrets.twitter['bearer_token'] = @bearer_token - end -end -- cgit v1.2.3 From bd5a537964d835ae1b572ecc0cf7288e30bcc1db Mon Sep 17 00:00:00 2001 From: thea Date: Tue, 6 Sep 2016 17:29:36 +0200 Subject: bearer token script includes creating token, creating secrets-file or just have the missing information filled like token or twitter handle --- script/generate_bearer_token | 81 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 script/generate_bearer_token (limited to 'script') diff --git a/script/generate_bearer_token b/script/generate_bearer_token new file mode 100755 index 0000000..7f7bb6b --- /dev/null +++ b/script/generate_bearer_token @@ -0,0 +1,81 @@ +#!/usr/bin/env ruby + +require "net/http" +require "uri" +require "json" +require "base64" +require "optparse" +require "yaml" + +options = {} + +option_parser = OptionParser.new do |opts| + opts.banner = "Create your bearer_token for twitter by including following two [options], feel free to have your secrets-file created/filled giving the other information as well:" + + opts.on("--key KEY", "consumer_key of your twitter application") do |key| + options[:conkey] = key + end + + opts.on("--secret SECRET", "consumer_secret of your twitter application") do |secret| + options[:consec] = secret + end + + opts.on("--projectroot DIR", "directory where leapweb is") do |projectroot| + options[:projectroot] = projectroot + end + + opts.on("--twitterhandle TWI", "twitterhandle without @ which will be passed into secrets-file") do |twitterhandle| + options[:twitterhandle] = twitterhandle + end + +end + +option_parser.parse! + +if options[:conkey].nil? || options[:consec].nil? then + puts option_parser + exit +else + consumer_key = options[:conkey] + consumer_secret = options[:consec] +end + +uri = URI("https://api.twitter.com/oauth2/token") +data = "grant_type=client_credentials" +cre = Base64.strict_encode64("#{consumer_key}:#{consumer_secret}") +authorization_headers = { "Authorization" => "Basic #{cre}"} + +Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| + response = http.request_post(uri, data, authorization_headers) + token_hash = JSON.parse(response.body) + $bearer_token = token_hash["access_token"] +end + +if options[:projectroot].nil? then + puts "You didn't tell us the directory to have your secrets-file created or being filled. Feel free to copy/paste your bearer_token:" + puts $bearer_token +else + if File.exist?("#{options[:projectroot]}/leap_web/config/secrets.yml") + secrets = YAML.load_file("#{options[:projectroot]}/leap_web/config/secrets.yml") + else + # secrets_content = {"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}} + # secrets = {"development"=> secrets_content, "test"=>secrets_content} + secrets = {"development"=> {"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}, "test"=>{"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}} + File.new("#{options[:projectroot]}/leap_web/config/secrets.yml", "w") + end + + if options[:twitterhandle].nil? then + if secrets["development"]["twitter"]["twitter_handle"] == "" then + puts "You didn't put your twitter-handle neither in the secrets-file nor passed it as a flag. Don't forget that you can't use the twitter-feature without your twitter-handle." + end + else + secrets["development"]["twitter"]["twitter_handle"] = options[:twitterhandle] + end + + secrets["development"]["twitter"]["bearer_token"] = $bearer_token + secrets["test"]["twitter"]["bearer_token"] = $bearer_token + + File.open("#{options[:projectroot]}/leap_web/config/secrets.yml", "r+") do |file| + file.write(secrets.to_yaml) + end +end -- cgit v1.2.3 From a9ce836e2ef5a2aaa8b30ca7603129ba9573deaf Mon Sep 17 00:00:00 2001 From: thea Date: Tue, 6 Sep 2016 17:48:03 +0200 Subject: included twitter handle also for test development in secrets-file --- script/generate_bearer_token | 1 + 1 file changed, 1 insertion(+) (limited to 'script') diff --git a/script/generate_bearer_token b/script/generate_bearer_token index 7f7bb6b..ec00740 100755 --- a/script/generate_bearer_token +++ b/script/generate_bearer_token @@ -70,6 +70,7 @@ else end else secrets["development"]["twitter"]["twitter_handle"] = options[:twitterhandle] + secrets["test"]["twitter"]["twitter_handle"] = options[:twitterhandle] end secrets["development"]["twitter"]["bearer_token"] = $bearer_token -- cgit v1.2.3 From f269b960f9928ce3211ee3ab80e7a8eead426dac Mon Sep 17 00:00:00 2001 From: thea Date: Wed, 7 Sep 2016 11:34:35 +0200 Subject: error message and exit from script when wrong directory or no secrets-file. automatic secrets-file creation was deleted/put as comment --- script/generate_bearer_token | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'script') diff --git a/script/generate_bearer_token b/script/generate_bearer_token index ec00740..7091a8d 100755 --- a/script/generate_bearer_token +++ b/script/generate_bearer_token @@ -58,10 +58,12 @@ else if File.exist?("#{options[:projectroot]}/leap_web/config/secrets.yml") secrets = YAML.load_file("#{options[:projectroot]}/leap_web/config/secrets.yml") else + puts "Please make sure that you created a secrets-file as described in the documentation or have given the correct directory. No secrets-file could be found." + exit # secrets_content = {"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}} # secrets = {"development"=> secrets_content, "test"=>secrets_content} - secrets = {"development"=> {"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}, "test"=>{"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}} - File.new("#{options[:projectroot]}/leap_web/config/secrets.yml", "w") + # secrets = {"development"=> {"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}, "test"=>{"twitter"=>{"enabled"=>false, "twitter_handle"=>"", "bearer_token"=>"", "twitter_picture"=>nil}}} + # File.new("#{options[:projectroot]}/leap_web/config/secrets.yml", "w") end if options[:twitterhandle].nil? then -- cgit v1.2.3 From dcd20f9b532bc7490e45ddda68e228f4c9bf2b93 Mon Sep 17 00:00:00 2001 From: thea Date: Fri, 9 Sep 2016 14:54:01 +0200 Subject: included script to invalidate token --- script/invalidate_bearer_token | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 script/invalidate_bearer_token (limited to 'script') diff --git a/script/invalidate_bearer_token b/script/invalidate_bearer_token new file mode 100755 index 0000000..eda1c7d --- /dev/null +++ b/script/invalidate_bearer_token @@ -0,0 +1,47 @@ +#!/usr/bin/env ruby + +require "net/http" +require "uri" +require "json" +require "base64" +require "optparse" + +options = {} + +option_parser = OptionParser.new do |opts| + opts.banner = "Invalidate your bearer_token for twitter by including the following [options]. The bearer token can't be used afterwards anymore. Please create a new bearer-token if you want to activate the twitter feature again." + + opts.on("--key KEY", "consumer_key of your twitter application") do |key| + options[:conkey] = key + end + + opts.on("--secret SECRET", "consumer_secret of your twitter application") do |secret| + options[:consec] = secret + end + + opts.on("--token TOKEN", "bearer token for twitter") do |token| + options[:token] = token + end + +end + +option_parser.parse! + +if options[:conkey].nil? || options[:consec].nil? || options[:token].nil? then + puts option_parser + exit +else + consumer_key = options[:conkey] + consumer_secret = options[:consec] + bearer_token = options[:token] +end + +uri = URI("https://api.twitter.com/oauth2/invalidate_token") +data = "access_token=#{bearer_token}" +cre = Base64.strict_encode64("#{consumer_key}:#{consumer_secret}") +authorization_headers = { "Authorization" => "Basic #{cre}"} + +Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| + response = http.request_post(uri, data, authorization_headers) + puts JSON.parse(response.body) +end -- cgit v1.2.3 From 94a5b32a58f5f4fb63eaf677a91af63e1fe3e325 Mon Sep 17 00:00:00 2001 From: thea Date: Thu, 15 Sep 2016 15:29:10 +0200 Subject: included production for readme & secrets --- script/generate_bearer_token | 2 ++ 1 file changed, 2 insertions(+) (limited to 'script') diff --git a/script/generate_bearer_token b/script/generate_bearer_token index 7091a8d..60e4600 100755 --- a/script/generate_bearer_token +++ b/script/generate_bearer_token @@ -73,10 +73,12 @@ else else secrets["development"]["twitter"]["twitter_handle"] = options[:twitterhandle] secrets["test"]["twitter"]["twitter_handle"] = options[:twitterhandle] + secrets["production"]["twitter"]["twitter_handle"] = options[:twitterhandle] end secrets["development"]["twitter"]["bearer_token"] = $bearer_token secrets["test"]["twitter"]["bearer_token"] = $bearer_token + secrets["production"]["twitter"]["twitter_handle"] = $bearer_token File.open("#{options[:projectroot]}/leap_web/config/secrets.yml", "r+") do |file| file.write(secrets.to_yaml) -- cgit v1.2.3 From a95c0dfe3f70671ee354648fc4b25a79d5b57824 Mon Sep 17 00:00:00 2001 From: thea Date: Tue, 20 Sep 2016 12:13:30 +0200 Subject: deleted leap_web from projectroot --- script/generate_bearer_token | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'script') diff --git a/script/generate_bearer_token b/script/generate_bearer_token index 60e4600..3a09382 100755 --- a/script/generate_bearer_token +++ b/script/generate_bearer_token @@ -55,8 +55,8 @@ if options[:projectroot].nil? then puts "You didn't tell us the directory to have your secrets-file created or being filled. Feel free to copy/paste your bearer_token:" puts $bearer_token else - if File.exist?("#{options[:projectroot]}/leap_web/config/secrets.yml") - secrets = YAML.load_file("#{options[:projectroot]}/leap_web/config/secrets.yml") + if File.exist?("#{options[:projectroot]}/config/secrets.yml") + secrets = YAML.load_file("#{options[:projectroot]}/config/secrets.yml") else puts "Please make sure that you created a secrets-file as described in the documentation or have given the correct directory. No secrets-file could be found." exit -- cgit v1.2.3