Sha256: 500d216d7cee7eff2dd5d6641482d1b1ac77ea78b359a077e852f2ddb771d415

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

require 'sinatra'
require 'cgi'
require 'authmac'
require 'json'

set :app_file, __FILE__
def hmac_secret
  "very_secret_string_of_at_least_the_length_of_the_hash_so_64_for_sha256"
end

get '/' do
  erb :form
end

post '/sign' do
  @params = params.select { |_k, v| v != '' }
  @secret = hmac_secret
  @checker = Authmac::HmacChecker.new(hmac_secret, '|', 'sha256')
  @params_to_sign = @params.merge \
    'timestamp'    => Time.now.to_i.to_s,
    'version'      => '3',
    'nonce'        => 'implementing_apps_should_store_this_to_prevent_replays',
    'consumer_key' => 'key_to_find_secret'
  @hmac    = @checker.sign(@params_to_sign)
  @params_with_hmac      = @params_to_sign.merge('hmac' => @hmac)
  @link                  = @params_with_hmac.map{|k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join("&")

  erb :sign
end

get '/auth' do
  hmac_checker      = Authmac::HmacChecker.new(hmac_secret, '|', 'sha256')
  timestamp_checker = Authmac::TimestampChecker.new(30, 10)
  authenticator     = Authmac::Authenticator.new(hmac_checker, timestamp_checker)
  @validation       = authenticator.validate(params)

  if @validation.success?
    erb :auth_success
  elsif @validation.hmac_failure?
    erb :auth_hmac_failure
  elsif @validation.timestamp_failure?
    erb :auth_timestamp_failure
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
authmac-2.0.2 example/app.rb
authmac-2.0.1 example/app.rb
authmac-2.0.0 example/app.rb