Skip to content

Commit

Permalink
Add shortcut for setting authorization header and add basic auth func…
Browse files Browse the repository at this point in the history
…tionality.
  • Loading branch information
andrewhavens committed Mar 27, 2019
1 parent 0b0a5c5 commit d0fb6ff
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/android/main_activity.rb
@@ -1,7 +1,7 @@
class MainActivity < Android::App::Activity
TEST_HOST = 'https://jsonplaceholder.typicode.com'
# TEST_HOST = 'https://jsonplaceholder.typicode.com'
# TEST_HOST = 'http://localhost:4567'
# TEST_HOST = 'http://192.168.0.5:4567'
TEST_HOST = 'http://192.168.0.5:4567'

def onCreate(savedInstanceState)
super
Expand Down Expand Up @@ -38,8 +38,9 @@ def test_requests
# puts response.body
# end
# end
HTTP.basic_auth('admin', 'letmein').get("#{TEST_HOST}/basic_auth") do |response|
if response.success?
puts "new post response object: #{response.object.inspect}"
puts "successfully authenticated!"
else
puts response.body
end
Expand Down
13 changes: 13 additions & 0 deletions app/cocoa/app_delegate.rb
Expand Up @@ -56,6 +56,19 @@ def table_data
end
end
}
}, {
title: 'GET /basic_auth',
action: -> {
# HTTP.basic_auth('admin', 'letmein')
HTTP.basic_auth('admin', 'badpass')
.get("#{TEST_HOST}/basic_auth") do |response|
if response.success?
puts "successfully authenticated!"
else
puts response.body
end
end
}
}
]
}]
Expand Down
13 changes: 13 additions & 0 deletions lib/android/base64.rb
@@ -0,0 +1,13 @@
# Copied from https://github.com/HipByte/Flow/blob/44283b31a63bc826d2c068557b6357dc1195680b/flow/base64/android/base64.rb
class Base64
def self.encode(string)
bytes = Java::Lang::String.new(string).getBytes("UTF-8")
Android::Util::Base64.encodeToString(bytes, Android::Util::Base64::NO_WRAP)
end

def self.decode(string)
java_string = Java::Lang::String.new(string)
bytes = Android::Util::Base64.decode(java_string, Android::Util::Base64::NO_WRAP)
Java::Lang::String.new(bytes, "UTF-8")
end
end
12 changes: 12 additions & 0 deletions lib/cocoa/base64.rb
@@ -0,0 +1,12 @@
# Copied from https://github.com/HipByte/Flow/blob/44283b31a63bc826d2c068557b6357dc1195680b/flow/base64/cocoa/base64.rb
class Base64
def self.encode(string)
data = string.dataUsingEncoding(NSUTF8StringEncoding)
data.base64EncodedStringWithOptions(0)
end

def self.decode(string)
data = NSData.alloc.initWithBase64EncodedString(string, options: 0)
NSString.alloc.initWithData(data, encoding: NSUTF8StringEncoding)
end
end
8 changes: 8 additions & 0 deletions lib/common/http.rb
Expand Up @@ -11,6 +11,14 @@ def client(*args)
Client.new(*args)
end

def basic_auth(username, password)
client.basic_auth(username, password)
end

def auth(header_value)
client.auth(header_value)
end

def get(url, options = nil, &callback)
client.get(url, options, &callback)
end
Expand Down
11 changes: 11 additions & 0 deletions lib/common/http/client.rb
Expand Up @@ -26,6 +26,17 @@ def headers(hash = nil)
@headers
end

def basic_auth(username, password)
header_value = 'Basic ' + Base64.encode("#{username}:#{password}")
auth(header_value)
self
end

def auth(header_value)
@headers.set 'Authorization', header_value
self
end

def get(path, options = nil, &callback)
request(:get, path, options, &callback)
end
Expand Down
40 changes: 40 additions & 0 deletions test_server.rb
@@ -1,7 +1,37 @@
require 'sinatra'

if ENV['bind']
set :bind, ENV['bind']
end
disable :logging

helpers do
def restricted_area
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized\n"
end

def http_protect!
return if http_authorized?
restricted_area
end

def token_protect!
return if token_authorized?
restricted_area
end

def token_authorized?
request.env.fetch('HTTP_AUTHORIZATION', nil) &&
'rubymotion' == request.env['HTTP_AUTHORIZATION'].match(/Token token="(.*)"/).captures.first
end

def http_authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['admin', 'letmein']
end
end

before do
puts "#{request.request_method} #{request.path_info} #{request.env['HTTP_VERSION']}"
headers = Hash[*request.env.select {|k,v| k.start_with? 'HTTP_'}
Expand All @@ -22,6 +52,16 @@
puts
end

get '/basic_auth' do
http_protect!
"Welcome"
end

get('/token_auth') do
token_protect!
"Welcome"
end

get '*' do
headers 'content-type' => 'application/json'
body '[{"title":"Example Post"}]'
Expand Down

0 comments on commit d0fb6ff

Please sign in to comment.