Skip to content
Extraits de code Groupes Projets
Valider 0b5de2aa rédigé par danielgrippi's avatar danielgrippi
Parcourir les fichiers

WIP getting rid of app_url in params

parent d186246d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -96,6 +96,6 @@ group :test do ...@@ -96,6 +96,6 @@ group :test do
gem 'rspec-instafail', '>= 0.1.7', :require => false gem 'rspec-instafail', '>= 0.1.7', :require => false
gem 'fuubar' gem 'fuubar'
gem 'diaspora-client', :git => 'git@github.com:diaspora/diaspora-client.git' gem 'diaspora-client', :path => "~/workspace/diaspora-client" #:git => 'git@github.com:diaspora/diaspora-client.git'
#:path => "~/work/diaspora-client"
end end
...@@ -38,8 +38,14 @@ GIT ...@@ -38,8 +38,14 @@ GIT
eventmachine (>= 0.12.9) eventmachine (>= 0.12.9)
GIT GIT
remote: git@github.com:diaspora/diaspora-client.git remote: https://github.com/zhitomirskiyi/ruby-jwt
revision: 6b1b07fba73106755acd77d6def3e503df007a3f revision: fa7f46b5ac3653e30cf60abc78de9ffb3319dc0c
specs:
jwt (0.1.3)
json (>= 1.2.4)
PATH
remote: ~/workspace/diaspora-client
specs: specs:
diaspora-client (0.0.0) diaspora-client (0.0.0)
activerecord activerecord
...@@ -48,13 +54,6 @@ GIT ...@@ -48,13 +54,6 @@ GIT
oauth2 oauth2
sinatra sinatra
GIT
remote: https://github.com/zhitomirskiyi/ruby-jwt
revision: fa7f46b5ac3653e30cf60abc78de9ffb3319dc0c
specs:
jwt (0.1.3)
json (>= 1.2.4)
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/
specs: specs:
......
...@@ -25,15 +25,18 @@ class AuthorizationsController < ApplicationController ...@@ -25,15 +25,18 @@ class AuthorizationsController < ApplicationController
def token def token
require 'jwt' require 'jwt'
if (!params[:type] == 'client_associate' || !params[:manifest_url]) signed_string = Base64.decode64(params[:signed_string])
app_url = signed_string.split(';')[0]
if (!params[:type] == 'client_associate' && !app_url)
render :text => "bad request: #{params.inspect}", :status => 403 render :text => "bad request: #{params.inspect}", :status => 403
return return
end end
packaged_manifest = JSON.parse(RestClient.get(params[:manifest_url]).body) packaged_manifest = JSON.parse(RestClient.get("#{app_url}/manifest.json").body)
public_key = OpenSSL::PKey::RSA.new(packaged_manifest['public_key']) public_key = OpenSSL::PKey::RSA.new(packaged_manifest['public_key'])
manifest = JWT.decode(packaged_manifest['jwt'], public_key) manifest = JWT.decode(packaged_manifest['jwt'], public_key)
message = verify(params[:signed_string], params[:signature], public_key) message = verify(signed_string, Base64.decode64(params[:signature]), public_key, manifest)
if not (message =='ok') if not (message =='ok')
render :text => message, :status => 403 render :text => message, :status => 403
elsif manifest["homepage_url"].match(/^http:\/\/(localhost:\d+|chubbi\.es|cubbi\.es)\/$/).nil? elsif manifest["homepage_url"].match(/^http:\/\/(localhost:\d+|chubbi\.es|cubbi\.es)\/$/).nil?
...@@ -69,17 +72,18 @@ class AuthorizationsController < ApplicationController ...@@ -69,17 +72,18 @@ class AuthorizationsController < ApplicationController
# @param [String] sig A Base64 encoded signature of the decoded signed_string with public_key. # @param [String] sig A Base64 encoded signature of the decoded signed_string with public_key.
# @param [OpenSSL::PKey::RSA] public_key The application's public key to verify sig with. # @param [OpenSSL::PKey::RSA] public_key The application's public key to verify sig with.
# @return [String] 'ok' or an error message. # @return [String] 'ok' or an error message.
def verify( enc_signed_string, sig, public_key) def verify( signed_string, sig, public_key, manifest)
signed_string = Base64.decode64(enc_signed_string)
split = signed_string.split(';') split = signed_string.split(';')
app_url = split[0]
time = split[2] time = split[2]
nonce = split[3] nonce = split[3]
return 'blank public key' if public_key.n.nil? return 'blank public key' if public_key.n.nil?
return 'the app url in the manifest does not match the url passed in the parameters' if manifest["homepage_url"] != app_url
return 'key too small, use at least 2048 bits' if public_key.n.num_bits < 2048 return 'key too small, use at least 2048 bits' if public_key.n.num_bits < 2048
return "invalid time" unless valid_time?(time) return "invalid time" unless valid_time?(time)
return 'invalid nonce' unless valid_nonce?(nonce) return 'invalid nonce' unless valid_nonce?(nonce)
return 'invalid signature' unless verify_signature(signed_string, Base64.decode64(sig), public_key) return 'invalid signature' unless verify_signature(signed_string, sig, public_key)
'ok' 'ok'
end end
......
...@@ -7,6 +7,6 @@ gem 'json' ...@@ -7,6 +7,6 @@ gem 'json'
gem 'shotgun' gem 'shotgun'
gem 'sqlite3' gem 'sqlite3'
gem 'activerecord', '3.0.3' gem 'activerecord', '3.0.3'
gem 'diaspora-client', :git => 'git@github.com:diaspora/diaspora-client.git' gem 'diaspora-client', :path => "~/workspace/diaspora-client" #:git => 'git@github.com:diaspora/diaspora-client.git'
#:path => "~/work/diaspora-client"
GIT PATH
remote: git@github.com:diaspora/diaspora-client.git remote: ~/workspace/diaspora-client
revision: 6b1b07fba73106755acd77d6def3e503df007a3f
specs: specs:
diaspora-client (0.0.0) diaspora-client (0.0.0)
activerecord activerecord
......
...@@ -19,25 +19,26 @@ describe AuthorizationsController do ...@@ -19,25 +19,26 @@ describe AuthorizationsController do
@time = Time.now @time = Time.now
Time.stub(:now).and_return(@time) Time.stub(:now).and_return(@time)
@nonce = 'asdfsfasf' @nonce = 'asdfsfasf'
@signable_string = ["http://chubbi.es/",'http://pod.pod/',"#{Time.now.to_i}", @nonce].join(';') @signed_string = ["http://chubbi.es",'http://pod.pod',"#{Time.now.to_i}", @nonce].join(';')
end @signature = @private_key.sign(OpenSSL::Digest::SHA256.new, @signed_string)
describe '#token' do @manifest = {
before do
manifest = {
"name" => "Chubbies", "name" => "Chubbies",
"description" => "The best way to chub.", "description" => "The best way to chub.",
"homepage_url" => "http://chubbi.es/", "homepage_url" => "http://chubbi.es",
"icon_url" => "#", "icon_url" => "#",
"permissions_overview" => "I will use the permissions this way!", "permissions_overview" => "I will use the permissions this way!",
} }
end
packaged_manifest = {:public_key => @public_key.export, :jwt => JWT.encode(manifest, @private_key, "RS256")}.to_json describe '#token' do
before do
packaged_manifest = {:public_key => @public_key.export, :jwt => JWT.encode(@manifest, @private_key, "RS256")}.to_json
stub_request(:get, "http://chubbi.es/manifest.json"). stub_request(:get, "http://chubbi.es/manifest.json").
to_return(:status => 200, :body => packaged_manifest, :headers => {}) to_return(:status => 200, :body => packaged_manifest, :headers => {})
@params_hash = {:type => 'client_associate', :manifest_url => "http://chubbi.es/manifest.json" } @params_hash = {:type => 'client_associate', :signed_string => Base64.encode64(@signed_string), :signature => Base64.encode64(@signature)}
end end
context 'special casing (temporary, read note in the controller)' do context 'special casing (temporary, read note in the controller)' do
...@@ -107,12 +108,13 @@ describe AuthorizationsController do ...@@ -107,12 +108,13 @@ describe AuthorizationsController do
end end
it 'verifies the signable string validity(time,nonce,sig)' do it 'verifies the signable string validity(time,nonce,sig)' do
@controller.should_receive(:verify){|a,b,c| @controller.should_receive(:verify){|a,b,c,d|
a.should == 'signed_string' a.should == @signed_string
b.should == 'sig' b.should == @signature
c.export.should == @public_key.export c.export.should == @public_key.export
d.should == @manifest
} }
post :token, @params_hash.merge!({:signed_string => 'signed_string', :signature => 'sig'}) post :token, @params_hash
end end
end end
...@@ -152,31 +154,35 @@ describe AuthorizationsController do ...@@ -152,31 +154,35 @@ describe AuthorizationsController do
describe '#verify' do describe '#verify' do
before do before do
@controller.stub!(:verify_signature) @controller.stub!(:verify_signature)
@sig = Base64.encode64('sig') @sig = 'sig'
end end
it 'checks for valid time' do it 'checks for valid time' do
@controller.should_receive(:valid_time?).with(@time.to_i.to_s) @controller.should_receive(:valid_time?).with(@time.to_i.to_s)
@controller.verify(Base64.encode64(@signable_string), @sig, @public_key) @controller.verify(@signed_string, @sig, @public_key, @manifest)
end end
it 'checks the signature' do it 'checks the signature' do
@controller.should_receive(:verify_signature).with(@signable_string, 'sig', @public_key) @controller.should_receive(:verify_signature).with(@signed_string, 'sig', @public_key)
@controller.verify(Base64.encode64(@signable_string), @sig, @public_key) @controller.verify(@signed_string, @sig, @public_key, @manifest)
end end
it 'checks for valid nonce' do it 'checks for valid nonce' do
@controller.should_receive(:valid_nonce?).with(@nonce) @controller.should_receive(:valid_nonce?).with(@nonce)
@controller.verify(Base64.encode64(@signable_string), @sig, @public_key) @controller.verify(@signed_string, @sig, @public_key, @manifest)
end end
it 'checks for public key' do it 'checks for public key' do
@controller.verify(Base64.encode64(@signable_string), @sig, RSA.new()).should == "blank public key" @controller.verify(@signed_string, @sig, RSA.new(), @manifest).should == "blank public key"
end
it 'checks consistency of app_url' do
@controller.verify(@signed_string, @sig, @public_key, @manifest.merge({"homepage_url" => "http://badsite.com"})).should == "the app url in the manifest does not match the url passed in the parameters"
end end
it 'checks key size' do it 'checks key size' do
short_key = RSA.generate(100) short_key = RSA.generate(100)
RSA.stub!(:new).and_return(short_key) RSA.stub!(:new).and_return(short_key)
@controller.verify(Base64.encode64(@signable_string), @sig, RSA.generate(100).public_key). @controller.verify(@signed_string, @sig, RSA.generate(100).public_key, @manifest).
should == "key too small, use at least 2048 bits" should == "key too small, use at least 2048 bits"
end end
end end
...@@ -184,17 +190,17 @@ describe AuthorizationsController do ...@@ -184,17 +190,17 @@ describe AuthorizationsController do
describe '#verify_signature' do describe '#verify_signature' do
before do before do
@sig = @private_key.sign(OpenSSL::Digest::SHA256.new, @signable_string) @sig = @private_key.sign(OpenSSL::Digest::SHA256.new, @signed_string)
end end
it 'returns true if the signature is valid' do it 'returns true if the signature is valid' do
@controller.verify_signature(@signable_string, @sig, @public_key).should be_true @controller.verify_signature(@signed_string, @sig, @public_key).should be_true
end end
it 'returns false if the signature is invalid' do it 'returns false if the signature is invalid' do
@signable_string = "something else" @signed_string = "something else"
@controller.verify_signature(@signable_string, @sig, @public_key).should be_false @controller.verify_signature(@signed_string, @sig, @public_key).should be_false
end end
end end
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter