allow authentication to the hosting platform and store the credentials in the ~/.netrc file

did committed Apr 30, 2014
commit b3e57c4079151af75e6f62308aa6855a20d42184
Showing 4 changed files with 95 additions and 0 deletions
locomotive/wagon.rb b/lib/locomotive/wagon.rb +41 -0
@@ @@ -5,6 +5,47 @@ require 'locomotive/wagon/exceptions'
module Locomotive
module Wagon
+ # Authenticate an user to the Hosting platform.
+ # If the user does not exist, then create an account for her/him.
+ # At the end, store the API key in the ~/.netrc file
+ #
+ # @param [ String ] email The email of the user
+ # @param [ String ] password The password of the user
+ # @param [ Object ] shell Used to ask for/prompt information
+ #
+ def self.authenticate(email, password, shell)
+ require 'locomotive/wagon/misc/engine_api'
+ require 'netrc'
+
+ api = Locomotive::ClientAPI.new
+
+ if api_key = api.api_key(email, password)
+ # existing account
+ say "You have been successfully authenticated.", :green
+ else
+ # new account?
+ shell.say "No account found for #{email} or invalid credentials", :yellow
+
+ if shell.yes?('Do you want to create a new account? [Y/N]')
+ name = shell.ask 'What is your name?'
+
+ if account = api.create_account(name, email, password)
+ shell.say "Your account has been successfully created.", :green
+ api_key = account['api_key']
+ end
+ end
+ end
+
+ if api_key
+ # record the credentials
+ netrc = Netrc.read
+ netrc[api.host] = email, api_key
+ netrc.save
+ else
+ shell.say "We were unable to authenticate you on our platform.", :red
+ end
+ end
+
# Create a site from a site generator.
#
# @param [ String ] name The name of the site (underscored)
locomotive/wagon/cli.rb b/lib/locomotive/wagon/cli.rb +10 -0
@@ @@ -141,6 +141,16 @@ module Locomotive
say Locomotive::Wagon::VERSION
end
+ desc 'auth [EMAIL] [PASSWORD]', 'Log into the LocomotiveHosting platform'
+ def auth(email = nil, password = nil)
+ say "LocomotiveHosting Sign in/up\n\n", :bold
+
+ email ||= ask('Enter your e-mail?')
+ password ||= ask('Enter your password?')
+
+ Locomotive::Wagon.authenticate(email, password, shell)
+ end
+
desc 'init NAME [PATH] [OPTIONS]', 'Create a brand new site'
method_option :template, aliases: '-t', type: 'string', default: 'blank', desc: 'instead of building from a blank site, you can also have a pre-fetched site from a template (see the templates command)'
method_option :lib, aliases: '-l', type: 'string', desc: 'Path to an external ruby lib or generator'
locomotive/wagon/misc/engine_api.rb b/lib/locomotive/wagon/misc/engine_api.rb +43 -0
@@ @@ -0,0 +1,43 @@
+ require 'httparty'
+
+ module Locomotive
+ class ClientAPI
+
+ include HTTParty
+
+ base_uri ENV['HOSTING_URL'] || 'https://www.locomotivehosting.fr'
+
+ def host
+ URI(self.class.base_uri).host
+ end
+
+ def auth_token(email, password)
+ response = self.class.get('/locomotive/api/tokens.json', { body: { email: email, password: password }})
+
+ if response.success?
+ response['token']
+ end
+ end
+
+ def api_key(email, password)
+ if auth_token = auth_token(email, password)
+ my_account(auth_token)['api_key']
+ end
+ end
+
+ def my_account(auth_token)
+ self.class.get('/locomotive/api/my_account.json', { query: { auth_token: auth_token }})
+ end
+
+ def create_account(name, email, password)
+ account = { name: name, email: email, password: password, password_confirmation: password }
+
+ response = self.class.post('/locomotive/api/my_account.json', { body: { account: account }})
+
+ if response.success?
+ response.parsed_response
+ end
+ end
+
+ end
+ end
\ No newline at end of file
locomotivecms_wagon.gemspec +1 -0
@@ @@ -30,6 +30,7 @@ Gem::Specification.new do |gem|
gem.add_dependency 'rack-cache', '~> 1.1'
gem.add_dependency 'better_errors', '~> 1.0.1'
gem.add_dependency 'rubyzip', '~> 1.1.0'
+ gem.add_dependency 'netrc', '~> 0.7.7'
gem.add_dependency 'listen', '~> 2.4.0'