class KnifeGithubTokenCreate::GithubTokenCreate
Public Instance Methods
create_github_token(params)
click to toggle source
Create the OAuth authentication token for the knife-github application. @param params [Hash] Hash containing all options
params[:username] [String] Username if no token specified params[:password] [String] Password if no token specified
# File lib/chef/knife/github_token_create.rb, line 154 def create_github_token(params) Chef::Log.debug("Creating new application token for user: #{username}.") params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations" params[:body] = '{"note":"knife-github","scopes":["delete_repo", "user", "public_repo", "repo", "gist"]"}' params[:action] = "POST" connection.request(params) end
delete_github_token(params)
click to toggle source
# File lib/chef/knife/github_token_create.rb, line 162 def delete_github_token(params) Chef::Log.debug("Deleting token id: #{params[':id']}") params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations/#{params[:id]}" params[:action] = "DELETE" connection.request(params) end
run()
click to toggle source
# File lib/chef/knife/github_token_create.rb, line 62 def run extend Chef::Mixin::ShellOut # validate base options from base module. validate_base_options # Display information if debug mode is on. display_debug_info # Get the name_args from the command line username = name_args.first # Get token information token = get_github_token() unless config[:force] # Create github token if needed if token.nil? token = validate_github_token(username) update_knife_config(token) end puts "Finished updating your token. Using key:#{token}" end
update_knife_config(token)
click to toggle source
Updates the knife configuration with the token information inside ~/.chef/knife.rb @param token [String] token key
# File lib/chef/knife/github_token_create.rb, line 89 def update_knife_config(token) contents = '' update = false config = File.join(ENV["HOME"], ".chef/knife.rb") File.foreach(config) do |line| if line =~ /^\s*knife\[:github_token\].*/ && !token.nil? Chef::Log.debug("Replacing current token with: #{token}") contents = contents << "knife[:github_token] = \"#{token}\"\n" update = true else contents = contents << line end end unless update Chef::Log.debug("Updating configuration with token: #{token}") contents = contents << "knife[:github_token] = \"#{token}\"\n" end File.open(config, 'w') {|f| f.write(contents) } return true end
validate_github_token(username=nil)
click to toggle source
Validate the OAuth authentication token for the knife-github application. @param username [String] validates the token for specific user. (default is ENV)
# File lib/chef/knife/github_token_create.rb, line 113 def validate_github_token(username=nil) params = {} username = ENV["USER"] if username.nil? params[:url] = @github_url + "/api/" + @github_api_version + "/authorizations" Chef::Log.debug("Validating token information for user: #{username}.") params[:username] = username params[:password] = HighLine.new.ask("Please enter github password for #{username} :") { |q| q.echo = "x" } params[:action] = "GET" token_key = nil result = connection.request(params) result.each do |token| if token['app'] && token['app']['name'] == "knife-github (API)" if token['scopes'].include?("delete_repo") Chef::Log.debug("Found and using token: #{token_key}") token_key = token['token'] else Chef::Log.debug("Found token: #{token_key} but wrong scope, deleting token.") params[:id] = token['id'] delete_github_token(params) end end end if token_key.nil? result = create_github_token(params) token_key = result['token'] end return token_key end